Skip to content

Commit

Permalink
Generate getters for interface fields (#2314)
Browse files Browse the repository at this point in the history
* Generate getters for interface fields

* Changes to make models_test.go pass

* Use text/template, not html/template

* Re-run go generate ./...

* gofmt a few files that were failing lint checks

* Another gofmt straggler

* Try making the "generated" match the exact whitespace github is disliking

Co-authored-by: Bill Rose <neptoess@gmail.com>
  • Loading branch information
neptoess and Bill Rose committed Aug 5, 2022
1 parent 0d91c89 commit 242c3ba
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@
*.test
*.out
gqlgen
*.exe
10 changes: 8 additions & 2 deletions _examples/selection/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions _examples/starwars/models/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions _examples/starwars/models/model.go
Expand Up @@ -13,6 +13,12 @@ type CharacterFields struct {
AppearsIn []Episode
}

func (cf CharacterFields) GetID() string { return cf.ID }
func (cf CharacterFields) GetName() string { return cf.Name }
func (cf CharacterFields) GetAppearsIn() []Episode { return cf.AppearsIn }
func (cf CharacterFields) GetFriendsConnection() *FriendsConnection { return nil } // Handled in resolver
func (cf CharacterFields) GetFriends() []Character { return nil } // Handled in resolver

type Human struct {
CharacterFields
StarshipIds []string
Expand Down
5 changes: 4 additions & 1 deletion _examples/type-system-extension/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion codegen/config/binder.go
Expand Up @@ -216,7 +216,6 @@ func (t *TypeReference) IsPtr() bool {
}

// fix for https://github.com/golang/go/issues/31103 may make it possible to remove this (may still be useful)
//
func (t *TypeReference) IsPtrToPtr() bool {
if p, isPtr := t.GO.(*types.Pointer); isPtr {
_, isPtr := p.Elem().(*types.Pointer)
Expand Down
2 changes: 1 addition & 1 deletion codegen/testserver/followschema/interfaces.go
Expand Up @@ -49,7 +49,7 @@ func (n *ConcreteNodeA) Child() (Node, error) {
return n.child, nil
}

// Implements the Node interface with another interface
// Implements the Node interface with another interface
type ConcreteNodeInterface interface {
Node
ID() string
Expand Down
7 changes: 5 additions & 2 deletions codegen/testserver/followschema/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/testserver/singlefile/interfaces.go
Expand Up @@ -49,7 +49,7 @@ func (n *ConcreteNodeA) Child() (Node, error) {
return n.child, nil
}

// Implements the Node interface with another interface
// Implements the Node interface with another interface
type ConcreteNodeInterface interface {
Node
ID() string
Expand Down
7 changes: 5 additions & 2 deletions codegen/testserver/singlefile/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions plugin/federation/fieldset/fieldset.go
Expand Up @@ -11,15 +11,12 @@ import (

// Set represents a FieldSet that is used in federation directives @key and @requires.
// Would be happier to reuse FieldSet parsing from gqlparser, but this suits for now.
//
type Set []Field

// Field represents a single field in a FieldSet
//
type Field []string

// New parses a FieldSet string into a TinyFieldSet.
//
func New(raw string, prefix []string) Set {
if !strings.Contains(raw, "{") {
return parseUnnestedKeyFieldSet(raw, prefix)
Expand Down Expand Up @@ -48,7 +45,6 @@ func New(raw string, prefix []string) Set {
}

// FieldDefinition looks up a field in the type.
//
func (f Field) FieldDefinition(schemaType *ast.Definition, schema *ast.Schema) *ast.FieldDefinition {
objType := schemaType
def := objType.Fields.ForName(f[0])
Expand All @@ -74,7 +70,6 @@ func (f Field) FieldDefinition(schemaType *ast.Definition, schema *ast.Schema) *
}

// TypeReference looks up the type of a field.
//
func (f Field) TypeReference(obj *codegen.Object, objects codegen.Objects) *codegen.Field {
var def *codegen.Field

Expand All @@ -89,7 +84,6 @@ func (f Field) TypeReference(obj *codegen.Object, objects codegen.Objects) *code
}

// ToGo converts a (possibly nested) field into a proper public Go name.
//
func (f Field) ToGo() string {
var ret string

Expand All @@ -100,7 +94,6 @@ func (f Field) ToGo() string {
}

// ToGoPrivate converts a (possibly nested) field into a proper private Go name.
//
func (f Field) ToGoPrivate() string {
var ret string

Expand All @@ -115,13 +108,11 @@ func (f Field) ToGoPrivate() string {
}

// Join concatenates the field parts with a string separator between. Useful in templates.
//
func (f Field) Join(str string) string {
return strings.Join(f, str)
}

// JoinGo concatenates the Go name of field parts with a string separator between. Useful in templates.
//
func (f Field) JoinGo(str string) string {
strs := []string{}

Expand All @@ -138,7 +129,6 @@ func (f Field) LastIndex() int {
// local functions

// parseUnnestedKeyFieldSet // handles simple case where none of the fields are nested.
//
func parseUnnestedKeyFieldSet(raw string, prefix []string) Set {
ret := Set{}

Expand All @@ -150,7 +140,6 @@ func parseUnnestedKeyFieldSet(raw string, prefix []string) Set {
}

// extractSubs splits out and trims sub-expressions from before, inside, and after "{}".
//
func extractSubs(str string) (string, string, string) {
start := strings.Index(str, "{")
end := matchingBracketIndex(str, start)
Expand All @@ -162,7 +151,6 @@ func extractSubs(str string) (string, string, string) {
}

// matchingBracketIndex returns the index of the closing bracket, assuming an open bracket at start.
//
func matchingBracketIndex(str string, start int) int {
if start < 0 || len(str) <= start+1 {
return -1
Expand Down

0 comments on commit 242c3ba

Please sign in to comment.