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

Add fromJson function #230

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sprig
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
)
Expand Down Expand Up @@ -63,6 +64,24 @@ func coalesce(v ...interface{}) interface{} {
return nil
}

// fromJSON decodes a JSON string to an item.
func fromJson(v interface{}) interface{} {
var result interface{}
switch v := v.(type) {
case []byte:
if err := json.Unmarshal(v, &result); err != nil {
panic(err)
}
case string:
if err := json.Unmarshal([]byte(v), &result); err != nil {
panic(err)
}
default:
panic(fmt.Errorf("Cannot decode JSON from type %T", v))
}
return result
}

// toJson encodes an item into a JSON string
func toJson(v interface{}) string {
output, _ := json.Marshal(v)
Expand Down
10 changes: 10 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ func TestCoalesce(t *testing.T) {
}
}

func TestFromJson(t *testing.T) {
dict := map[string]interface{}{"Top": `{"number":42}`}

tpl := `{{ index (.Top | fromJson) "number" }}`
expected := `42`
if err := runtv(tpl, expected, dict); err != nil {
t.Error(err)
}
}

func TestToJson(t *testing.T) {
dict := map[string]interface{}{"Top": map[string]interface{}{"bool": true, "string": "test", "number": 42}}

Expand Down
10 changes: 10 additions & 0 deletions docs/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ The above will first check to see if `.name` is empty. If it is not, it will ret
that value. If it _is_ empty, `coalesce` will evaluate `.parent.name` for emptiness.
Finally, if both `.name` and `.parent.name` are empty, it will return `Matt`.

## fromJson

The `fromJson` function decodes data from a JSON string. If the item cannot be decoded the function will return an error.

```
fromJson .Value
```

The above decodes the JSON data from `.Value`.

## toJson, mustToJson

The `toJson` function encodes an item into a JSON string. If the item cannot be converted to JSON the function will return an empty string.
Expand Down
1 change: 1 addition & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ var genericMap = map[string]interface{}{
"empty": empty,
"coalesce": coalesce,
"compact": compact,
"fromJson": fromJson,
"mustCompact": mustCompact,
"toJson": toJson,
"toPrettyJson": toPrettyJson,
Expand Down