Skip to content

Commit 849731b

Browse files
author
Adrian Lungu
committed
fix missing return in model plugin getObject;
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
1 parent 384d4a0 commit 849731b

File tree

6 files changed

+156
-12
lines changed

6 files changed

+156
-12
lines changed

plugin/modelgen/models.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
117117
return err
118118
}
119119

120+
if !cfg.OmitEmbeddedStructs {
121+
ob, err := m.getObject(cfg, schemaType, b)
122+
if err != nil {
123+
return err
124+
}
125+
if ob == nil {
126+
continue
127+
}
128+
129+
ob.Name = fmt.Sprintf("%s%s", cfg.EmbeddedStructsPrefix, ob.Name)
130+
b.Models = append(b.Models, ob)
131+
}
132+
120133
b.Interfaces = append(b.Interfaces, it)
121134
}
122135
}
@@ -131,6 +144,9 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
131144
if err != nil {
132145
return err
133146
}
147+
if it == nil {
148+
continue
149+
}
134150

135151
b.Models = append(b.Models, it)
136152
case ast.Enum:
@@ -739,17 +755,6 @@ func (m *Plugin) getInterface(cfg *config.Config, schemaType *ast.Definition, b
739755
}
740756
}
741757

742-
if !cfg.OmitEmbeddedStructs {
743-
it := &Object{
744-
Description: schemaType.Description,
745-
// To not conflict with the interface name, we prefix the struct name with "Base"
746-
Name: fmt.Sprintf("%s%s", cfg.EmbeddedStructsPrefix, schemaType.Name),
747-
Fields: fields,
748-
}
749-
750-
b.Models = append(b.Models, it)
751-
}
752-
753758
it := &Interface{
754759
Description: schemaType.Description,
755760
Name: schemaType.Name,
@@ -774,6 +779,8 @@ func (m *Plugin) getObject(cfg *config.Config, schemaType *ast.Definition, b *Mo
774779
Name: schemaType.Name,
775780
}, nil
776781
}
782+
783+
return nil, nil
777784
}
778785

779786
fields, err := m.generateFields(cfg, schemaType, b)

plugin/modelgen/models.gotpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
{{- with .Description }}
3838
{{.|prefixLines "// "}}
3939
{{- end}}
40-
{{ $field.GoName }} {{$field.Type | ref}} `{{$field.Tag}}`
40+
{{ $field.GoName }} {{$field.Type | ref}} {{if $field.Tag}}`{{end}}{{$field.Tag}}{{if $field.Tag}}`{{end}}
4141
{{- end }}
4242
}
4343

plugin/modelgen/models_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,32 @@ func TestModelGenerationOmitRootModels(t *testing.T) {
315315
require.NotContains(t, string(generated), "type Subscription struct")
316316
}
317317

318+
func TestModelGenerationDontOmitEmbeddedStructs(t *testing.T) {
319+
cfg, err := config.LoadConfig("testdata/gqlgen_embedded_structs_models.yml")
320+
require.NoError(t, err)
321+
require.NoError(t, cfg.Init())
322+
p := Plugin{
323+
FieldHook: DefaultFieldMutateHook,
324+
}
325+
require.NoError(t, p.MutateConfig(cfg))
326+
require.NoError(t, goBuild(t, "./out_embedded_structs_models/"))
327+
generated, err := os.ReadFile("./out_embedded_structs_models/generated_embedded_structs_models.go")
328+
require.NoError(t, err)
329+
require.Contains(t, string(generated), "type BaseElement")
330+
331+
carbonStr := getStringInBetween(string(generated), "type Carbon struct {", "}")
332+
require.NotEqual(t, carbonStr, "")
333+
require.Contains(t, carbonStr, "BaseElement")
334+
335+
magnesiumStr := getStringInBetween(string(generated), "type Magnesium struct {", "}")
336+
require.NotEqual(t, magnesiumStr, "")
337+
require.Contains(t, magnesiumStr, "BaseElement")
338+
339+
potassiumStr := getStringInBetween(string(generated), "type Potassium struct {", "}")
340+
require.NotEqual(t, potassiumStr, "")
341+
require.Contains(t, potassiumStr, "BaseElement")
342+
}
343+
318344
func TestModelGenerationOmitResolverFields(t *testing.T) {
319345
cfg, err := config.LoadConfig("testdata/gqlgen_omit_resolver_fields.yml")
320346
require.NoError(t, err)
@@ -699,3 +725,18 @@ func TestCustomTemplate(t *testing.T) {
699725
}
700726
require.NoError(t, p.MutateConfig(cfg))
701727
}
728+
729+
func getStringInBetween(str string, start string, end string) string {
730+
startIndex := strings.Index(str, start)
731+
if startIndex == -1 {
732+
return ""
733+
}
734+
735+
newStr := str[startIndex+len(start):]
736+
e := strings.Index(newStr, end)
737+
if e == -1 {
738+
return ""
739+
}
740+
741+
return newStr[:e]
742+
}

plugin/modelgen/out_embedded_structs_models/generated_embedded_structs_models.go

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
schema:
2+
- "testdata/schema_embedded_structs_models.graphql"
3+
4+
model:
5+
filename: out_embedded_structs_models/generated_embedded_structs_models.go
6+
7+
omit_embedded_structs: false
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
interface Node {
2+
id: ID!
3+
}
4+
5+
interface Element implements Node {
6+
id: ID!
7+
name: String!
8+
}
9+
10+
type Magnesium implements Element & Node {
11+
id: ID!
12+
name: String!
13+
types: Int!
14+
}
15+
16+
type Potassium implements Element & Node {
17+
id: ID!
18+
name: String!
19+
price: Float!
20+
}
21+
22+
type Carbon implements Element & Node {
23+
id: ID!
24+
name: String!
25+
footprint: String!
26+
}

0 commit comments

Comments
 (0)