Skip to content

Commit

Permalink
Switching to github.com/ugorji/go/codec instead of encoding/json
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed May 20, 2017
1 parent 28b3c7e commit 83b143b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
28 changes: 21 additions & 7 deletions test/integration/typeconv_funcs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ function teardown () {
[[ "${output}" == "true" ]]
}

@test "toJSONPretty" {
gomplate -i '{{ `{"hello": "world"}` | json | toJSONPretty " " }}
{{ toJSONPretty "" (`{"hello": "world"}` | json) }}'
@test "'toJSON' can handle nested maps" {
gomplate -i '{{ "foo:\n bar:\n baz: qux" | yaml | toJSON }}'
[ "$status" -eq 0 ]
[[ "${output}" == "{
\"hello\": \"world\"
[[ "${output}" == '{"foo":{"bar":{"baz":"qux"}}}' ]]
}

@test "'toJSONPretty' can handle nested maps" {
gomplate -i '{{ `{"foo":{"bar":{"baz":"qux"}}}` | json | toJSONPretty " " }}
{{ toJSONPretty "" (`{"foo":{"bar":{"baz":"qux"}}}` | json) }}'
[ "$status" -eq 0 ]
[[ "${output}" == '{
"foo": {
"bar": {
"baz": "qux"
}
}
}
{
\"hello\": \"world\"
}" ]]
"foo": {
"bar": {
"baz": "qux"
}
}
}' ]]
}

@test "indent" {
Expand Down
22 changes: 16 additions & 6 deletions typeconv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand All @@ -9,6 +10,8 @@ import (
"strings"

yaml "gopkg.in/yaml.v2"

"github.com/ugorji/go/codec"
)

// TypeConv - type conversion function
Expand Down Expand Up @@ -74,19 +77,26 @@ func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) string {
return string(b)
}

func toJSONBytes(in interface{}) []byte {
h := &codec.JsonHandle{}
h.Canonical = true
buf := new(bytes.Buffer)
codec.NewEncoder(buf, h).Encode(in)
return buf.Bytes()
}

// ToJSON - Stringify a struct as JSON
func (t *TypeConv) ToJSON(in interface{}) string {
return marshalObj(in, json.Marshal)
return string(toJSONBytes(in))
}

// ToJSONPretty - Stringify a struct as JSON (indented)
func (t *TypeConv) toJSONPretty(indent string, in interface{}) string {
b, err := json.MarshalIndent(in, "", indent)
if err != nil {
log.Fatalf("Unable to marshal object %s: %v", in, err)
}
out := new(bytes.Buffer)
b := toJSONBytes(in)
json.Indent(out, b, "", indent)

return string(b)
return string(out.Bytes())
}

// ToYAML - Stringify a struct as YAML
Expand Down
6 changes: 3 additions & 3 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func TestToJSON(t *testing.T) {
"foo": "bar",
"one": 1,
"true": true,
"down": map[string]interface{}{
"the": map[string]interface{}{
"rabbit": map[string]interface{}{
"down": map[interface{}]interface{}{
"the": map[interface{}]interface{}{
"rabbit": map[interface{}]interface{}{
"hole": true,
},
},
Expand Down

0 comments on commit 83b143b

Please sign in to comment.