Skip to content

Commit

Permalink
Add omitempty to struct tags for nullable types (#2436)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <[email protected]>
  • Loading branch information
StevenACoffman authored Mar 9, 2023
1 parent acbae6f commit 0bbc7f8
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 106 deletions.
2 changes: 1 addition & 1 deletion _examples/scalars/model/generated.go

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

8 changes: 4 additions & 4 deletions _examples/selection/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 _examples/starwars/models/generated.go

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

2 changes: 1 addition & 1 deletion _examples/todo/models_gen.go

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

38 changes: 19 additions & 19 deletions codegen/testserver/followschema/models-gen.go

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

38 changes: 19 additions & 19 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: 6 additions & 6 deletions integration/models-go/generated.go

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

4 changes: 2 additions & 2 deletions plugin/federation/testdata/entityresolver/generated/models.go

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

9 changes: 8 additions & 1 deletion plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition)
GoName: name,
Type: typ,
Description: field.Description,
Tag: `json:"` + field.Name + `"`,
Tag: getStructTagFromField(field),
}

if m.FieldHook != nil {
Expand All @@ -388,6 +388,13 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition)
return fields, nil
}

func getStructTagFromField(field *ast.FieldDefinition) string {
if !field.Type.NonNull {
return `json:"` + field.Name + `,omitempty"`
}
return `json:"` + field.Name + `"`
}

// GoTagFieldHook prepends the goTag directive to the generated Field f.
// When applying the Tag to the field, the field
// name is used if no value argument is present.
Expand Down
16 changes: 8 additions & 8 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func TestModelGeneration(t *testing.T) {

expectedTags := []string{
`json:"missing2" database:"MissingTypeNotNullmissing2"`,
`json:"name" database:"MissingInputname"`,
`json:"missing2" database:"MissingTypeNullablemissing2"`,
`json:"name" database:"TypeWithDescriptionname"`,
`json:"name,omitempty" database:"MissingInputname"`,
`json:"missing2,omitempty" database:"MissingTypeNullablemissing2"`,
`json:"name,omitempty" database:"TypeWithDescriptionname"`,
}

for _, tag := range expectedTags {
require.True(t, strings.Contains(fileText, tag))
require.True(t, strings.Contains(fileText, tag), "\nexpected:\n"+tag+"\ngot\n"+fileText)
}
})

Expand All @@ -82,10 +82,10 @@ func TestModelGeneration(t *testing.T) {
fileText := string(file)

expectedTags := []string{
`json:"name" anotherTag:"tag"`,
`json:"enum" yetAnotherTag:"12"`,
`json:"noVal" yaml:"noVal" repeated:"true"`,
`json:"repeated" someTag:"value" repeated:"true"`,
`json:"name,omitempty" anotherTag:"tag"`,
`json:"enum,omitempty" yetAnotherTag:"12"`,
`json:"noVal,omitempty" yaml:"noVal" repeated:"true"`,
`json:"repeated,omitempty" someTag:"value" repeated:"true"`,
}

for _, tag := range expectedTags {
Expand Down
Loading

0 comments on commit 0bbc7f8

Please sign in to comment.