-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
templating: adding fieldFrom helper function to retrieve a field from…
… a previous expression (#210) `field` templating helper function allow to retrieve a key into a map[string], even if the key is not a valid templating keyword. `fieldFrom` provides the same feature, expect that it doesn't use the values map from the task/resolution/steps, but use the previous templating pipeline as a templating source. Closes #200 Signed-off-by: Romain Beuque <[email protected]>
- Loading branch information
Showing
5 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package values_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ghodss/yaml" | ||
"github.com/maxatome/go-testdeep/td" | ||
"github.com/ovh/utask/engine/values" | ||
) | ||
|
||
func TestTmpl(t *testing.T) { | ||
input := `"step": | ||
"first": | ||
"output": | ||
"result": | ||
"my-payload": "{\"common-name\":\"utask.example.org\",\"id\":32,\"foo\":{\"bar\":1}}"` | ||
obj := map[string]map[string]map[string]map[string]interface{}{} | ||
err := yaml.Unmarshal([]byte(input), &obj) | ||
td.CmpNil(t, err) | ||
|
||
v := values.NewValues() | ||
v.SetOutput("first", obj["step"]["first"]["output"]) | ||
|
||
output, err := v.Apply("{{ field `step` `first` `output` `result` `my-payload` }}", nil, "foo") | ||
td.CmpNil(t, err) | ||
td.Cmp(t, string(output), "{\"common-name\":\"utask.example.org\",\"id\":32,\"foo\":{\"bar\":1}}") | ||
|
||
output, err = v.Apply("{{ field `step` `first` `output` `result` `my-payload` | fromJson | fieldFrom `common-name` }}", nil, "foo") | ||
td.CmpNil(t, err) | ||
td.Cmp(t, string(output), "utask.example.org") | ||
|
||
output, err = v.Apply("{{ field `step` `first` `output` `result` `my-payload` | fromJson | fieldFrom `foo` `bar` }}", nil, "foo") | ||
td.CmpNil(t, err) | ||
td.Cmp(t, string(output), "1") | ||
|
||
output, err = v.Apply("{{ `{\"common-name\":\"utask.example.org\",\"id\":32}` | fromJson | fieldFrom `invalid` | default `example.org` }}", nil, "foo") | ||
td.CmpNil(t, err) | ||
td.Cmp(t, string(output), "example.org") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters