From 065aea3efa27f0662eb2f62ef2dd131e153009ba Mon Sep 17 00:00:00 2001 From: Mg Pig Date: Sat, 26 Aug 2023 05:52:15 +0800 Subject: [PATCH] Fix gqlgen truncates tag value with colon (#2759) --- plugin/modelgen/models.go | 10 ++++++---- plugin/modelgen/models_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index bbf599d6edb..4383d76c92a 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -568,17 +568,19 @@ func removeDuplicateTags(t string) string { continue } - processed[kv[0]] = true + key := kv[0] + value := strings.Join(kv[1:], ":") + processed[key] = true if len(returnTags) > 0 { returnTags = " " + returnTags } - isContained := containsInvalidSpace(kv[1]) + isContained := containsInvalidSpace(value) if isContained { - panic(fmt.Errorf("tag value should not contain any leading or trailing spaces: %s", kv[1])) + panic(fmt.Errorf("tag value should not contain any leading or trailing spaces: %s", value)) } - returnTags = kv[0] + ":" + kv[1] + returnTags + returnTags = key + ":" + value + returnTags } return returnTags diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index fa15fa010ce..8607615772e 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -534,6 +534,22 @@ func TestRemoveDuplicate(t *testing.T) { want: "gorm:\"unique;not null\" json:\"name,name2\"", wantPanic: false, }, + { + name: "Test gorm tag with colon", + args: args{ + t: "gorm:\"type:varchar(63);unique_index\"", + }, + want: "gorm:\"type:varchar(63);unique_index\"", + wantPanic: false, + }, + { + name: "Test mix use of gorm and duplicate json tags with colon", + args: args{ + t: "json:\"name0\" gorm:\"type:varchar(63);unique_index\" json:\"name,name2\"", + }, + want: "gorm:\"type:varchar(63);unique_index\" json:\"name,name2\"", + wantPanic: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {