@@ -1144,53 +1144,38 @@ variable types are supported:
1144
1144
- ` int`
1145
1145
- ` float`
1146
1146
- ` array`
1147
+ - ` map`
1147
1148
1148
1149
:::note
1149
1150
1150
- Maps are not supported by default, but there is an
1151
- [experiment][map-variables] that can be enabled to add support. If
1152
- you're interested in this functionality, we would appreciate your feedback.
1153
- :pray :
1151
+ Defining a map requires that you use a special `map` subkey (see example below).
1154
1152
1155
- In the meantime, it is technically possible to define a map using a `ref` resolver and a templating function. For example :
1156
-
1157
- ` ` ` yaml
1158
- version: '3'
1159
-
1160
- tasks:
1161
- task-with-map:
1162
- vars:
1163
- FOO:
1164
- ref: dict "a" "1" "b" "2" "c" "3"
1165
- cmds:
1166
- - echo {{.FOO}}
1167
- ` ` `
1168
-
1169
- ` ` ` txt
1170
- map[a:1 b:2 c:3]
1171
- ` ` `
1172
-
1173
- OR by using the same technique with JSON :
1153
+ :: :
1174
1154
1175
1155
` ` ` yaml
1176
- version: '3'
1156
+ version: 3
1177
1157
1178
1158
tasks:
1179
- task-with-map :
1159
+ foo :
1180
1160
vars:
1181
- JSON: '{"a": 1, "b": 2, "c": 3}'
1182
- FOO:
1183
- ref: "fromJson .JSON"
1184
- cmds:
1185
- - echo {{.FOO}}
1161
+ STRING: 'Hello, World!'
1162
+ BOOL: true
1163
+ INT: 42
1164
+ FLOAT: 3.14
1165
+ ARRAY: [1, 2, 3]
1166
+ MAP:
1167
+ map: {A: 1, B: 2, C: 3}
1168
+ cmds:
1169
+ - 'echo {{.STRING}}' # Hello, World!
1170
+ - 'echo {{.BOOL}}' # true
1171
+ - 'echo {{.INT}}' # 42
1172
+ - 'echo {{.FLOAT}}' # 3.14
1173
+ - 'echo {{.ARRAY}}' # [1 2 3]
1174
+ - 'echo {{.ARRAY.0}}' # 1
1175
+ - 'echo {{.MAP}}' # map[A:1 B:2 C:3]
1176
+ - 'echo {{.MAP.A}}' # 1
1186
1177
` ` `
1187
1178
1188
- ` ` ` txt
1189
- map[a:1 b:2 c:3]
1190
- ` ` `
1191
-
1192
- :: :
1193
-
1194
1179
Variables can be set in many places in a Taskfile. When executing
1195
1180
[templates][templating-reference], Task will look for variables in the order
1196
1181
listed below (most important first) :
@@ -1391,6 +1376,29 @@ tasks:
1391
1376
- 'echo {{.FOO}}' # <-- FOO is just the letter 'A'
1392
1377
` ` `
1393
1378
1379
+ # ## Parsing JSON/YAML into map variables
1380
+
1381
+ If you have a raw JSON or YAML string that you want to process in Task, you can
1382
+ use a combination of the `ref` keyword and the `fromJson` or `fromYaml`
1383
+ templating functions to parse the string into a map variable. For example :
1384
+
1385
+ ` ` ` yaml
1386
+ version: '3'
1387
+
1388
+ tasks:
1389
+ task-with-map:
1390
+ vars:
1391
+ JSON: '{"a": 1, "b": 2, "c": 3}'
1392
+ FOO:
1393
+ ref: "fromJson .JSON"
1394
+ cmds:
1395
+ - echo {{.FOO}}
1396
+ ` ` `
1397
+
1398
+ ` ` ` txt
1399
+ map[a:1 b:2 c:3]
1400
+ ` ` `
1401
+
1394
1402
# # Looping over values
1395
1403
1396
1404
Task allows you to loop over certain values and execute a command for each.
@@ -1518,7 +1526,7 @@ tasks:
1518
1526
cmd: cat {{.ITEM}}
1519
1527
` ` `
1520
1528
1521
- You can also loop over arrays directly and maps :
1529
+ You can also loop over arrays and maps directly :
1522
1530
1523
1531
` ` ` yaml
1524
1532
version: 3
@@ -2327,6 +2335,5 @@ if called by another task, either directly or as a dependency.
2327
2335
2328
2336
{/* prettier-ignore-start */}
2329
2337
[gotemplate] : https://golang.org/pkg/text/template/
2330
- [map-variables] : ./experiments/map_variables.mdx
2331
2338
[templating-reference] : ./reference/templating.mdx
2332
2339
{/* prettier-ignore-end */}
0 commit comments