Skip to content

Commit

Permalink
Merge pull request #137 from hairyhenderson/tojsonpretty-func
Browse files Browse the repository at this point in the history
Adding new toJSONPretty function
  • Loading branch information
hairyhenderson committed May 12, 2017
2 parents 6695b99 + 527628a commit ebb865e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Gomplate is an alternative that will let you process templates which also includ
- [`yaml`](#yaml)
- [`yamlArray`](#yamlarray)
- [`toJSON`](#tojson)
- [`toJSONPretty`](#tojsonpretty)
- [`toYAML`](#toyaml)
- [`datasource`](#datasource)
- [`datasourceExists`](#datasourceexists)
Expand Down Expand Up @@ -544,6 +545,26 @@ $ gomplate < input.tmpl
{"hello":"world"}
```

#### `toJSONPretty`

Converts an object to a pretty-printed (or _indented_) JSON document. Input objects may be the result of `json`, `yaml`, `jsonArray`, or `yamlArray` functions, or they could be provided by a `datasource`.

The indent string must be provided as an argument.

##### Example

_`input.tmpl`:_
```
{{ `{"hello":"world"}` | json | toJSONPretty " " }}
```

```console
$ gomplate < input.tmpl
{
"hello": "world"
}
```

#### `toYAML`

Converts an object to a YAML document. Input objects may be the result of `json`, `yaml`, `jsonArray`, or `yamlArray` functions, or they could be provided by a `datasource`.
Expand Down
1 change: 1 addition & 0 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
"slice": typeconv.Slice,
"join": typeconv.Join,
"toJSON": typeconv.ToJSON,
"toJSONPretty": typeconv.toJSONPretty,
"toYAML": typeconv.ToYAML,
"ec2meta": ec2meta.Meta,
"ec2dynamic": ec2meta.Dynamic,
Expand Down
12 changes: 12 additions & 0 deletions test/integration/typeconv_funcs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ function teardown () {
[ "$status" -eq 0 ]
[[ "${output}" == "true" ]]
}

@test "toJSONPretty" {
gomplate -i '{{ `{"hello": "world"}` | json | toJSONPretty " " }}
{{ toJSONPretty "" (`{"hello": "world"}` | json) }}'
[ "$status" -eq 0 ]
[[ "${output}" == "{
\"hello\": \"world\"
}
{
\"hello\": \"world\"
}" ]]
}
10 changes: 10 additions & 0 deletions typeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ func (t *TypeConv) ToJSON(in interface{}) string {
return marshalObj(in, json.Marshal)
}

// 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)
}

return string(b)
}

// ToYAML - Stringify a struct as YAML
func (t *TypeConv) ToYAML(in interface{}) string {
return marshalObj(in, yaml.Marshal)
Expand Down
29 changes: 29 additions & 0 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ func TestToJSON(t *testing.T) {
assert.Equal(t, expected, ty.ToJSON(in))
}

func TestToJSONPretty(t *testing.T) {
ty := new(TypeConv)
expected := `{
"down": {
"the": {
"rabbit": {
"hole": true
}
}
},
"foo": "bar",
"one": 1,
"true": true
}`
in := map[string]interface{}{
"foo": "bar",
"one": 1,
"true": true,
"down": map[string]interface{}{
"the": map[string]interface{}{
"rabbit": map[string]interface{}{
"hole": true,
},
},
},
}
assert.Equal(t, expected, ty.toJSONPretty(" ", in))
}

func TestToYAML(t *testing.T) {
ty := new(TypeConv)
expected := `d: 2006-01-02T15:04:05.999999999-07:00
Expand Down

0 comments on commit ebb865e

Please sign in to comment.