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 YAML funcs: fromYaml, toYaml, mustFromYaml, mustToYaml #366

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
29 changes: 29 additions & 0 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"strings"
"time"

"gopkg.in/yaml.v3"
)

func init() {
Expand Down Expand Up @@ -161,3 +163,30 @@ func ternary(vt interface{}, vf interface{}, v bool) interface{} {

return vf
}

// fromYaml decodes YAML into a structured value, ignoring errors.
func fromYaml(v string) interface{} {
output, _ := mustFromYaml(v)
return output
}

// mustFromYaml decodes YAML into a structured value, returning errors.
func mustFromYaml(v string) (interface{}, error) {
var output interface{}
err := yaml.Unmarshal([]byte(v), &output)
return output, err
}

// toYaml encodes an item into a YAML string
func toYaml(v interface{}) string {
output, _ := yaml.Marshal(v)
return string(output)
}

func mustToYaml(v interface{}) (string, error) {
output, err := yaml.Marshal(v)
if err != nil {
return "", err
}
return string(output), nil
}
26 changes: 26 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,29 @@ func TestTernary(t *testing.T) {
t.Error(err)
}
}

func TestFromYaml(t *testing.T) {
dict := map[string]interface{}{"Input": `foo: 55`}

tpl := `{{.Input | fromYaml}}`
expected := `map[foo:55]`
if err := runtv(tpl, expected, dict); err != nil {
t.Error(err)
}

tpl = `{{(.Input | fromYaml).foo}}`
expected = `55`
if err := runtv(tpl, expected, dict); err != nil {
t.Error(err)
}
}

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

tpl := `{{.Top | toYaml}}`
expected := "bool: true\nnumber: 42\nstring: test\n"
if err := runtv(tpl, expected, dict); err != nil {
t.Error(err)
}
}
18 changes: 18 additions & 0 deletions docs/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ false | ternary "foo" "bar"
```

The above returns `"bar"`.

## fromYaml, mustFromYaml

`fromYaml` decodes a YAML document into a structure. If the input cannot be decoded as YAML the function will return an empty string.
`mustFromYaml` will return an error in case the YAML is invalid.

```
fromYaml "foo: 55"
```

## toYaml, mustToYaml

The `toYaml` function encodes an item into a YAML string. If the item cannot be converted to YAML the function will return an empty string.
`mustToYaml` will return an error in case the item cannot be encoded in YAML.

```
toYaml .Item
```
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Sprig library provides over 70 template functions for Go's template language
- [Integer Slice Functions](integer_slice.md): `until`, `untilStep`
- [Float Math Functions](mathf.md): `addf`, `maxf`, `mulf`, etc.
- [Date Functions](date.md): `now`, `date`, etc.
- [Defaults Functions](defaults.md): `default`, `empty`, `coalesce`, `fromJson`, `toJson`, `toPrettyJson`, `toRawJson`, `ternary`
- [Defaults Functions](defaults.md): `default`, `empty`, `coalesce`, `fromJson`, `toJson`, `toPrettyJson`, `toRawJson`, `ternary`, `fromYaml`, `toYaml`
- [Encoding Functions](encoding.md): `b64enc`, `b64dec`, etc.
- [Lists and List Functions](lists.md): `list`, `first`, `uniq`, etc.
- [Dictionaries and Dict Functions](dicts.md): `get`, `set`, `dict`, `hasKey`, `pluck`, `dig`, `deepCopy`, etc.
Expand Down
4 changes: 4 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ var genericMap = map[string]interface{}{
"mustToPrettyJson": mustToPrettyJson,
"mustToRawJson": mustToRawJson,
"ternary": ternary,
"fromYaml": fromYaml,
"toYaml": toYaml,
"mustFromYaml": mustFromYaml,
"mustToYaml": mustToYaml,
"deepCopy": deepCopy,
"mustDeepCopy": mustDeepCopy,

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.3.0
gopkg.in/yaml.v3 v3.0.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=