Skip to content

Commit

Permalink
feat: Provide values of withItems maps as JSON in {{item}}. Fixes #1905
Browse files Browse the repository at this point in the history
… (#1906)
  • Loading branch information
markterm authored and simster7 committed Jan 7, 2020
1 parent de3ffd7 commit 5d7e918
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,12 @@ func processItem(fstTmpl *fasttemplate.Template, name string, index int, item wf
replaceMap[fmt.Sprintf("item.%s", itemKey)] = fmt.Sprintf("%v", itemVal)
vals = append(vals, fmt.Sprintf("%s:%s", itemKey, itemVal))
}
jsonByteVal, err := json.Marshal(item.MapVal)
if err != nil {
return "", errors.InternalWrapError(err)
}
replaceMap["item"] = string(jsonByteVal)

// sort the values so that the name is deterministic
sort.Strings(vals)
newName = fmt.Sprintf("%s(%d:%v)", name, index, strings.Join(vals, ","))
Expand Down
5 changes: 3 additions & 2 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ spec:
arguments:
parameters:
- name: message
value: "{{item.os}} {{item.version}}"
value: "{{item.os}} {{item.version}} JSON({{item}})"
withItems:
- {os: debian, version: 9.1}
- {os: debian, version: 9.1}
Expand All @@ -910,7 +910,7 @@ spec:
container:
image: docker/whalesay:latest
command: [sh, -c]
args: ["cowsay {{inputs.parameters.message}}"]
args: ["cowsay \"{{inputs.parameters.message}}\""]
`

func TestExpandWithItemsMap(t *testing.T) {
Expand All @@ -924,6 +924,7 @@ func TestExpandWithItemsMap(t *testing.T) {
newSteps, err := woc.expandStep(wf.Spec.Templates[0].Steps[0].Steps[0])
assert.NoError(t, err)
assert.Equal(t, 3, len(newSteps))
assert.Equal(t, "debian 9.1 JSON({\"os\":\"debian\",\"version\":9.1})", *newSteps[0].Arguments.Parameters[0].Value)
}

var suspendTemplate = `
Expand Down
2 changes: 2 additions & 0 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,8 @@ spec:
- false
- string
- 1.2
- os: "debian"
version: "9.0"
- name: whalesay
inputs:
Expand Down

0 comments on commit 5d7e918

Please sign in to comment.