Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to override existing tags #2536 #2537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 19 additions & 14 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,25 +419,30 @@ func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Fie
return f, nil
}

func removeDuplicateTags(t string) string {
processed := make(map[string]bool)
tt := strings.Split(t, " ")
returnTags := ""

for _, ti := range tt {
kv := strings.Split(ti, ":")
if len(kv) == 0 || processed[kv[0]] {
continue
func removeDuplicateTags(_tags string) string {

tags := strings.Split(_tags, " ")
uniqueTags := []string{}

if len(tags) > 0 {
tagMap := map[string]string{}

for _, tag := range tags {
tagKV := strings.Split(tag, ":")
if len(tagKV) > 0 {
tagMap[tagKV[0]] = tag
}
}

processed[kv[0]] = true
if len(returnTags) > 0 {
returnTags += " "
for _, tag := range tagMap {
uniqueTags = append(uniqueTags, tag)
}
returnTags += kv[0] + ":" + kv[1]
sort.Strings(uniqueTags) // to assure sorting order

return strings.Join(uniqueTags, " ")
}

return returnTags
return _tags
}

// GoFieldHook applies the goField directive to the generated Field f.
Expand Down
34 changes: 27 additions & 7 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,29 @@ func TestModelGeneration(t *testing.T) {

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

for _, tag := range expectedTags {
require.True(t, strings.Contains(fileText, tag), tag)
}
})

t.Run("tags can be overridden", func(t *testing.T) {
file, err := os.ReadFile("./out/generated.go")
require.NoError(t, err)

fileText := string(file)

expectedTags := []string{
`json:"name0" someOtherTag:"someOtherTagValue" database:"GoTagHookDuplicationTestname0"`,
`json:"name1" database:"GoTagHookDuplicationTestname1"`,
`json:"name2" database:"GoTagHookDuplicationTestname2"`,
`json:"name3" database:"GoTagHookDuplicationTestname3"`,
`json:"name4" someOtherTag:"someOtherTagValue" database:"GoTagHookDuplicationTestname4"`,
`json:"name5" someOtherTag:"someOtherTagValue2" database:"GoTagHookDuplicationTestname5"`,
}

for _, tag := range expectedTags {
Expand Down Expand Up @@ -372,28 +392,28 @@ func TestRemoveDuplicate(t *testing.T) {
args: args{
t: "json:\"name\" json:\"name2\"",
},
want: "json:\"name\"",
want: "json:\"name2\"",
},
{
name: "Duplicate Test with 3",
args: args{
t: "json:\"name\" json:\"name2\" json:\"name3\"",
},
want: "json:\"name\"",
want: "json:\"name3\"",
},
{
name: "Duplicate Test with 3 and 1 unrelated",
args: args{
t: "json:\"name\" something:\"name2\" json:\"name3\"",
},
want: "json:\"name\" something:\"name2\"",
want: "json:\"name3\" something:\"name2\"",
},
{
name: "Duplicate Test with 3 and 2 unrelated",
args: args{
t: "something:\"name1\" json:\"name\" something:\"name2\" json:\"name3\"",
},
want: "something:\"name1\" json:\"name\"",
want: "json:\"name3\" something:\"name2\"",
},
}
for _, tt := range tests {
Expand Down
15 changes: 12 additions & 3 deletions plugin/modelgen/out/generated.go

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

15 changes: 12 additions & 3 deletions plugin/modelgen/out_struct_pointers/generated.go

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

8 changes: 8 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ type FieldMutationHook {
enum: ExistingEnum @goTag(key: "yetAnotherTag", value: "12")
noVal: String @goTag(key: "yaml") @goTag(key : "repeated", value: "true")
repeated: String @goTag(key: "someTag", value: "value") @goTag(key : "repeated", value: "true")
}

type GoTagHookDuplicationTest {
name0: String @goTag(key: "someOtherTag", value: "someOtherTagValue")
name1: String @goTag(key: "json", value: "Name1")
name2: String @goTag(key: "json", value: "name2") @goTag(key: "json", value: "Name2")
name3: String @goTag(key: "json", value: "name2") @goTag(key: "json", value: "name3") @goTag(key: "json", value: "Name3")
name4: String @goTag(key: "json", value: "name2") @goTag(key: "someOtherTag", value: "someOtherTagValue") @goTag(key: "json", value: "Name4")
name5: String @goTag(key: "json", value: "name2") @goTag(key: "someOtherTag", value: "someOtherTagValue") @goTag(key: "json", value: "Name4") @goTag(key: "someOtherTag", value: "someOtherTagValue2")
}

enum ExistingEnum {
Expand Down