From ac6ae2f107daf0cfd962a681312d60030ad383da Mon Sep 17 00:00:00 2001 From: Umang Parmar Date: Tue, 29 Nov 2022 00:32:04 +0530 Subject: [PATCH 1/2] consider goField directive for getters generation --- codegen/testserver/followschema/models-gen.go | 2 +- codegen/testserver/singlefile/models-gen.go | 2 +- graphql/executable_schema_mock.go | 41 ++++++++++--------- plugin/modelgen/models.go | 19 +++++++++ plugin/modelgen/out/generated.go | 13 ++++++ plugin/modelgen/testdata/schema.graphql | 14 +++++++ 6 files changed, 70 insertions(+), 21 deletions(-) diff --git a/codegen/testserver/followschema/models-gen.go b/codegen/testserver/followschema/models-gen.go index 0cc3b4a24e6..875941bef29 100644 --- a/codegen/testserver/followschema/models-gen.go +++ b/codegen/testserver/followschema/models-gen.go @@ -220,7 +220,7 @@ type ValidInput struct { Underscore string `json:"_"` } -// These things are all valid, but without care generate invalid go code +// These things are all valid, but without care generate invalid go code type ValidType struct { DifferentCase string `json:"differentCase"` DifferentCaseOld string `json:"different_case"` diff --git a/codegen/testserver/singlefile/models-gen.go b/codegen/testserver/singlefile/models-gen.go index 53afbdf3fd9..5ab72058e3b 100644 --- a/codegen/testserver/singlefile/models-gen.go +++ b/codegen/testserver/singlefile/models-gen.go @@ -220,7 +220,7 @@ type ValidInput struct { Underscore string `json:"_"` } -// These things are all valid, but without care generate invalid go code +// These things are all valid, but without care generate invalid go code type ValidType struct { DifferentCase string `json:"differentCase"` DifferentCaseOld string `json:"different_case"` diff --git a/graphql/executable_schema_mock.go b/graphql/executable_schema_mock.go index 5d7433162fe..5e71cb83040 100644 --- a/graphql/executable_schema_mock.go +++ b/graphql/executable_schema_mock.go @@ -15,25 +15,25 @@ var _ ExecutableSchema = &ExecutableSchemaMock{} // ExecutableSchemaMock is a mock implementation of ExecutableSchema. // -// func TestSomethingThatUsesExecutableSchema(t *testing.T) { +// func TestSomethingThatUsesExecutableSchema(t *testing.T) { // -// // make and configure a mocked ExecutableSchema -// mockedExecutableSchema := &ExecutableSchemaMock{ -// ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) { -// panic("mock out the Complexity method") -// }, -// ExecFunc: func(ctx context.Context) ResponseHandler { -// panic("mock out the Exec method") -// }, -// SchemaFunc: func() *ast.Schema { -// panic("mock out the Schema method") -// }, -// } +// // make and configure a mocked ExecutableSchema +// mockedExecutableSchema := &ExecutableSchemaMock{ +// ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) { +// panic("mock out the Complexity method") +// }, +// ExecFunc: func(ctx context.Context) ResponseHandler { +// panic("mock out the Exec method") +// }, +// SchemaFunc: func() *ast.Schema { +// panic("mock out the Schema method") +// }, +// } // -// // use mockedExecutableSchema in code that requires ExecutableSchema -// // and then make assertions. +// // use mockedExecutableSchema in code that requires ExecutableSchema +// // and then make assertions. // -// } +// } type ExecutableSchemaMock struct { // ComplexityFunc mocks the Complexity method. ComplexityFunc func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) @@ -95,7 +95,8 @@ func (mock *ExecutableSchemaMock) Complexity(typeName string, fieldName string, // ComplexityCalls gets all the calls that were made to Complexity. // Check the length with: -// len(mockedExecutableSchema.ComplexityCalls()) +// +// len(mockedExecutableSchema.ComplexityCalls()) func (mock *ExecutableSchemaMock) ComplexityCalls() []struct { TypeName string FieldName string @@ -132,7 +133,8 @@ func (mock *ExecutableSchemaMock) Exec(ctx context.Context) ResponseHandler { // ExecCalls gets all the calls that were made to Exec. // Check the length with: -// len(mockedExecutableSchema.ExecCalls()) +// +// len(mockedExecutableSchema.ExecCalls()) func (mock *ExecutableSchemaMock) ExecCalls() []struct { Ctx context.Context } { @@ -160,7 +162,8 @@ func (mock *ExecutableSchemaMock) Schema() *ast.Schema { // SchemaCalls gets all the calls that were made to Schema. // Check the length with: -// len(mockedExecutableSchema.SchemaCalls()) +// +// len(mockedExecutableSchema.SchemaCalls()) func (mock *ExecutableSchemaMock) SchemaCalls() []struct { } { var calls []struct { 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 From de30b2070072ccfff0db4bb443a24551286264a7 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Sat, 3 Dec 2022 15:09:26 -0500 Subject: [PATCH 2/2] Re-generate to pass linting Signed-off-by: Steve Coffman --- codegen/testserver/followschema/models-gen.go | 2 +- codegen/testserver/singlefile/models-gen.go | 2 +- graphql/executable_schema_mock.go | 41 +++++++++---------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/codegen/testserver/followschema/models-gen.go b/codegen/testserver/followschema/models-gen.go index 875941bef29..0cc3b4a24e6 100644 --- a/codegen/testserver/followschema/models-gen.go +++ b/codegen/testserver/followschema/models-gen.go @@ -220,7 +220,7 @@ type ValidInput struct { Underscore string `json:"_"` } -// These things are all valid, but without care generate invalid go code +// These things are all valid, but without care generate invalid go code type ValidType struct { DifferentCase string `json:"differentCase"` DifferentCaseOld string `json:"different_case"` diff --git a/codegen/testserver/singlefile/models-gen.go b/codegen/testserver/singlefile/models-gen.go index 5ab72058e3b..53afbdf3fd9 100644 --- a/codegen/testserver/singlefile/models-gen.go +++ b/codegen/testserver/singlefile/models-gen.go @@ -220,7 +220,7 @@ type ValidInput struct { Underscore string `json:"_"` } -// These things are all valid, but without care generate invalid go code +// These things are all valid, but without care generate invalid go code type ValidType struct { DifferentCase string `json:"differentCase"` DifferentCaseOld string `json:"different_case"` diff --git a/graphql/executable_schema_mock.go b/graphql/executable_schema_mock.go index 5e71cb83040..5d7433162fe 100644 --- a/graphql/executable_schema_mock.go +++ b/graphql/executable_schema_mock.go @@ -15,25 +15,25 @@ var _ ExecutableSchema = &ExecutableSchemaMock{} // ExecutableSchemaMock is a mock implementation of ExecutableSchema. // -// func TestSomethingThatUsesExecutableSchema(t *testing.T) { +// func TestSomethingThatUsesExecutableSchema(t *testing.T) { // -// // make and configure a mocked ExecutableSchema -// mockedExecutableSchema := &ExecutableSchemaMock{ -// ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) { -// panic("mock out the Complexity method") -// }, -// ExecFunc: func(ctx context.Context) ResponseHandler { -// panic("mock out the Exec method") -// }, -// SchemaFunc: func() *ast.Schema { -// panic("mock out the Schema method") -// }, -// } +// // make and configure a mocked ExecutableSchema +// mockedExecutableSchema := &ExecutableSchemaMock{ +// ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) { +// panic("mock out the Complexity method") +// }, +// ExecFunc: func(ctx context.Context) ResponseHandler { +// panic("mock out the Exec method") +// }, +// SchemaFunc: func() *ast.Schema { +// panic("mock out the Schema method") +// }, +// } // -// // use mockedExecutableSchema in code that requires ExecutableSchema -// // and then make assertions. +// // use mockedExecutableSchema in code that requires ExecutableSchema +// // and then make assertions. // -// } +// } type ExecutableSchemaMock struct { // ComplexityFunc mocks the Complexity method. ComplexityFunc func(typeName string, fieldName string, childComplexity int, args map[string]interface{}) (int, bool) @@ -95,8 +95,7 @@ func (mock *ExecutableSchemaMock) Complexity(typeName string, fieldName string, // ComplexityCalls gets all the calls that were made to Complexity. // Check the length with: -// -// len(mockedExecutableSchema.ComplexityCalls()) +// len(mockedExecutableSchema.ComplexityCalls()) func (mock *ExecutableSchemaMock) ComplexityCalls() []struct { TypeName string FieldName string @@ -133,8 +132,7 @@ func (mock *ExecutableSchemaMock) Exec(ctx context.Context) ResponseHandler { // ExecCalls gets all the calls that were made to Exec. // Check the length with: -// -// len(mockedExecutableSchema.ExecCalls()) +// len(mockedExecutableSchema.ExecCalls()) func (mock *ExecutableSchemaMock) ExecCalls() []struct { Ctx context.Context } { @@ -162,8 +160,7 @@ func (mock *ExecutableSchemaMock) Schema() *ast.Schema { // SchemaCalls gets all the calls that were made to Schema. // Check the length with: -// -// len(mockedExecutableSchema.SchemaCalls()) +// len(mockedExecutableSchema.SchemaCalls()) func (mock *ExecutableSchemaMock) SchemaCalls() []struct { } { var calls []struct {