Skip to content

Commit

Permalink
fix missing return in model plugin getObject;
Browse files Browse the repository at this point in the history
refactor to use `getObject` to create the base struct to be used in embedding;
add test for embedded structs;
update models.gotpl to not create the `` for tags when there are no tags
  • Loading branch information
Adrian Lungu committed Sep 5, 2024
1 parent 384d4a0 commit 849731b
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 12 deletions.
29 changes: 18 additions & 11 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
return err
}

if !cfg.OmitEmbeddedStructs {
ob, err := m.getObject(cfg, schemaType, b)
if err != nil {
return err
}
if ob == nil {
continue
}

ob.Name = fmt.Sprintf("%s%s", cfg.EmbeddedStructsPrefix, ob.Name)
b.Models = append(b.Models, ob)
}

b.Interfaces = append(b.Interfaces, it)
}
}
Expand All @@ -131,6 +144,9 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
if err != nil {
return err
}
if it == nil {
continue
}

b.Models = append(b.Models, it)
case ast.Enum:
Expand Down Expand Up @@ -739,17 +755,6 @@ func (m *Plugin) getInterface(cfg *config.Config, schemaType *ast.Definition, b
}
}

if !cfg.OmitEmbeddedStructs {
it := &Object{
Description: schemaType.Description,
// To not conflict with the interface name, we prefix the struct name with "Base"
Name: fmt.Sprintf("%s%s", cfg.EmbeddedStructsPrefix, schemaType.Name),
Fields: fields,
}

b.Models = append(b.Models, it)
}

it := &Interface{
Description: schemaType.Description,
Name: schemaType.Name,
Expand All @@ -774,6 +779,8 @@ func (m *Plugin) getObject(cfg *config.Config, schemaType *ast.Definition, b *Mo
Name: schemaType.Name,
}, nil
}

return nil, nil
}

fields, err := m.generateFields(cfg, schemaType, b)
Expand Down
2 changes: 1 addition & 1 deletion plugin/modelgen/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{{- with .Description }}
{{.|prefixLines "// "}}
{{- end}}
{{ $field.GoName }} {{$field.Type | ref}} `{{$field.Tag}}`
{{ $field.GoName }} {{$field.Type | ref}} {{if $field.Tag}}`{{end}}{{$field.Tag}}{{if $field.Tag}}`{{end}}
{{- end }}
}

Expand Down
41 changes: 41 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,32 @@ func TestModelGenerationOmitRootModels(t *testing.T) {
require.NotContains(t, string(generated), "type Subscription struct")
}

func TestModelGenerationDontOmitEmbeddedStructs(t *testing.T) {
cfg, err := config.LoadConfig("testdata/gqlgen_embedded_structs_models.yml")
require.NoError(t, err)
require.NoError(t, cfg.Init())
p := Plugin{
FieldHook: DefaultFieldMutateHook,
}
require.NoError(t, p.MutateConfig(cfg))
require.NoError(t, goBuild(t, "./out_embedded_structs_models/"))
generated, err := os.ReadFile("./out_embedded_structs_models/generated_embedded_structs_models.go")
require.NoError(t, err)
require.Contains(t, string(generated), "type BaseElement")

carbonStr := getStringInBetween(string(generated), "type Carbon struct {", "}")
require.NotEqual(t, carbonStr, "")

Check failure on line 332 in plugin/modelgen/models_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

expected-actual: need to reverse actual and expected values (testifylint)
require.Contains(t, carbonStr, "BaseElement")

magnesiumStr := getStringInBetween(string(generated), "type Magnesium struct {", "}")
require.NotEqual(t, magnesiumStr, "")

Check failure on line 336 in plugin/modelgen/models_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

expected-actual: need to reverse actual and expected values (testifylint)
require.Contains(t, magnesiumStr, "BaseElement")

potassiumStr := getStringInBetween(string(generated), "type Potassium struct {", "}")
require.NotEqual(t, potassiumStr, "")

Check failure on line 340 in plugin/modelgen/models_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

expected-actual: need to reverse actual and expected values (testifylint)
require.Contains(t, potassiumStr, "BaseElement")
}

func TestModelGenerationOmitResolverFields(t *testing.T) {
cfg, err := config.LoadConfig("testdata/gqlgen_omit_resolver_fields.yml")
require.NoError(t, err)
Expand Down Expand Up @@ -699,3 +725,18 @@ func TestCustomTemplate(t *testing.T) {
}
require.NoError(t, p.MutateConfig(cfg))
}

func getStringInBetween(str string, start string, end string) string {

Check failure on line 729 in plugin/modelgen/models_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

paramTypeCombine: func(str string, start string, end string) string could be replaced with func(str, start, end string) string (gocritic)
startIndex := strings.Index(str, start)
if startIndex == -1 {
return ""
}

newStr := str[startIndex+len(start):]
e := strings.Index(newStr, end)
if e == -1 {
return ""
}

return newStr[:e]
}

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

7 changes: 7 additions & 0 deletions plugin/modelgen/testdata/gqlgen_embedded_structs_models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
schema:
- "testdata/schema_embedded_structs_models.graphql"

model:
filename: out_embedded_structs_models/generated_embedded_structs_models.go

omit_embedded_structs: false
26 changes: 26 additions & 0 deletions plugin/modelgen/testdata/schema_embedded_structs_models.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface Node {
id: ID!
}

interface Element implements Node {
id: ID!
name: String!
}

type Magnesium implements Element & Node {
id: ID!
name: String!
types: Int!
}

type Potassium implements Element & Node {
id: ID!
name: String!
price: Float!
}

type Carbon implements Element & Node {
id: ID!
name: String!
footprint: String!
}

0 comments on commit 849731b

Please sign in to comment.