Skip to content

Commit

Permalink
template filters: replaced parseJSON and parseJSONArray with parseYAM…
Browse files Browse the repository at this point in the history
…L and parseYAMLArray

both filters are working now with json and yaml (json is a subset of
yaml)
  • Loading branch information
HeavyHorst committed Nov 20, 2016
1 parent 3a22136 commit 252e801
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/content/template/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ something
</details>

<details>
<summary> **parseJSON** -- Returns an map[string]interface{} of the json value.</summary>
<summary> **parseYAML** -- Returns an map[string]interface{} of the yaml/json value.</summary>
</details>

<details>
<summary> **parseJSONArray** -- Returns a []interface{} from a json array. </summary>
<summary> **parseYAMLArray** -- Returns a []interface{} from a yaml/json array. </summary>
</details>

<details>
Expand Down
12 changes: 6 additions & 6 deletions template/filter_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
func init() {
pongo2.RegisterFilter("reverse", filterReverse)
pongo2.RegisterFilter("sortByLength", filterSortByLength)
pongo2.RegisterFilter("parseJSON", filterUnmarshalJSONObject)
pongo2.RegisterFilter("parseJSONArray", filterUnmarshalJSONArray)
pongo2.RegisterFilter("parseYAML", filterUnmarshalYAMLObject)
pongo2.RegisterFilter("parseYAMLArray", filterUnmarshalYAMLArray)
pongo2.RegisterFilter("toJSON", filterToJSON)
pongo2.RegisterFilter("toPrettyJSON", filterToPrettyJSON)
pongo2.RegisterFilter("toYAML", filterToYAML)
Expand Down Expand Up @@ -85,25 +85,25 @@ func filterToYAML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2
return pongo2.AsValue(string(b)), nil
}

func filterUnmarshalJSONObject(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
func filterUnmarshalYAMLObject(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.IsString() {
return in, nil
}

var ret map[string]interface{}
if err := json.Unmarshal([]byte(in.String()), &ret); err != nil {
if err := yaml.Unmarshal([]byte(in.String()), &ret); err != nil {
return nil, &pongo2.Error{ErrorMsg: err.Error()}
}
return pongo2.AsValue(ret), nil
}

func filterUnmarshalJSONArray(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
func filterUnmarshalYAMLArray(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.IsString() {
return in, nil
}

var ret []interface{}
if err := json.Unmarshal([]byte(in.String()), &ret); err != nil {
if err := yaml.Unmarshal([]byte(in.String()), &ret); err != nil {
return nil, &pongo2.Error{ErrorMsg: err.Error()}
}
return pongo2.AsValue(ret), nil
Expand Down
8 changes: 4 additions & 4 deletions template/filter_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ test3: 2.5
t.Check(res.String(), Equals, expected)
}

func (s *FilterSuite) TestFilterUnmarshalJSONObject(t *C) {
func (s *FilterSuite) TestFilterUnmarshalYAMLObject(t *C) {
in := pongo2.AsValue(`{"test":"bla","test2":"1","test3":"2.5"}`)
expected := map[string]interface{}{
"test": "bla",
"test2": "1",
"test3": "2.5",
}
res, err := filterUnmarshalJSONObject(in, nil)
res, err := filterUnmarshalYAMLObject(in, nil)
if err != nil {
t.Error(err.ErrorMsg)
}
m1 := res.Interface().(map[string]interface{})
t.Check(m1, DeepEquals, expected)
}

func (s *FilterSuite) TestFilterUnmarshalJSONArray(t *C) {
func (s *FilterSuite) TestFilterUnmarshalYAMLArray(t *C) {
in := pongo2.AsValue(`["a", "b", "c"]`)
expected := []interface{}{"a", "b", "c"}
res, err := filterUnmarshalJSONArray(in, nil)
res, err := filterUnmarshalYAMLArray(in, nil)
if err != nil {
t.Error(err.ErrorMsg)
}
Expand Down

0 comments on commit 252e801

Please sign in to comment.