From 5c083c792afa0b0ab9fd61221df7681922b6c723 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Sat, 3 Dec 2022 15:13:56 -0500 Subject: [PATCH] use goField directive for getters generation (#2447) * consider goField directive for getters generation * Re-generate to pass linting Signed-off-by: Steve Coffman Signed-off-by: Steve Coffman Co-authored-by: Umang Parmar --- plugin/modelgen/models.go | 19 +++++++++++++++++++ plugin/modelgen/out/generated.go | 13 +++++++++++++ plugin/modelgen/testdata/schema.graphql | 14 ++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 45c32715c6c..b3a1ccdbbdc 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -23,6 +23,11 @@ type FieldMutateHook = func(td *ast.Definition, fd *ast.FieldDefinition, f *Fiel // defaultFieldMutateHook is the default hook for the Plugin which applies the GoTagFieldHook. func defaultFieldMutateHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) { + var err error + f, err = GoFieldHook(td, fd, f) + if err != nil { + return f, err + } return GoTagFieldHook(td, fd, f) } @@ -412,6 +417,20 @@ func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Fie return f, nil } +// GoFieldHook applies the goField directive to the generated Field f. +func GoFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) { + args := make([]string, 0) + _ = args + for _, goField := range fd.Directives.ForNames("goField") { + if arg := goField.Arguments.ForName("name"); arg != nil { + if k, err := arg.Value.Value(nil); err == nil { + f.GoName = k.(string) + } + } + } + return f, nil +} + func isStruct(t types.Type) bool { _, is := t.Underlying().(*types.Struct) return is diff --git a/plugin/modelgen/out/generated.go b/plugin/modelgen/out/generated.go index 7381a95a5f8..c968c466dc4 100644 --- a/plugin/modelgen/out/generated.go +++ b/plugin/modelgen/out/generated.go @@ -65,6 +65,11 @@ type UnionWithDescription interface { IsUnionWithDescription() } +type X interface { + IsX() + GetId() string +} + type CDImplemented struct { A string `json:"a" database:"CDImplementeda"` B int `json:"b" database:"CDImplementedb"` @@ -202,6 +207,14 @@ type TypeWithDescription struct { func (TypeWithDescription) IsUnionWithDescription() {} +type Xer struct { + Id string `json:"Id" database:"XerId"` + Name string `json:"Name" database:"XerName"` +} + +func (Xer) IsX() {} +func (this Xer) GetId() string { return this.Id } + type FooBarr struct { Name string `json:"name" database:"_Foo_Barrname"` } diff --git a/plugin/modelgen/testdata/schema.graphql b/plugin/modelgen/testdata/schema.graphql index f800ce97f0e..3287f5cbec4 100644 --- a/plugin/modelgen/testdata/schema.graphql +++ b/plugin/modelgen/testdata/schema.graphql @@ -3,6 +3,11 @@ directive @goTag( value: String ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION +directive @goField( + forceResolver: Boolean + name: String +) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | INTERFACE + type Query { thisShoudlntGetGenerated: Boolean } @@ -178,4 +183,13 @@ interface ArrayOfA { type ImplArrayOfA implements ArrayOfA { trickyField: [CDImplemented!]! trickyFieldPointer: [CDImplemented] +} + +interface X { + Id: String! @goField(name: "Id") +} + +type Xer implements X { + Id: String! @goField(name: "Id") + Name: String! } \ No newline at end of file