Skip to content

Commit

Permalink
Merge pull request #83 from bmoylan/bm/struct-tags
Browse files Browse the repository at this point in the history
Use %q to quote struct tag values
  • Loading branch information
dave committed Nov 12, 2021
2 parents 2abe0ee + 50f4f83 commit 45cc0b7
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
@@ -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
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 45cc0b7

Please sign in to comment.