Added eval operator

This commit is contained in:
Mike Farah
2022-02-01 14:47:51 +11:00
parent 77204551ab
commit 535799462f
19 changed files with 209 additions and 30 deletions
+34
View File
@@ -0,0 +1,34 @@
package yqlib
import (
"testing"
)
var evalOperatorScenarios = []expressionScenario{
{
description: "Dynamically evaluate a path",
document: `{pathExp: '.a.b[] | select(.name == "cat")', a: {b: [{name: dog}, {name: cat}]}}`,
expression: `eval(.pathExp)`,
expected: []string{
"D0, P[a b 1], (!!map)::{name: cat}\n",
},
},
{
description: "Dynamically update a path from an environment variable",
subdescription: "The env variable can be any valid yq expression.",
document: `{a: {b: [{name: dog}, {name: cat}]}}`,
environmentVariable: ".a.b[0].name",
expression: `eval(strenv(myenv)) = "cow"`,
expected: []string{
"D0, P[], (doc)::{a: {b: [{name: cow}, {name: cat}]}}\n",
},
},
}
func TestEvalOperatorsScenarios(t *testing.T) {
for _, tt := range evalOperatorScenarios {
testScenario(t, &tt)
}
documentOperatorScenarios(t, "eval", evalOperatorScenarios)
}