-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0012-preprocess-Read-JSON-files-through-Jsonnet-compiler.patch
1446 lines (1443 loc) · 39.6 KB
/
0012-preprocess-Read-JSON-files-through-Jsonnet-compiler.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From: Oliver Reiche <[email protected]>
Date: Sat, 23 Dec 2023 15:04:32 +0100
Subject: [preprocess] Read JSON files through Jsonnet compiler
---
.../build_engine/base_maps/json_file_map.hpp | 2 +-
src/buildtool/file_system/TARGETS | 3 +-
src/buildtool/main/main.cpp | 2 +-
src/jsonnet/TARGETS | 23 +
src/jsonnet/builtins.libsonnet | 1359 +++++++++++++++++
5 files changed, 1386 insertions(+), 3 deletions(-)
create mode 100644 src/jsonnet/TARGETS
create mode 100644 src/jsonnet/builtins.libsonnet
diff --git a/src/buildtool/build_engine/base_maps/json_file_map.hpp b/src/buildtool/build_engine/base_maps/json_file_map.hpp
--- a/src/buildtool/build_engine/base_maps/json_file_map.hpp
+++ b/src/buildtool/build_engine/base_maps/json_file_map.hpp
@@ -95,7 +95,7 @@ auto CreateJsonFileMap(
return;
}
- auto const file_content = root->ReadContent(json_file_path);
+ auto file_content = root->ReadJSON(json_file_path);
if (not file_content) {
(*logger)(fmt::format("cannot read JSON file {}.",
json_file_path.string()),
diff --git a/src/buildtool/file_system/TARGETS b/src/buildtool/file_system/TARGETS
--- a/src/buildtool/file_system/TARGETS
+++ b/src/buildtool/file_system/TARGETS
@@ -163,7 +163,8 @@
, "file_root":
{ "type": ["@", "rules", "CC", "library"]
, "name": ["file_root"]
- , "hdrs": ["file_root.hpp", ["src/jsonnet", "builtins.h"]]
+ , "hdrs": ["file_root.hpp"]
+ , "private-hdrs": [["src/jsonnet", "builtins.h"]]
, "srcs": ["file_root.cpp"]
, "deps":
[ "file_system_manager"
diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp
--- a/src/buildtool/main/main.cpp
+++ b/src/buildtool/main/main.cpp
@@ -421,7 +421,7 @@ void StoreTargetCacheShard(
Logger::Log(LogLevel::Error, "Expected file at {}.", target_file);
std::exit(kExitFailure);
}
- auto file_content = target_root->ReadContent(target_file);
+ auto file_content = target_root->ReadJSON(target_file);
if (not file_content) {
Logger::Log(LogLevel::Error, "Cannot read file {}.", target_file);
std::exit(kExitFailure);
diff --git a/src/jsonnet/TARGETS b/src/jsonnet/TARGETS
new file mode 100644
index 00000000..19df5dee
--- /dev/null
+++ b/src/jsonnet/TARGETS
@@ -0,0 +1,23 @@
+{ "builtins.blob":
+ { "type": "generic"
+ , "deps": ["builtins.libsonnet"]
+ , "cmds":
+ [ "cp builtins.libsonnet x"
+ , "sed -i 's|//.*||g' x"
+ , "sed -i 's/^\\s*//g' x"
+ , "sed -i '/^\\s*$/d' x"
+ , "sed -i 's/\\\\/\\\\\\\\/g' x"
+ , "sed -i 's/\"/\\\\\"/g' x"
+ , "cat x | tr '\\n' ' ' > builtins.blob"
+ ]
+ , "outs": ["builtins.blob"]
+ }
+, "builtins.h":
+ { "type": "generic"
+ , "deps": ["builtins.blob"]
+ , "cmds":
+ [ "printf 'static auto constexpr kBuiltins = \"%s\";' \"$(cat builtins.blob)\" > builtins.h"
+ ]
+ , "outs": ["builtins.h"]
+ }
+}
diff --git a/src/jsonnet/builtins.libsonnet b/src/jsonnet/builtins.libsonnet
new file mode 100644
index 00000000..11fd2878
--- /dev/null
+++ b/src/jsonnet/builtins.libsonnet
@@ -0,0 +1,1359 @@
+/// # Mustbuild Language Extensions
+///
+/// The language extensions use the [Mustbuild preprocessor](./preprocessor.md)
+/// to generate JSON code that serves as a intermediate representation. This
+/// intermediate representation is used by the Mustbuild backend and is also
+/// identical to the [Justbuild expression
+/// language](https://github.com/just-buildsystem/justbuild/blob/master/doc/concepts/expressions.md).
+///
+/// There are two important particularities inherited from Justbuild:
+///
+/// 1. Literal maps `{}` are considered to be Justbuild expressions and
+/// therefore are not supported (unless explicitly stated otherwise). To
+/// create a map, please see the [`map()`](#map) utility function.
+/// 2. Every expression can be interpreted as a boolean for conditions. In that
+/// case, they are considered to be `true` if they do *not* evaluate to
+/// `null`, `false`, `0`, `''`, `[]`, or `{}`.
+///
+
+/// ## Expressions
+///
+/// Every Mustbuild expression is implemented as a disjoint mapping to one or
+/// more corresponding Justbuild expressions.
+///
+
+// helper functions
+local internal = {
+ unary(name, arg1):: {type: name, '$1': arg1},
+ binary(name, arg1, arg2):: {type: name, '$1': arg1, '$2': arg2},
+};
+
+/// ---
+///
+/// ### `var`
+///
+/// Access value of variable.
+///
+/// `var`(`name`: *str*, `default`: *expr*) -> *expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `name` | Name of the variable to read | None |
+/// | `default` | (optional) Expression to use if variable is not set | `null` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to value of variable 'a' or null if not set
+/// var('a')
+/// // evaluates to "foo" if variable 'a' is not set
+/// var('a', default='foo')
+/// ```
+///
+local var(name, default=null) = {
+ assert std.isString(name) : 'var() expects string literal argument for name',
+ type: 'var',
+ name: name,
+} + if default == null then {} else { default: default };
+
+/// ---
+///
+/// ### `let`
+///
+/// Sequentially set variables for use in specified expression.
+///
+/// `let`(`bindings`: *list[pair]*, `body`: *expr*) -> *expr*
+///
+/// Each *pair* is of the form [`var`: *str-expr*, `val`: *expr*].
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `bindings` | List of var-val pairs overlaying the environment | None |
+/// | `body` | Expression to evaluate in overlayed environment | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "foobar"
+/// let([ set('a', 'foo'),
+/// set('b', 'bar'),
+/// ],
+/// join([var('a'), var('b')]))
+/// ```
+///
+/// > See also [`set()`](#set) and [`join()`](#join).
+///
+local let(bindings, body) = {
+ assert std.isArray(bindings) : 'let() expects list argument for bindings',
+ local assert_bindings = [
+ if std.isArray(b) then
+ if std.length(b) == 2 then
+ if std.isString(b[0]) then b
+ else error 'let() expects first pair element to be string literal'
+ else error 'let() expects pairs of length 2'
+ else error 'let() expects literal pairs in bindings list'
+ for b in bindings
+ ],
+ type: 'let*',
+ bindings: bindings,
+ body: body,
+};
+
+/// ---
+///
+/// ### `select`
+///
+/// Select expression depending on condition.
+///
+/// `select`(`cond`: *expr*, `pass`: *expr*, `fail`: *expr*) -> *expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `cond` | Expression for condition | None |
+/// | `pass` | (optional) Expression to evaluate if `cond` succeeds | `[]` |
+/// | `fail` | (optional) Expression to evaluate if `cond` fails | `[]` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "pass" or "fail" (depending on variable 'a')
+/// select(var('a'), 'pass', 'fail')
+/// // evaluates to []
+/// select(false, 'pass')
+/// ```
+///
+local select(cond, pass=[], fail=[]) = {
+ type: 'if',
+ cond: cond,
+ 'then': pass,
+ 'else': fail,
+};
+
+/// ---
+///
+/// ### `cond`
+///
+/// Sequentially evaluate conditions and select expression of the first
+/// successful condition.
+///
+/// `cond`(`cond`: *list[pair]*, `default`: *expr*) -> *expr*
+///
+/// Each *pair* is of the form [`cond`: *expr*, `value`: *expr*], with
+/// `cond` being interpreted as a boolean.
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `cond` | List of cond-value pairs | None |
+/// | `default` | (optional) Expression to evaluate if no condition succeeds | `[]` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "pass" (value of first successful condition)
+/// cond([ [null, 'fail'],
+/// [true, 'pass'],
+/// [var('a'), 'unknown'],
+/// ],
+/// default='fallback')
+/// ```
+///
+local cond(cond, default=[]) = {
+ assert std.isArray(cond) : 'cond() expects list argument for cond',
+ local assert_cond = [
+ if std.isArray(c) then
+ if std.length(c) == 2 then c
+ else error 'cond() expects pairs of length 2'
+ else error 'cond() expects literal pairs in cond list'
+ for c in cond
+ ],
+ type: 'cond',
+ cond: cond,
+} + if default == [] then {} else { default: default };
+
+/// ---
+///
+/// ### `case`
+///
+/// Select expression from cases.
+///
+/// `case`(`expr`: *expr*, `case`: *map | list[pair]*, `default`: *expr*) -> *expr*
+///
+/// If cases are specified as a literal map, only string cases can be matched.
+/// If cases are specified as list of pairs, arbitrary expressions can be
+/// matched as cases sequentially. Each *pair* is of the form [`comp`: *expr*,
+/// `value`: *expr*].
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `expr` | Expression to evaluate for comparison | None |
+/// | `case` | Map for string comparison or pairs for arbitrary comparison | None |
+/// | `default` | (optional) Expression to evaluate if no comparision succeeds | `[]` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "pass" or "fail" (depending on variable 'a')
+/// case(select(var('a'), 'yes', 'no'),
+/// { yes: 'pass',
+/// no: 'fail',
+/// maybe: 'unknown',
+/// },
+/// default='fallback')
+/// // evaluates to "pass" or "fail" (depending on variable 'a')
+/// case(select(var('a'), true, null),
+/// [ [true, 'pass'],
+/// [null, 'fail'],
+/// ['maybe', 'unknown'],
+/// ],
+/// default='fallback')
+/// ```
+///
+local case(expr, case, default=[]) = {
+ assert std.isArray(case) || std.isObject(case)
+ : 'case() expects list or map argument for case',
+ local assert_case =
+ if std.isArray(case) then
+ [ if std.isArray(c) then
+ if std.length(c) == 2 then c
+ else error 'case() expects pairs of length 2'
+ else error 'case() expects literal pairs in case list'
+ for c in cond
+ ]
+ else case,
+ type: if std.isArray(case) then 'case*' else 'case',
+ expr: expr,
+ case: case,
+} + if default == [] then {} else { default: default };
+
+/// ---
+///
+/// ### `and`
+///
+/// Logical AND.
+///
+/// `and`(`conds`: *list-expr*) -> *bool-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `conds` | List of expressions or expression evaluating to list | None |
+///
+/// > Note that literal lists are subject to short-circuit evaluation.
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to true or false (depending on varable 'a')
+/// and([true, var('a')])
+/// ```
+///
+local and(conds) = {
+ assert std.isArray(conds) || std.isObject(conds)
+ : 'and() expects list of expressions or list-expression',
+ type: 'and',
+ '$1': conds,
+};
+
+/// ---
+///
+/// ### `or`
+///
+/// Logical OR.
+///
+/// `or`(`conds`: *list-expr*) -> *bool-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `conds` | List of expressions or expression evaluating to list | None |
+///
+/// > Note that literal lists are subject to short-circuit evaluation.
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to true or false (depending on varable 'a')
+/// or([false, var('a')])
+/// ```
+///
+local or(conds) = {
+ assert std.isArray(conds) || std.isObject(conds)
+ : 'or() expects list of expressions or list-expression',
+ type: 'or',
+ '$1': conds,
+};
+
+/// ---
+///
+/// ### `foreach`
+///
+/// Evaluate expression for each element in list.
+///
+/// `foreach`(`var`: *str*, `range`: *list-expr*, `body`: *expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `var` | Literal string used as iteration variable name | None |
+/// | `range` | Expression evaluating to list | None |
+/// | `body` | Expression to evaluate in each iteration | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["food","foot"]
+/// foreach('x', ['d', 't'], join(['foo', var('x')]))
+/// ```
+///
+/// > See also [`join()`](#join).
+///
+local foreach(var, range, body) = {
+ assert std.isString(var) : 'foreach() expects literal string for var',
+ type: 'foreach',
+ range: range,
+ var: var,
+ body: body,
+};
+
+/// ---
+///
+/// ### `foreach_map`
+///
+/// Evaluate expression for each field in map.
+///
+/// `foreach_map`(`var`: *str*, `var_val`: *str*, `range`: *map-expr*, `body`: *expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `var` | Literal string used as iteration variable name for keys | None |
+/// | `var_val` | Literal string used as iteration variable name for values | None |
+/// | `range` | Expression evaluating to map | None |
+/// | `body` | Expression to evaluate in each iteration | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["a:x","b:y"]
+/// foreach_map('k', 'v', map({'a':'x','b':'y'})],
+/// join([var('k'), ':', var('v')]))
+/// ```
+///
+/// > See also [`map()`](#map) and [`join()`](#join).
+///
+local foreach_map(var, var_val, range, body) = {
+ assert std.isString(var) : 'foreach() expects literal string for var',
+ assert std.isString(var_val) : 'foreach() expects literal string for var_val',
+ type: 'foreach',
+ range: range,
+ var_key: var,
+ var_val: var_val,
+ body: body,
+};
+
+/// ---
+///
+/// ### `foldl`
+///
+/// Sequentially evaluate expression for each element in list. The result of
+/// each evaluation is assigned to an accumulation variable.
+///
+/// `foldl`(`var`: *str*, `var_acc`: *str*, `range`: *list-expr*, `start`: *expr*, `body`: *expr*) -> *expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `var` | Literal string used as iteration variable name | None |
+/// | `var_acc` | Literal string used as accumulation variable name | None |
+/// | `range` | Expression evaluating to list | None |
+/// | `start` | Expression to evaluate the accumulation start value | None |
+/// | `body` | Expression to evaluate the next accumulation value | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "foobarbaz"
+/// foldl('x', 'acc', ['bar', 'baz'],
+/// start='foo',
+/// body=join([var('acc'), var('x')]))
+/// ```
+///
+/// > See also [`join()`](#join).
+///
+local foldl(var, var_acc, range, start, body) = {
+ assert std.isString(var) : 'foldl() expects literal string for var',
+ assert std.isString(var_acc) : 'foldl() expects literal string for var_acc',
+ type: 'foldl',
+ range: range,
+ var: var,
+ accum_var: var_acc,
+ start: start,
+ body: body,
+};
+
+/// ---
+///
+/// ### `not`
+///
+/// Negate boolean expression.
+///
+/// `not`(`cond`: *expr*) -> *bool-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `cond` | Expression to negate its boolean value | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to true
+/// not(null)
+/// ```
+///
+local not(cond) = internal.unary('not', cond);
+
+/// ---
+///
+/// ### `nub_right`
+///
+/// Remove duplicates from list and retain only the right-most one.
+///
+/// `nub_right`(`list`: *list-expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `list` | List-expression to remove left-most duplicates from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["foo","baz","bar"]
+/// nub_right(['foo', 'bar', 'baz', 'bar', 'bar'])
+/// ```
+///
+local nub_right(list) = internal.unary('nub_right', list);
+
+/// ---
+///
+/// ### `basename`
+///
+/// Get basename from string.
+///
+/// `basename`(`path`: *str-expr*) -> *str-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `path` | String-expression to extract file name from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "bar.baz"
+/// basename('foo/bar.baz')
+/// ```
+///
+local basename(path) = internal.unary('basename', path);
+
+/// ---
+///
+/// ### `keys`
+///
+/// Get keys from map as list.
+///
+/// `keys`(`map`: *map-expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `map` | Map-expression to read keys from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["a","b"]
+/// keys(map({a:'x',b:'y'}))
+/// ```
+///
+/// > See also [`map()`](#map).
+///
+local keys(map) = internal.unary('keys', map);
+
+/// ---
+///
+/// ### `values`
+///
+/// Get values from map as list.
+///
+/// `values`(`map`: *map-expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `map` | Map-expression to read values from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["x","y"]
+/// values(map({a:'x',b:'y'}))
+/// ```
+///
+/// > See also [`map()`](#map).
+///
+local values(map) = internal.unary('values', map);
+
+/// ---
+///
+/// ### `range`
+///
+/// Generate range from length as number or string. String must be the decimal
+/// representation of an integer.
+///
+/// `range`(`num`: *number | str-expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `num` | Number or string-expression to generate the range list from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["0","1","2"]
+/// range(3.0)
+/// // evaluates to ["0","1","2"]
+/// range("3")
+/// ```
+///
+local range(num) = internal.unary('range', num);
+
+/// ---
+///
+/// ### `map_env`
+///
+/// Generate map from variables.
+///
+/// `map_env`(`vars`: *list[str]*) -> *map-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `vars` | List of environment variable names to create a map from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {"a":"x","b":"y"}
+/// let([ set('a', 'x'),
+/// set('b', 'y')
+/// ],
+/// map_env(['a', 'b']))
+/// ```
+///
+/// > See also [`let()`](#let).
+///
+local map_env(vars) = {
+ assert std.isArray(vars) : 'env() expects list argument for vars',
+ local assert_vars = [
+ if std.isString(v) then v
+ else error 'env() expects sting literals in vars'
+ for v in vars
+ ],
+ type: 'env',
+ vars: vars,
+};
+
+/// ---
+///
+/// ### `map_enum`
+///
+/// Generate map with values from list. All keys are the decimal representation
+/// of the position in the list, padded with leading zeros to length 10.
+///
+/// `map_enum`(`list`: *list-expr*) -> *map-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `list` | List-expression to generate the map from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {"0000000000":"a","0000000001":"b"}
+/// map_enum(['a', 'b'])
+/// ```
+///
+local map_enum(list) = internal.unary('enumerate', list);
+
+/// ---
+///
+/// ### `map_set`
+///
+/// Generate map with keys from list. All values are set to `true`.
+///
+/// `map_set`(`keys`: *list-expr*) -> *map-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `keys` | List-expression with key names to generate the map from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {"a":true,"b":true}
+/// map_set(['a', 'b'])
+/// ```
+///
+local map_set(keys) = internal.unary('set', keys);
+
+/// ---
+///
+/// ### `reverse`
+///
+/// Reverse list.
+///
+/// `reverse`(`list`: *list-expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `list` | List-expression to reverse | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["c","b","a"]
+/// reverse(['a', 'b', 'c'])
+/// ```
+///
+local reverse(list) = internal.unary('reverse', list);
+
+/// ---
+///
+/// ### `length`
+///
+/// Get length of list.
+///
+/// `length`(`list`: *list-expr*) -> *num-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `list` | List-expression to get length of | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to 3
+/// length(['a', 'b', 'c'])
+/// ```
+///
+local length(list) = internal.unary('length', list);
+
+/// ---
+///
+/// ### `flatten`
+///
+/// Concatenate lists.
+///
+/// `flatten`(`lists`: *list-expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `lists` | List-expression containing the lists to concatenate | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ["a","b","c","d"]
+/// flatten([ ['a', 'b'], ['c', 'd'] ])
+/// ```
+///
+local flatten(lists) = internal.unary('++', lists);
+
+/// ---
+///
+/// ### `map_union`
+///
+/// Combine maps with the union of their fields.
+///
+/// `map_union`(`maps`: *list-expr*, `disjoint`: *bool*, `msg`: *expr*) -> *list-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `lists` | List-expression containing the maps to unify | None |
+/// | `disjoint` | (optional) Bool indicating that maps should be disjoint | `false` |
+/// | `msg` | (optional) Expression stringified as error message on conflict | `null` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {"a":"x","b":"y"}
+/// map_union([ map({a:'x'}), map({b:'y'} ])
+/// // evaluation fails with specified 'error message'
+/// map_union([ map({a:'x'}), map({a:'y'} ], disjoint=true, msg='error message')
+/// ```
+///
+/// > See also [`map()`](#map).
+///
+local map_union(maps, disjoint=false, msg=null) =
+ if !std.isBoolean(disjoint) then
+ error 'map_union() expects bool for disjoint'
+ else
+ internal.unary(
+ if disjoint then 'disjoint_map_union' else 'map_union', maps)
+ + if !disjoint || msg==null then {} else {msg: msg};
+
+/// ---
+///
+/// ### `sum`
+///
+/// Compute sum from list of numbers.
+///
+/// `sum`(`nums`: *list-expr*) -> *num-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `nums` | List-expression containing numbers | None |
+///
+/// > Note that sum of the empty list is the neutral element `0`.
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to 0
+/// sum([])
+/// // evaluates to 6
+/// sum([4, 2])
+/// ```
+///
+local sum(nums) = internal.unary('+', nums);
+
+/// ---
+///
+/// ### `prod`
+///
+/// Compute product from list of numbers.
+///
+/// `prod`(`nums`: *list-expr*) -> *num-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `nums` | List-expression containing numbers | None |
+///
+/// > Note that product of the empty list is the neutral element `1`.
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to 1
+/// prod([])
+/// // evaluates to 8
+/// prod([4, 2])
+/// ```
+///
+local prod(nums) = internal.unary('*', nums);
+
+/// ---
+///
+/// ### `join_cmd`
+///
+/// Join strings with shell quoting (POSIX shell can decode original list).
+///
+/// `join_cmd`(`args`: *list-expr*) -> *str-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `args` | List-expression containing the command arguments to join | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "'echo' 'foo' ''\''bar'\'' baz'"
+/// join_cmd(['echo' 'foo', "'bar' baz"])
+/// ```
+///
+local join_cmd(args) = internal.unary('join_cmd', args);
+
+/// ---
+///
+/// ### `json_encode`
+///
+/// Serialize JSON to string.
+///
+/// `json_encode`(`data`: *expr*) -> *str-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `data` | Expression containing the command arguments to join | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "[\"foo\",\"bar\"]"
+/// json_encode(['foo','bar'])
+/// ```
+///
+local json_encode(data) = internal.unary('json_encode', data);
+
+/// ---
+///
+/// ### `change_ending`
+///
+/// Change extension of a filename component.
+///
+/// `change_ending`(`path`: *str-expr*, `ending`: *str-expr*) -> *str-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `path` | String-expression of which to change the ending | None |
+/// | `ending` | String-expression specifying the new ending | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "src/main.o"
+/// change_ending('src/main.c', '.o')
+/// ```
+///
+local change_ending(path, ending) =
+ internal.unary('change_ending', path) + {ending: ending};
+
+/// ---
+///
+/// ### `join`
+///
+/// Join strings with optional separator.
+///
+/// `join`(`strings`: *list-expr*, `sep`: *str-expr*) -> *str-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `strings` | List-expression containing strings to concatenate | None |
+/// | `sep` | (optional) String-expression used as concatenation separator | `''` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to "foo,bar"
+/// join(['foo', 'bar'], ',')
+/// ```
+///
+local join(strings, sep='') =
+ internal.unary('join', strings) + {separator: sep};
+
+/// ---
+///
+/// ### `escape_chars`
+///
+/// Escape charaters from string.
+///
+/// `escape_chars`(`string`: *str-expr*, `chars`: *list-expr*, `escape`: *str-expr*) -> *str-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `string` | String-expression specifying the input string | None |
+/// | `chars` | List-expression specifying the characters to escape | None |
+/// | `escape` | (optional) String-expression specifying the escape character | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to ",foo,bar"
+/// escape_chars('foobar', ['f', 'b'], ',')
+/// ```
+///
+local escape_chars(string, chars, escape='\\') =
+ internal.unary('escape_chars', string) + {chars: chars, escape_prefix: escape};
+
+/// ---
+///
+/// ### `to_subdir`
+///
+/// Interpret keys of map as paths and change their prefix.
+///
+/// `to_subdir`(`subdir`: *str-expr*, `map`: *map-expr*, `flat`: *expr*, `msg`: *expr*) -> *map-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `subdir` | String-expression specifying the path prefixed to keys | None |
+/// | `map` | Map-expression containin the keys to modify | None |
+/// | `flat` | (optional) Expression boolean foring replacement keys' dirname | `false` |
+/// | `msg` | (optional) Expression stringified as error message on conflict | `null` |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {"foo/a/b":"xy"}
+/// to_subdir('foo', map({'a/b':'xy'}))
+/// // evaluates to {"foo/b":"xy"}
+/// to_subdir('foo', map({'a/b':'xy'}), flat=true, msg='conflict error')
+/// // evaluation fails with specified 'error message'
+/// to_subdir('foo', map({'a/b':'xy','c/b':'yz'}), flat=true, msg='conflict error')
+/// ```
+///
+/// > See also [`map()`](#map).
+///
+local to_subdir(subdir, map, flat=false, msg=null) =
+ internal.unary('to_subdir', map) + {subdir: subdir, flat: flat}
+ + if !flat || msg == null then {} else {msg: msg};
+
+/// ---
+///
+/// ### `eq`
+///
+/// Compare expressions for equality.
+///
+/// `eq`(`lhs`: *expr*, `rhs`: *expr*) -> *bool-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `lhs` | Left-hand side expression of comparison | None |
+/// | `rhs` | Right-hand side expression of comparison | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to true if variable 'a' is not set
+/// eq(var('a'), null)
+/// ```
+///
+local eq(lhs, rhs) = internal.binary('==', lhs, rhs);
+
+/// ---
+///
+/// ### `empty_map`
+///
+/// Generates an empty map. See also [`map()`](#map).
+///
+/// `empty_map`() -> *map-expr*
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {}
+/// empty_map()
+/// ```
+///
+local empty_map() = {type: 'empty_map'};
+
+/// ---
+///
+/// ### `singleton_map`
+///
+/// Generates a map from key and value. See also [`map()`](#map).
+///
+/// `singleton_map`(`key`: *str-expr*, `value`: *expr*) -> *map-expr*
+///
+/// | Argument | Description | Default value |
+/// |-|:-|:-:|
+/// | `key` | String-expression used as key to create map from | None |
+/// | `value` | String-expression used as value to create map from | None |
+///
+/// Example:
+/// ```jsonnet
+/// // evaluates to {"foo":"bar"}
+/// singleton_map('foo', 'bar')
+/// ```
+///