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

Use %q to quote struct tag values #83

Merged
merged 1 commit into from Nov 12, 2021
Merged

Conversation

bmoylan
Copy link
Contributor

@bmoylan bmoylan commented Nov 11, 2021

Previously, it was possible to generate invalid struct tags because not all special characters inside the value are correctly quoted/escaped. Using %q delegates to strconv.Quote and ensures we have a valid go string.

// Output:
// type foo struct {
// A string `json:"a"`
// B int `bar:"the value of\nthe \"bar\" tag" json:"b"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we would get B int "bar:\"the value of\nthe\"bar\" tag\" json:\"b\""

Notice that the quotes around bar are escaped at the same level as the quotes for the tag value, meaning the reflect.StructTag.Get parser will recognize the first quote after opening as the end of the value.

@dave
Copy link
Owner

dave commented Nov 12, 2021

Looks good to me.

@dave dave merged commit 45cc0b7 into dave:master Nov 12, 2021
@bmoylan bmoylan deleted the bm/struct-tags branch November 12, 2021 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants