Skip to content

Commit

Permalink
Use %q to quote struct tag values
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoylan committed Nov 11, 2021
1 parent 2abe0ee commit 50f4f83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions jen/examples_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package jen_test

import (
"fmt"

"bytes"
"fmt"

. "github.com/dave/jennifer/jen"
)
Expand Down Expand Up @@ -1295,6 +1294,19 @@ func ExampleTag() {
// }
}

func ExampleTag_withQuotesAndNewline() {
c := Type().Id("foo").Struct(
Id("A").String().Tag(map[string]string{"json": "a"}),
Id("B").Int().Tag(map[string]string{"json": "b", "bar": "the value of\nthe\"bar\" tag"}),
)
fmt.Printf("%#v", c)
// Output:
// type foo struct {
// A string `json:"a"`
// B int `bar:"the value of\nthe \"bar\" tag" json:"b"`
// }
}

func ExampleNull_and_nil() {
c := Func().Id("foo").Params(
nil,
Expand Down
2 changes: 1 addition & 1 deletion jen/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (t tag) render(f *File, w io.Writer, s *Statement) error {
if len(str) > 0 {
str += " "
}
str += fmt.Sprintf(`%s:"%s"`, k, v)
str += fmt.Sprintf(`%s:%q`, k, v)
}

if strconv.CanBackquote(str) {
Expand Down

0 comments on commit 50f4f83

Please sign in to comment.