Skip to content

Commit 9b9cc6d

Browse files
committed
feat: support looping over map variables
1 parent 311cdf0 commit 9b9cc6d

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

docs/docs/experiments/any_variables.mdx

+24-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ tasks:
8686
cmd: echo {{.ITEM}}
8787
```
8888

89-
Because this experiment adds support for array variables, the `for` keyword has
90-
been updated to support looping over arrays directly:
89+
Because this experiment adds support for "collection-type" variables, the `for`
90+
keyword has been updated to support looping over arrays directly:
9191

9292
```yaml
9393
version: 3
@@ -102,6 +102,28 @@ tasks:
102102
cmd: echo {{.ITEM}}
103103
```
104104

105+
This also works for maps. However, remember that maps are unordered, so the
106+
order in which the items are looped over is random:
107+
108+
```yaml
109+
version: 3
110+
111+
tasks:
112+
foo:
113+
vars:
114+
MAP:
115+
KEY_1:
116+
SUBKEY: sub_value_1
117+
KEY_2:
118+
SUBKEY: sub_value_2
119+
KEY_3:
120+
SUBKEY: sub_value_3
121+
cmds:
122+
- for:
123+
var: MAP
124+
cmd: echo {{.ITEM.SUBKEY}}
125+
```
126+
105127
String splitting is still supported and remember that for simple cases, you have
106128
always been able to loop over an array without using variables at all:
107129

testdata/vars/any/Taskfile.yml

+26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ tasks:
99
- task: string-array
1010
- task: for-string
1111
- task: for-int
12+
- task: for-map
13+
- task: for-multi-layer-map
1214

1315
dynamic:
1416
vars:
@@ -78,3 +80,27 @@ tasks:
7880
var: LIST
7981
cmd: echo {{add .ITEM 100}}
8082

83+
for-map:
84+
vars:
85+
MAP:
86+
KEY_1: value_1
87+
KEY_2: value_2
88+
KEY_3: value_3
89+
cmds:
90+
- for:
91+
var: MAP
92+
cmd: echo {{.ITEM}}
93+
94+
for-multi-layer-map:
95+
vars:
96+
MAP:
97+
KEY_1:
98+
SUBKEY: sub_value_1
99+
KEY_2:
100+
SUBKEY: sub_value_2
101+
KEY_3:
102+
SUBKEY: sub_value_3
103+
cmds:
104+
- for:
105+
var: MAP
106+
cmd: echo {{.ITEM.SUBKEY}}

variables.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/joho/godotenv"
9+
"golang.org/x/exp/maps"
910

1011
"github.com/go-task/task/v3/errors"
1112
"github.com/go-task/task/v3/internal/execext"
@@ -170,10 +171,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
170171
case []any:
171172
list = value
172173
case map[string]any:
173-
return &taskfile.Task{}, errors.TaskfileInvalidError{
174-
URI: origTask.Location.Taskfile,
175-
Err: errors.New("sh is not supported with the 'Any Variables' experiment enabled.\nSee https://taskfile.dev/experiments/any-variables for more information."),
176-
}
174+
list = maps.Values(value)
177175
default:
178176
return nil, errors.TaskfileInvalidError{
179177
URI: origTask.Location.Taskfile,

0 commit comments

Comments
 (0)