Skip to content

Commit

Permalink
Change how JSON UUIDs and byte arrays are exported
Browse files Browse the repository at this point in the history
  • Loading branch information
blt04 committed Sep 24, 2024
1 parent 9a5be37 commit 5197454
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/runZeroInc/mustache/v2
go 1.23

require (
github.com/google/uuid v1.6.0
github.com/spf13/cobra v1.0.0
gopkg.in/yaml.v2 v2.3.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
33 changes: 32 additions & 1 deletion mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,40 @@ func (tmpl *Template) renderElement(element any, contextChain []any, buf io.Writ
s := fmt.Sprint(val.Interface())
switch tmpl.outputMode {
case EscapeJSON:
// Whether to use JSON's marshalling (true) or our JSON escaping (false)
useMarshal := false

var kind reflect.Kind
typeof := reflect.TypeOf(val.Interface())
if typeof != nil {
kind = typeof.Kind()
}

// Output arrays and objects in JSON format, if in JSON mode
kind := reflect.TypeOf(val.Interface()).Kind()
if kind == reflect.Slice || kind == reflect.Array || kind == reflect.Map {
useMarshal = true
}

//
// Special case overrides
//
if typeof != nil {
switch typeof.String() {
case "uuid.UUID":
// JSON's implementation encloses UUID in double-quotes,
// so use ours instead
useMarshal = false
case "[]uint8":
// JSON's implementation encloses the base64 encoded value in double quotes,
// so use ours instead
if ba, ok := val.Interface().([]byte); ok {
s = string(ba[:])
useMarshal = false
}
}
}

if useMarshal {
marshalledJson, err := json.Marshal(val.Interface())
if err != nil {
return err
Expand Down
18 changes: 18 additions & 0 deletions mustache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path"
"strings"
"testing"

"github.com/google/uuid"
)

type Test struct {
Expand Down Expand Up @@ -452,6 +454,8 @@ func TestRenderJSON(t *testing.T) {
Name string
}

testUuid := uuid.New()

tests := []struct {
Template string
Data map[string]any
Expand Down Expand Up @@ -505,6 +509,20 @@ func TestRenderJSON(t *testing.T) {
},
Result: `[4,5,6]`,
},
{
Template: `{{uuid}}`,
Data: map[string]any{
"uuid": testUuid,
},
Result: testUuid.String(),
},
{
Template: `{{byteAry}}`,
Data: map[string]any{
"byteAry": []byte("foobar🟡"),
},
Result: "foobar🟡",
},
}
for _, tst := range tests {
tmpl, err := New().WithEscapeMode(EscapeJSON).CompileString(tst.Template)
Expand Down

0 comments on commit 5197454

Please sign in to comment.