diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 851c4b53db5..c18548ed7ea 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -388,8 +388,9 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition) return fields, nil } -// GoTagFieldHook prepend the goTag directive to the generated Field f. When applying the Tag to the field, the field -// name is used when no value argument is present. +// 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. func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) { args := make([]string, 0) for _, goTag := range fd.Directives.ForNames("goTag") { @@ -412,15 +413,12 @@ func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Fie } if len(args) > 0 { - f.Tag = strings.Join(args, " ") + " " + f.Tag + f.Tag = removeDuplicateTags(strings.Join(args, " ") + " " + f.Tag) } - f.Tag = removeDuplicateTags(f.Tag) - return f, nil } -// removeDuplicateTags removes duplicate tags func removeDuplicateTags(t string) string { processed := make(map[string]bool) tt := strings.Split(t, " ") @@ -434,9 +432,9 @@ func removeDuplicateTags(t string) string { processed[kv[0]] = true if len(returnTags) > 0 { - returnTags = returnTags + " " + returnTags += " " } - returnTags = returnTags + kv[0] + ":" + kv[1] + returnTags += kv[0] + ":" + kv[1] } return returnTags diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index 8058d5f5985..bb08a379db9 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -82,14 +82,14 @@ func TestModelGeneration(t *testing.T) { fileText := string(file) expectedTags := []string{ - `json:"name" anotherTag:"tag"`, - `json:"enum" yetAnotherTag:"12"`, - `json:"noVal" yaml:"noVal"`, - `json:"repeated" someTag:"value" repeated:"true"`, + `anotherTag:"tag" json:"name"`, + `yetAnotherTag:"12" json:"enum"`, + `yaml:"noVal" repeated:"true" json:"noVal"`, + `someTag:"value" repeated:"true" json:"repeated"`, } for _, tag := range expectedTags { - require.True(t, strings.Contains(fileText, tag)) + require.True(t, strings.Contains(fileText, tag), tag) } }) diff --git a/plugin/modelgen/out/generated.go b/plugin/modelgen/out/generated.go index c968c466dc4..32d1c0a1ca1 100644 --- a/plugin/modelgen/out/generated.go +++ b/plugin/modelgen/out/generated.go @@ -106,10 +106,10 @@ type CyclicalB struct { } type FieldMutationHook struct { - Name *string `json:"name" anotherTag:"tag" database:"FieldMutationHookname"` - Enum *ExistingEnum `json:"enum" yetAnotherTag:"12" database:"FieldMutationHookenum"` - NoVal *string `json:"noVal" yaml:"noVal" repeated:"true" database:"FieldMutationHooknoVal"` - Repeated *string `json:"repeated" someTag:"value" repeated:"true" database:"FieldMutationHookrepeated"` + Name *string `anotherTag:"tag" json:"name" database:"FieldMutationHookname"` + Enum *ExistingEnum `yetAnotherTag:"12" json:"enum" database:"FieldMutationHookenum"` + NoVal *string `yaml:"noVal" repeated:"true" json:"noVal" database:"FieldMutationHooknoVal"` + Repeated *string `someTag:"value" repeated:"true" json:"repeated" database:"FieldMutationHookrepeated"` } type ImplArrayOfA struct { diff --git a/plugin/modelgen/out_struct_pointers/generated.go b/plugin/modelgen/out_struct_pointers/generated.go index 955049d7b91..6d429db1e6f 100644 --- a/plugin/modelgen/out_struct_pointers/generated.go +++ b/plugin/modelgen/out_struct_pointers/generated.go @@ -88,10 +88,10 @@ type CyclicalB struct { } type FieldMutationHook struct { - Name *string `json:"name" anotherTag:"tag" database:"FieldMutationHookname"` + Name *string `anotherTag:"tag" json:"name" database:"FieldMutationHookname"` Enum *ExistingEnum `yetAnotherTag:"12" json:"enum" database:"FieldMutationHookenum"` - NoVal *string `repeated:"true" json:"noVal" yaml:"noVal" database:"FieldMutationHooknoVal"` - Repeated *string `json:"repeated" someTag:"value" repeated:"true" database:"FieldMutationHookrepeated"` + NoVal *string `yaml:"noVal" repeated:"true" json:"noVal" database:"FieldMutationHooknoVal"` + Repeated *string `someTag:"value" repeated:"true" json:"repeated" database:"FieldMutationHookrepeated"` } type ImplArrayOfA struct {