-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
RemoveDuplicateTags func introduced to fix #2514 #2531
Conversation
Thanks for working on this! The first run through only some of the matrix of tests failed (e.g. ubuntu, Go 1.18):
I wonder if this means that either in that particular test, or perhaps in your code, it somehow related to ranging through a map in an order that is not deterministic. I'm on my phone right now, so I can't look at it in more detail, but just food for thought. |
Also, your code appears to prefer the last value of duplicate tags, rather than the first. Update: Ah, I see. In
Your original code preserved the first appearing tag key order, but gave the reverse precedence for duplicate tag values (first occurring duplicate would have last occurring value). I felt like this was confusing. If someone was using a https://go.dev/play/p/GxXnDGI9c2g Another alternative would be to change
to prepend instead of append:
This would allow the code to be further simplified (since we don't need to walk backwards through the struct tag key:value pairs. |
// removeDuplicateTags removes duplicate tags | ||
func removeDuplicateTags(t string) string { | ||
|
||
tt := strings.Split(t, " ") | ||
|
||
if len(tt) > 0 { | ||
tagMap := map[string]string{} | ||
returnTags := "" | ||
|
||
for _, ti := range tt { | ||
kv := strings.Split(ti, ":") | ||
if len(kv) > 0 { | ||
tagMap[kv[0]] = kv[1] | ||
} | ||
} | ||
|
||
for k, v := range tagMap { | ||
if len(returnTags) > 0 { | ||
returnTags += " " | ||
} | ||
returnTags += k + ":" + v | ||
} | ||
return returnTags | ||
|
||
} | ||
|
||
return t | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// removeDuplicateTags removes duplicate tags | |
func removeDuplicateTags(t string) string { | |
tt := strings.Split(t, " ") | |
if len(tt) > 0 { | |
tagMap := map[string]string{} | |
returnTags := "" | |
for _, ti := range tt { | |
kv := strings.Split(ti, ":") | |
if len(kv) > 0 { | |
tagMap[kv[0]] = kv[1] | |
} | |
} | |
for k, v := range tagMap { | |
if len(returnTags) > 0 { | |
returnTags += " " | |
} | |
returnTags += k + ":" + v | |
} | |
return returnTags | |
} | |
return t | |
} | |
// removeDuplicateTags removes duplicate tags | |
func removeDuplicateTags(t string) string { | |
processed := make(map[string]bool) | |
tt := strings.Split(t, " ") | |
returnTags := "" | |
// iterate backwards through tags so appended goTag directives are prioritized | |
for i := len(tt) - 1; i >= 0; i-- { | |
ti := tt[i] | |
kv := strings.Split(ti, ":") | |
if len(kv) == 0 || processed[kv[0]] { | |
continue | |
} | |
processed[kv[0]] = true | |
if len(returnTags) > 0 { | |
returnTags = " " + returnTags | |
} | |
returnTags = kv[0] + ":" + kv[1] + returnTags | |
} | |
return returnTags | |
} |
args: args{ | ||
t: "json:\"name\" something:\"name2\" json:\"name3\"", | ||
}, | ||
want: "json:\"name3\" something:\"name2\"", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
want: "json:\"name3\" something:\"name2\"", | |
want: "something:\"name2\" json:\"name3\"", |
Hey, I hope it's ok, but I merged the tweaks I made in #2533 as they seemed a little easier to read. I would be happy to accept a PR if you feel like yours is better. Thanks! |
Hey @StevenACoffman Thank you for taking a look at it. Gqlgen can be used as just a model generator and in that case we may not have a full control over backend. |
That makes sense! Hit me up with a PR if you feel like it should change to the behavior you originally proposed. |
Sure thing. Let me report issue and fork again |
As described in #2514, and without this PR,
GoTagFieldHook
just appends new tags, so@goTag
will not override an existing tag.Ideally it should override existing tag, since the sole purpose of the
@goTag
directive is to give control to user.In this PR, the
removeDuplicateTags
helper func is introduced to fix #2514I have: