Skip to content

Commit

Permalink
Adding json filter function
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Mar 26, 2016
1 parent 2a00d3e commit 158995a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ $ FOO=true gomplate < input.tmpl
foo
```

#### `json`

Converts a JSON string into an object. Only works for JSON Objects. This can be used to access properties of JSON objects.

##### Example

_`input.tmpl`:_
```
Hello {{ (getenv "FOO" | json).hello }}
```

```console
$ export FOO='{"hello":"world"}'
$ gomplate < input.tmpl
Hello world
```

### Some more complex examples

##### Variable assignment and `if`/`else`
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func NewGomplate() *Gomplate {
"getenv": env.Getenv,
"Bool": typeconv.Bool,
"bool": typeconv.Bool,
"JSON": typeconv.JSON,
"json": typeconv.JSON,
"Ec2meta": ec2meta.Ec2meta,
"ec2meta": ec2meta.Ec2meta,
},
Expand Down
14 changes: 14 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ func TestEc2MetaTemplates_ValidKey(t *testing.T) {
assert.Equal(t, "i-1234", testTemplate(g, `{{ec2meta "instance-id"}}`))
assert.Equal(t, "i-1234", testTemplate(g, `{{ec2meta "instance-id" "default"}}`))
}

func TestEc2MetaTemplates_WithJSON(t *testing.T) {
server, ec2meta := aws.MockServer(200, `{"foo":"bar"}`)
defer server.Close()
ty := new(TypeConv)
g := &Gomplate{
funcMap: template.FuncMap{
"ec2meta": ec2meta.Ec2meta,
"json": ty.JSON,
},
}

assert.Equal(t, "bar", testTemplate(g, `{{ (ec2meta "obj" | json).foo }}`))
}
16 changes: 15 additions & 1 deletion typeconv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "strconv"
import (
"encoding/json"
"log"
"strconv"
)

// TypeConv - type conversion function
type TypeConv struct {
Expand All @@ -15,3 +19,13 @@ func (t *TypeConv) Bool(in string) bool {
}
return false
}

// JSON - Unmarshal a JSON string
func (t *TypeConv) JSON(in string) map[string]interface{} {
obj := make(map[string]interface{})
err := json.Unmarshal([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal JSON object %s: %v", in, err)
}
return obj
}
13 changes: 13 additions & 0 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ func TestBool(t *testing.T) {
assert.True(t, ty.Bool("T"))
assert.True(t, ty.Bool("1"))
}

func TestJSON(t *testing.T) {
ty := new(TypeConv)
expected := make(map[string]interface{})
expected["foo"] = "bar"
expected["one"] = 1.0
expected["true"] = true

actual := ty.JSON(`{"foo":"bar","one":1.0,"true":true}`)
assert.Equal(t, expected["foo"], actual["foo"])
assert.Equal(t, expected["one"], actual["one"])
assert.Equal(t, expected["true"], actual["true"])
}

0 comments on commit 158995a

Please sign in to comment.