Skip to content

Commit

Permalink
Support interface unexported methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dvic committed Jul 5, 2018
1 parent f79b6a5 commit 455343c
Show file tree
Hide file tree
Showing 9 changed files with 1,187 additions and 5 deletions.
2 changes: 1 addition & 1 deletion codegen/interface_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cfg *Config) isValueReceiver(intf *NamedType, implementor *NamedType, prog
for i := 0; i < interfaceType.NumMethods(); i++ {
intfMethod := interfaceType.Method(i)

implMethod := findMethod(implementorType, intfMethod.Name())
implMethod := findMethod(implementorType, intfMethod.Name(), false)
if implMethod == nil {
fmt.Fprintf(os.Stderr, "missing method %s on %s\n", intfMethod.Name(), implementor.GoType)
return false
Expand Down
12 changes: 8 additions & 4 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ func findGoInterface(prog *loader.Program, pkgName string, typeName string) (*ty
return underlying, nil
}

func findMethod(typ *types.Named, name string) *types.Func {
func findExportedMethod(typ *types.Named, name string) *types.Func {
return findMethod(typ, name, true)
}

func findMethod(typ *types.Named, name string, onlyExported bool) *types.Func {
for i := 0; i < typ.NumMethods(); i++ {
method := typ.Method(i)
if !method.Exported() {
if !method.Exported() && onlyExported {
continue
}

Expand All @@ -93,7 +97,7 @@ func findMethod(typ *types.Named, name string) *types.Func {
}

if named, ok := field.Type().(*types.Named); ok {
if f := findMethod(named, name); f != nil {
if f := findExportedMethod(named, name); f != nil {
return f
}
}
Expand Down Expand Up @@ -144,7 +148,7 @@ func bindObject(t types.Type, object *Object, imports *Imports) error {

for i := range object.Fields {
field := &object.Fields[i]
if method := findMethod(namedType, field.GQLName); method != nil {
if method := findExportedMethod(namedType, field.GQLName); method != nil {
sig := method.Type().(*types.Signature)
field.GoMethodName = "obj." + method.Name()
field.Type.Modifiers = modifiersFromGoType(sig.Results().At(0).Type())
Expand Down
12 changes: 12 additions & 0 deletions example/iface/.gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schema: schema.graphql
exec:
filename: generated.go
model:
filename: models_gen.go
models:
Bar:
model: github.com/vektah/gqlgen/example/iface/models.Bar
Entity:
model: github.com/vektah/gqlgen/example/iface/models.Entity
Foo:
model: github.com/vektah/gqlgen/example/iface/models.Foo
Loading

0 comments on commit 455343c

Please sign in to comment.