11---
2- slug : /experiments/any -variables/
2+ slug : /experiments/map -variables/
33---
44
55import Tabs from ' @theme/Tabs' ;
66import TabItem from ' @theme/TabItem' ;
77
8- # Any Variables (#1415 )
8+ # Map Variables (#1585 )
99
1010:::caution
1111
@@ -15,19 +15,9 @@ environment. They are intended for testing and feedback only.
1515
1616:::
1717
18- Currently, Task only supports string variables. This experiment allows you to
19- specify and use the following variable types:
20-
21- - ` string `
22- - ` bool `
23- - ` int `
24- - ` float `
25- - ` array `
26- - ` map `
27-
28- This allows you to have a lot more flexibility in how you use variables in
29- Task's templating engine. There are two active proposals for this experiment.
30- Click on the tabs below to switch between them.
18+ Currently, Task supports all variable types except for maps. This experiment
19+ adds two different proposals for map variables. Click on the tabs below to
20+ switch between them.
3121
3222<Tabs defaultValue = " 1" queryString = " proposal"
3323 values = { [
@@ -48,13 +38,11 @@ This experiment proposal breaks the following functionality:
4838:::info
4939
5040To enable this experiment, set the environment variable:
51- ` TASK_X_ANY_VARIABLES =1` . Check out [ our guide to enabling experiments
41+ ` TASK_X_MAP_VARIABLES =1` . Check out [ our guide to enabling experiments
5242] [ enabling-experiments ] for more information.
5343
5444:::
5545
56- ## Maps
57-
5846This proposal removes support for the ` sh ` keyword in favour of a new syntax for
5947dynamically defined variables, This allows you to define a map directly as you
6048would for any other type:
@@ -111,19 +99,16 @@ will now need to escape the `$` with a backslash (`\`) to stop Task from
11199executing it as a command.
112100
113101</TabItem>
114-
115102<TabItem value="2">
116103
117104:::info
118105
119106To enable this experiment, set the environment variable :
120- ` TASK_X_ANY_VARIABLES =2` . Check out [our guide to enabling experiments
107+ ` TASK_X_MAP_VARIABLES =2` . Check out [our guide to enabling experiments
121108][enabling-experiments] for more information.
122109
123110:: :
124111
125- # # Maps
126-
127112This proposal maintains backwards-compatibility and the `sh` subkey and adds
128113another new `map` subkey for defining map variables :
129114
@@ -150,7 +135,13 @@ objects/arrays. This is similar to the `fromJSON` template function, but means
150135that you only have to parse the JSON/YAML once when you declare the variable,
151136instead of every time you want to access a value.
152137
153- Before :
138+ <Tabs defaultValue="2"
139+ values={[
140+ {label: 'Before', value: '1'},
141+ {label: 'After', value: '2'}
142+ ]}>
143+
144+ <TabItem value="1">
154145
155146` ` ` yaml
156147version: 3
@@ -164,7 +155,8 @@ tasks:
164155 - 'echo {{(fromJSON .FOO).b}}'
165156` ` `
166157
167- After :
158+ </TabItem>
159+ <TabItem value="2">
168160
169161` ` ` yaml
170162version: 3
@@ -179,12 +171,26 @@ tasks:
179171 - 'echo {{.FOO.b}}'
180172` ` `
181173
174+ </TabItem></Tabs>
175+
182176# # Variables by reference
183177
184178Lastly, this proposal adds support for defining and passing variables by
185179reference. This is really important now that variables can be types other than a
186- string. Previously, to send a variable from one task to another, you would have
187- to use the templating system to pass it :
180+ string.
181+
182+ Previously, to send a variable from one task to another, you would have to use
183+ the templating system. Unfortunately, the templater _always_ outputs a string
184+ and operations on the passed variable may not have behaved as expected. With
185+ this proposal, you can now pass variables by reference using the `ref` subkey :
186+
187+ <Tabs defaultValue="2"
188+ values={[
189+ {label: 'Before', value: '1'},
190+ {label: 'After', value: '2'}
191+ ]}>
192+
193+ <TabItem value="1">
188194
189195` ` ` yaml
190196version: 3
@@ -202,10 +208,8 @@ tasks:
202208 - 'echo {{index .FOO 0}}' # <-- FOO is a string so the task outputs '91' which is the ASCII code for '[' instead of the expected 'A'
203209` ` `
204210
205- Unfortunately, this results in the value always being passed as a string as this
206- is the output type of the templater and operations on the passed variable may
207- not behave as expected. With this proposal, you can now pass variables by
208- reference using the `ref` subkey :
211+ </TabItem>
212+ <TabItem value="2">
209213
210214` ` ` yaml
211215version: 3
@@ -218,12 +222,14 @@ tasks:
218222 - task: bar
219223 vars:
220224 FOO:
221- ref: FOO # <-- FOO gets passed by reference to bar and maintains its type
225+ ref: . FOO # <-- FOO gets passed by reference to bar and maintains its type
222226 bar:
223227 cmds:
224228 - 'echo {{index .FOO 0}}' # <-- FOO is still a map so the task outputs 'A' as expected
225229` ` `
226230
231+ </TabItem></Tabs>
232+
227233This means that the type of the variable is maintained when it is passed to
228234another Task. This also works the same way when calling `deps` and when defining
229235a variable and can be used in any combination :
@@ -236,111 +242,94 @@ tasks:
236242 vars:
237243 FOO: [A, B, C] # <-- FOO is defined as an array
238244 BAR:
239- ref: FOO # <-- BAR is defined as a reference to FOO
245+ ref: . FOO # <-- BAR is defined as a reference to FOO
240246 deps:
241247 - task: bar
242248 vars:
243249 BAR:
244- ref: BAR # <-- BAR gets passed by reference to bar and maintains its type
250+ ref: . BAR # <-- BAR gets passed by reference to bar and maintains its type
245251 bar:
246252 cmds:
247253 - 'echo {{index .BAR 0}}' # <-- BAR still refers to FOO so the task outputs 'A'
248254` ` `
249255
250- </TabItem></Tabs>
251-
252- ---
253-
254- # # Common to both proposals
255-
256- Both proposals add support for all other variable types by directly defining
257- them in the Taskfile. For example :
258-
259- # ## Evaluating booleans
256+ All references use the same templating syntax as regular templates, so in
257+ addition to simply calling `.FOO`, you can also pass subkeys (`.FOO.BAR`) or
258+ indexes (`index .FOO 0`) and use functions (`len .FOO`) :
260259
261260` ` ` yaml
262261version: 3
263262
264263tasks:
265264 foo:
266265 vars:
267- BOOL: false
266+ FOO: [A, B, C] # <-- FOO is defined as an array
268267 cmds:
269- - '{{if .BOOL}}echo foo{{end}}'
270- ` ` `
271-
272- # ## Arithmetic
273-
274- ` ` ` yaml
275- version: 3
276-
277- tasks:
278- foo:
279- vars:
280- INT: 10
281- FLOAT: 3.14159
268+ - task: bar
269+ vars:
270+ FOO:
271+ ref: index .FOO 0 # <-- The element at index 0 is passed by reference to bar
272+ bar:
282273 cmds:
283- - 'echo {{add .INT .FLOAT}} '
274+ - 'echo {{.MYVAR}}' # <-- FOO is just the letter 'A '
284275` ` `
285276
286- # ## Ranging
287-
288- ` ` ` yaml
289- version: 3
277+ </TabItem></Tabs>
290278
291- tasks:
292- foo:
293- vars:
294- ARRAY: [1, 2, 3]
295- cmds:
296- - 'echo {{range .ARRAY}}{{.}}{{end}}'
297- ` ` `
279+ # # Looping over maps
298280
299- There are many more templating functions which can be used with the new types of
300- variables. For a full list, see the [slim-sprig][slim-sprig] documentation.
281+ This experiment also adds support for looping over maps using the `for` keyword,
282+ just like arrays. In addition to the `{{.ITEM}}` variable being populated when
283+ looping over a map, we also make an additional `{{.KEY}}` variable available
284+ that holds the string value of the map key.
301285
302- # # Looping over variables
286+ <Tabs defaultValue="1" queryString="proposal"
287+ values={[
288+ {label: 'Proposal 1', value: '1'},
289+ {label: 'Proposal 2', value: '2'}
290+ ]}>
303291
304- Previously, you would have to use a delimiter separated string to loop over an
305- arbitrary list of items in a variable and split them by using the `split` subkey
306- to specify the delimiter :
292+ <TabItem value="1">
307293
308294` ` ` yaml
309295version: 3
310296
311297tasks:
312298 foo:
313299 vars:
314- LIST: 'foo,bar,baz'
300+ MAP: {a: 1, b: 2, c: 3}
315301 cmds:
316302 - for:
317- var: LIST
318- split: ','
319- cmd: echo {{.ITEM}}
303+ var: MAP
304+ cmd: 'echo "{{.KEY}}: {{.ITEM}}"'
320305` ` `
321306
322- Both of these proposals add support for looping over "collection-type" variables
323- using the `for` keyword, so now you are able to loop over a map/array variable
324- directly :
307+ </TabItem>
308+ <TabItem value="2">
325309
326310` ` ` yaml
327311version: 3
328312
329313tasks:
330314 foo:
331315 vars:
332- LIST: [foo, bar, baz]
316+ map:
317+ MAP: {a: 1, b: 2, c: 3}
333318 cmds:
334319 - for:
335- var: LIST
336- cmd: echo {{.ITEM}}
320+ var: MAP
321+ cmd: ' echo " {{.KEY}}: {{. ITEM}}"'
337322` ` `
338323
339- When looping over a map we also make an additional `{{.KEY}}` variable availabe
340- that holds the string value of the map key. Remember that maps are unordered, so
324+ :::note
325+
326+ Remember that maps are unordered, so
341327the order in which the items are looped over is random.
342328
329+ :: :
330+
331+ </TabItem></Tabs>
332+
343333{/* prettier-ignore-start */}
344- [enabling-experiments] : /experiments/#enabling-experiments
345- [slim-sprig] : https://go-task.github.io/slim-sprig/
334+ [enabling-experiments] : ./experiments.mdx#enabling-experiments
346335{/* prettier-ignore-end */}
0 commit comments