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

Using github.com/ugorji/go/codec for JSON encoding instead of encoding/json #144

Merged
merged 2 commits into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import:
- package: github.com/blang/vfs
version: c14afca
- package: gopkg.in/yaml.v2
repo: https://github.com/go-yaml/yaml.git
version: v2
repo: https://github.com/go-yaml/yaml.git
- package: github.com/spf13/cobra
- package: github.com/spf13/pflag
- package: github.com/ugorji/go
subpackages:
- codec
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
Expand Down
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
24 changes: 20 additions & 4 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,32 @@ 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)
err := codec.NewEncoder(buf, h).Encode(in)
if err != nil {
log.Fatalf("Unable to marshal %s: %v", in, err)
}
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)
out := new(bytes.Buffer)
b := toJSONBytes(in)
err := json.Indent(out, b, "", indent)
if err != nil {
log.Fatalf("Unable to marshal object %s: %v", in, err)
log.Fatalf("Unable to indent JSON %s: %v", b, err)
}

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
22 changes: 22 additions & 0 deletions vendor/github.com/ugorji/go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/ugorji/go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions vendor/github.com/ugorji/go/codec/0doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading