Skip to content

Commit

Permalink
Adding toJSON and toYAML functions
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Mar 7, 2017
1 parent 198c114 commit 7b6434d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Gomplate is an alternative that will let you process templates which also includ
- [Example](#example)
- [`yamlArray`](#yamlarray)
- [Example](#example)
- [`toJSON`](#tojson)
- [Example](#example)
- [`toYAML`](#toyaml)
- [Example](#example)
- [`datasource`](#datasource)
- [Examples](#examples)
- [Basic usage](#basic-usage)
Expand Down Expand Up @@ -111,7 +115,7 @@ $ apk add gomplate
...
```

_Note: the Alpine version of gomplate may lag behind the latest release of gomplate._
_Note: the Alpine version of gomplate may lag behind the latest release of gomplate._

### use with Docker

Expand Down Expand Up @@ -453,6 +457,43 @@ $ gomplate < input.tmpl
Hello world
```

#### `toJSON`

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

##### Example

_This is obviously contrived - `json` is used to create an object._

_`input.tmpl`:_
```
{{ (`{"foo":{"hello":"world"}}` | json).foo | toJSON }}
```

```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`.

##### Example

_This is obviously contrived - `json` is used to create an object._

_`input.tmpl`:_
```
{{ (`{"foo":{"hello":"world"}}` | json).foo | toYAML }}
```

```console
$ gomplate < input.tmpl
hello: world

```

#### `datasource`

Parses a given datasource (provided by the [`--datasource/-d`](#--datasource-d) argument).
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func NewGomplate(data *Data) *Gomplate {
"yamlArray": typeconv.YAMLArray,
"slice": typeconv.Slice,
"join": typeconv.Join,
"toJSON": typeconv.ToJSON,
"toYAML": typeconv.ToYAML,
"ec2meta": ec2meta.Meta,
"ec2dynamic": ec2meta.Dynamic,
"ec2tag": ec2info.Tag,
Expand Down
19 changes: 19 additions & 0 deletions typeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ func (t *TypeConv) YAMLArray(in string) []interface{} {
return unmarshalArray(obj, in, yaml.Unmarshal)
}

func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) string {
b, err := f(obj)
if err != nil {
log.Fatalf("Unable to marshal object %s: %v", obj, err)
}

return string(b)
}

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

// ToYAML - Stringify a struct as YAML
func (t *TypeConv) ToYAML(in interface{}) string {
return marshalObj(in, yaml.Marshal)
}

// Slice creates a slice from a bunch of arguments
func (t *TypeConv) Slice(args ...interface{}) []interface{} {
return args
Expand Down
46 changes: 46 additions & 0 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -59,6 +60,51 @@ func TestUnmarshalArray(t *testing.T) {
`))
}

func TestToJSON(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.ToJSON(in))
}

func TestToYAML(t *testing.T) {
ty := new(TypeConv)
expected := `d: 2006-01-02T15:04:05.999999999-07:00
foo: bar
? |-
multi
line
key
: hello: world
one: 1
"true": true
`
mst, _ := time.LoadLocation("MST")
in := map[string]interface{}{
"foo": "bar",
"one": 1,
"true": true,
`multi
line
key`: map[string]interface{}{
"hello": "world",
},
"d": time.Date(2006, time.January, 2, 15, 4, 5, 999999999, mst),
}
assert.Equal(t, expected, ty.ToYAML(in))
}

func TestSlice(t *testing.T) {
ty := new(TypeConv)
expected := []string{"foo", "bar"}
Expand Down

0 comments on commit 7b6434d

Please sign in to comment.