Skip to content

Commit

Permalink
feat: make vals able to eval dictionaries (#530)
Browse files Browse the repository at this point in the history
* feat: make vals able to eval dictionaries

Signed-off-by: Thomas Becker <[email protected]>

* chore: gci format

Signed-off-by: Thomas Becker <[email protected]>

---------

Signed-off-by: Thomas Becker <[email protected]>
  • Loading branch information
thomasbecker authored Nov 1, 2024
1 parent af6d407 commit c6d5af5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
45 changes: 41 additions & 4 deletions vals.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func (r *Runtime) Get(code string) (string, error) {

return ret, nil
}

func cloneMap(m map[string]interface{}) map[string]interface{} {
bs, err := yaml.Marshal(m)
if err != nil {
Expand Down Expand Up @@ -615,15 +616,28 @@ func EvalNodes(nodes []yaml.Node, c Options) ([]yaml.Node, error) {
var res []yaml.Node
for _, node := range nodes {
node := node
var nodeValue map[string]interface{}
var nodeValue interface{}
err := node.Decode(&nodeValue)
if err != nil {
return nil, err
}
evalResult, err := Eval(nodeValue, c)
if err != nil {
return nil, err

var evalResult interface{}
switch v := nodeValue.(type) {
case map[string]interface{}:
evalResult, err = Eval(v, c)
if err != nil {
return nil, err
}
case []interface{}:
evalResult, err = evalArray(v, c)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unexpected type: %T", v)
}

err = node.Encode(evalResult)
if err != nil {
return nil, err
Expand All @@ -637,6 +651,29 @@ func EvalNodes(nodes []yaml.Node, c Options) ([]yaml.Node, error) {
return res, nil
}

func evalArray(arr []interface{}, c Options) ([]interface{}, error) {
var res []interface{}
for _, item := range arr {
switch v := item.(type) {
case map[string]interface{}:
evalResult, err := Eval(v, c)
if err != nil {
return nil, err
}
res = append(res, evalResult)
case []interface{}:
evalResult, err := evalArray(v, c)
if err != nil {
return nil, err
}
res = append(res, evalResult)
default:
res = append(res, v)
}
}
return res, nil
}

func Eval(template map[string]interface{}, o ...Options) (map[string]interface{}, error) {
opts := Options{}
if len(o) > 0 {
Expand Down
33 changes: 33 additions & 0 deletions vals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,36 @@ kind: Secret

require.Equal(t, expected, buf.String())
}

func TestEvalNodesWithDictionaries(t *testing.T) {
var yamlDocs = `- entry: first
username: ref+echo://secrets.enc.yaml
- entry: second
username: ref+echo://secrets.enc.yaml
`

var expected = `- entry: first
username: secrets.enc.yaml
- entry: second
username: secrets.enc.yaml
`

tmpFile, err := os.CreateTemp("", "secrets.yaml")
defer os.Remove(tmpFile.Name())
require.NoError(t, err)

_, err = tmpFile.WriteString(yamlDocs)
require.NoError(t, err)

input, err := Inputs(tmpFile.Name())
require.NoError(t, err)

nodes, err := EvalNodes(input, Options{})
require.NoError(t, err)
buf := new(strings.Builder)

err = Output(buf, "", nodes)
require.NoError(t, err)

require.Equal(t, expected, buf.String())
}

0 comments on commit c6d5af5

Please sign in to comment.