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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
|
local html = import 'html.libsonnet';
{
intro: html.paragraphs([
|||
This page describes the functions available in Jsonnet's standard library, i.e. the object
implicitly bound to the <code>std</code> variable. Some of the standard library functions
can be implemented in Jsonnet. Their code can be found in the <tt>std.jsonnet</tt> file.
The behavior of some of the other functions, i.e. the ones that expose extra functionality
not otherwise available to programmers, is described formally in the <a href="/language/spec.html">specification</a>.
|||,
|||
The standard library is implicitly added to all Jsonnet programs by enclosing them in a
local construct. For example, if the program given by the user is <code>{x: "foo"}</code>,
then the actual code executed would be <code>local std = { ... }; {x: "foo"}</code>. The
functions in the standard library are all hidden fields of the <code>std</code> object.
|||,
]),
prefix: 'std',
groups: [
{
name: 'External Variables',
id: 'ext_vars',
fields: [
{
name: 'extVar',
params: ['x'],
description: 'If an external variable with the given name was defined, return its string value. Otherwise, raise an error.',
},
],
},
{
name: 'Types and Reflection',
id: 'types_reflection',
fields: [
{
name: 'thisFile',
description: 'Note that this is a field. It contains the current Jsonnet filename as a string.',
},
{
name: 'type',
params: ['x'],
description: html.paragraphs([
|||
Return a string that indicates the type of the value. The possible return values are:
"array", "boolean", "function", "null", "number", "object", and "string".
|||,
|||
The following functions are also available and return a boolean:
<code>std.isArray(v)</code>, <code>std.isBoolean(v)</code>, <code>std.isFunction(v)</code>,
<code>std.isNumber(v)</code>, <code>std.isObject(v)</code>, and
<code>std.isString(v)</code>.
|||,
]),
},
{
name: 'length',
params: ['x'],
description: |||
Depending on the type of the value given, either returns the number of elements in the
array, the number of codepoints in the string, the number of parameters in the function, or
the number of fields in the object. Raises an error if given a primitive value, i.e.
<code>null</code>, <code>true</code> or <code>false</code>.
|||,
},
{
name: 'objectHas',
params: ['o', 'f'],
description: |||
Returns <code>true</code> if the given object has the field (given as a string), otherwise
<code>false</code>. Raises an error if the arguments are not object and string
respectively. Returns false if the field is hidden.
|||,
},
{
name: 'objectFields',
params: ['o'],
description: |||
Returns an array of strings, each element being a field from the given object. Does not include
hidden fields.
|||,
},
{
name: 'objectValues',
params: ['o'],
availableSince: '0.17.0',
description: |||
Returns an array of the values in the given object. Does not include hidden fields.
|||,
},
{
name: 'objectHasAll',
params: ['o', 'f'],
description: |||
As <code>std.objectHas</code> but also includes hidden fields.
|||,
},
{
name: 'objectFieldsAll',
params: ['o'],
description: |||
As <code>std.objectFields</code> but also includes hidden fields.
|||,
},
{
name: 'objectValuesAll',
params: ['o'],
availableSince: '0.17.0',
description: |||
As <code>std.objectValues</code> but also includes hidden fields.
|||,
},
{
name: 'prune',
params: ['a'],
description: |||
Recursively remove all "empty" members of <code>a</code>. "Empty" is defined as zero
length `arrays`, zero length `objects`, or `null` values.
The argument <code>a</code> may have any type.
|||,
},
{
name: 'mapWithKey',
params: ['func', 'obj'],
description: |||
Apply the given function to all fields of the given object, also passing
the field name. The function <code>func</code> is expected to take the
field name as the first parameter and the field value as the second.
|||,
},
],
},
{
name: 'Mathematical Utilities',
id: 'math',
intro: [
|||
<p>
The following mathematical functions are available:
</p>
<ul>
<ul><code>std.abs(n)</code></ul>
<ul><code>std.sign(n)</code></ul>
<ul><code>std.max(a, b)</code></ul>
<ul><code>std.min(a, b)</code></ul>
<ul><code>std.pow(x, n)</code></ul>
<ul><code>std.exp(x)</code></ul>
<ul><code>std.log(x)</code></ul>
<ul><code>std.exponent(x)</code></ul>
<ul><code>std.mantissa(x)</code></ul>
<ul><code>std.floor(x)</code></ul>
<ul><code>std.ceil(x)</code></ul>
<ul><code>std.sqrt(x)</code></ul>
<ul><code>std.sin(x)</code></ul>
<ul><code>std.cos(x)</code></ul>
<ul><code>std.tan(x)</code></ul>
<ul><code>std.asin(x)</code></ul>
<ul><code>std.acos(x)</code></ul>
<ul><code>std.atan(x)</code></ul>
</ul>
<p>
The function <code>std.mod(a, b)</code> is what the % operator is desugared to. It performs
modulo arithmetic if the left hand side is a number, or if the left hand side is a string,
it does Python-style string formatting with <code>std.format()</code>.
</p>
|||,
],
fields: [
{
name: 'std.clamp',
params: ['x', 'minVal', 'maxVal'],
availableSince: '0.15.0',
description: |||
Clamp a value to fit within the range [<code>minVal</code>, <code>maxVal</code>].
Equivalent to <code>std.max(minVal, std.min(x, maxVal))</code>.
|||,
examples: [
{
input: 'std.clamp(-3, 0, 5)',
output: std.clamp(-3, 0, 5),
},
{
input: 'std.clamp(4, 0, 5)',
output: std.clamp(4, 0, 5),
},
{
input: 'std.clamp(7, 0, 5)',
output: std.clamp(7, 0, 5),
},
],
},
],
},
{
name: 'Assertions and Debugging',
id: 'assertions_debugging',
fields: [
{
name: 'assertEqual',
params: ['a', 'b'],
description: 'Ensure that <code>a == b</code>. Returns <code>true</code> or throws an error message.',
},
],
},
{
name: 'String Manipulation',
id: 'string',
fields: [
{
name: 'toString',
params: ['a'],
description: |||
Convert the given argument to a string.
|||,
},
{
name: 'codepoint',
params: ['str'],
description: |||
Returns the positive integer representing the unicode codepoint of the character in the
given single-character string. This function is the inverse of <code>std.char(n)</code>.
|||,
},
{
name: 'char',
params: ['n'],
description: |||
Returns a string of length one whose only unicode codepoint has integer id <code>n</code>.
This function is the inverse of <code>std.codepoint(str)</code>.
|||,
},
{
name: 'substr',
params: ['str', 'from', 'len'],
description: |||
Returns a string that is the part of <code>s</code> that starts at offset <code>from</code>
and is <code>len</code> codepoints long. If the string <code>s</code> is shorter than
<code>from+len</code>, the suffix starting at position <code>from</code> will be returned.
|||,
},
{
name: 'findSubstr',
params: ['pat', 'str'],
description: |||
Returns an array that contains the indexes of all occurances of <code>pat</code> in
<code>str</code>.
|||,
},
{
name: 'startsWith',
params: ['a', 'b'],
description: |||
Returns whether the string a is prefixed by the string b.
|||,
},
{
name: 'endsWith',
params: ['a', 'b'],
description: |||
Returns whether the string a is suffixed by the string b.
|||,
},
{
name: 'stripChars',
params: ['str', 'chars'],
availableSince: '0.15.0',
description: |||
Removes characters <code>chars</code> from the beginning and from the end of <code>str</code>.
|||,
examples: [
{
input: 'std.stripChars(" test test test ", " ")',
output: std.stripChars(' test test test ', ' '),
},
{
input: 'std.stripChars("aaabbbbcccc", "ac")',
output: std.stripChars('aaabbbbcccc', 'ac'),
},
{
input: 'std.stripChars("cacabbbbaacc", "ac")',
output: std.stripChars('cacabbbbaacc', 'ac'),
},
],
},
{
name: 'lstripChars',
params: ['str', 'chars'],
availableSince: '0.15.0',
description: |||
Removes characters <code>chars</code> from the beginning of <code>str</code>.
|||,
examples: [
{
input: 'std.lstripChars(" test test test ", " ")',
output: std.lstripChars(' test test test ', ' '),
},
{
input: 'std.lstripChars("aaabbbbcccc", "ac")',
output: std.lstripChars('aaabbbbcccc', 'ac'),
},
{
input: 'std.lstripChars("cacabbbbaacc", "ac")',
output: std.lstripChars('cacabbbbaacc', 'ac'),
},
],
},
{
name: 'rstripChars',
params: ['str', 'chars'],
availableSince: '0.15.0',
description: |||
Removes characters <code>chars</code> from the end of <code>str</code>.
|||,
examples: [
{
input: 'std.rstripChars(" test test test ", " ")',
output: std.rstripChars(' test test test ', ' '),
},
{
input: 'std.rstripChars("aaabbbbcccc", "ac")',
output: std.rstripChars('aaabbbbcccc', 'ac'),
},
{
input: 'std.rstripChars("cacabbbbaacc", "ac")',
output: std.rstripChars('cacabbbbaacc', 'ac'),
},
],
},
{
name: 'split',
params: ['str', 'c'],
description: |||
Split the string <code>str</code> into an array of strings, divided by the single character
<code>c</code>.
|||,
examples: [
{
input: @'std.split("foo/bar", "/")',
output: std.split('foo/bar', '/'),
},
{
input: @'std.split("/foo/", "/")',
output: std.split('/foo/', '/'),
},
],
},
{
name: 'splitLimit',
params: ['str', 'c', 'maxsplits'],
description: |||
As std.split(str, c) but will stop after <code>maxsplits</code> splits, thereby the largest
array it will return has length <code>maxsplits + 1</code>. A limit of -1 means unlimited.
|||,
examples: [
{
input: @'std.splitLimit("foo/bar", "/", 1)',
output: std.splitLimit('foo/bar', '/', 1),
},
{
input: @'std.splitLimit("/foo/", "/", 1)',
output: std.splitLimit('foo/bar', '/', 1),
},
],
},
{
name: 'strReplace',
params: ['str', 'from', 'to'],
description: |||
Returns a copy of the string in which all occurrences of string <code>from</code> have been
replaced with string <code>to</code>.
|||,
examples: [
{
input: @"std.strReplace('I like to skate with my skateboard', 'skate', 'surf')",
output: std.strReplace('I like to skate with my skateboard', 'skate', 'surf'),
},
],
},
{
name: 'asciiUpper',
params: ['str'],
description: |||
Returns a copy of the string in which all ASCII letters are capitalized.
|||,
examples: [
{
input: "std.asciiUpper('100 Cats!')",
output: std.asciiUpper('100 Cats!'),
},
],
},
{
name: 'asciiLower',
params: ['str'],
description: |||
Returns a copy of the string in which all ASCII letters are lower cased.
|||,
examples: [
{
input: "std.asciiLower('100 Cats!')",
output: std.asciiLower('100 Cats!'),
},
],
},
{
name: 'stringChars',
params: ['str'],
description: |||
Split the string <code>str</code> into an array of strings, each containing a single
codepoint.
|||,
examples: [
{
input: 'std.stringChars("foo")',
output: std.stringChars('foo'),
},
],
},
{
name: 'format',
params: ['str', 'vals'],
description: |||
Format the string <code>str</code> using the values in <code>vals</code>. The values can be
an array, an object, or in other cases are treated as if they were provided in a singleton
array. The string formatting follows the <a
href="https://docs.python.org/2/library/stdtypes.html#string-formatting">same rules</a> as
Python. The <code>%</code> operator can be used as a shorthand for this function.
|||,
examples: [
{
input: 'std.format("Hello %03d", 12)',
output: std.format('Hello %03d', 12),
},
{
input: '"Hello %03d" % 12',
output: 'Hello %03d' % 12,
},
{
input: '"Hello %s, age %d" % ["Foo", 25]',
output: 'Hello %s, age %d' % ['Foo', 25],
},
{
input: '"Hello %(name)s, age %(age)d" % {age: 25, name: "Foo"}',
output: 'Hello %(name)s, age %(age)d' % { age: 25, name: 'Foo' },
},
],
},
{
name: 'escapeStringBash',
params: ['str'],
description: |||
Wrap <code>str</code> in single quotes, and escape any single quotes within <code>str</code>
by changing them to a sequence <tt>'"'"'</tt>. This allows injection of arbitrary strings
as arguments of commands in bash scripts.
|||,
},
{
name: 'escapeStringDollars',
params: ['str'],
description: |||
Convert $ to $$ in <code>str</code>. This allows injection of arbitrary strings into
systems that use $ for string interpolation (like Terraform).
|||,
},
{
name: 'escapeStringJson',
params: ['str'],
description: |||
Convert <code>str</code> to allow it to be embedded in a JSON representation, within a
string. This adds quotes, escapes backslashes, and escapes unprintable characters.
|||,
examples: [
{
input: |||
local description = "Multiline\nc:\\path";
"{name: %s}" % std.escapeStringJson(description)
|||,
output: (
local description = 'Multiline\nc:\\path';
'{name: %s}' % std.escapeStringJson(description)
),
},
],
},
{
name: 'escapeStringPython',
params: ['str'],
description: |||
Convert <code>str</code> to allow it to be embedded in Python. This is an alias for
<code>std.escapeStringJson</code>.
|||,
},
],
},
{
name: 'Parsing',
id: 'parsing',
fields: [
{
name: 'parseInt',
params: ['str'],
description: |||
Parses a signed decimal integer from the input string.
|||,
examples: [
{
input: 'std.parseInt("123")',
output: std.parseInt('123'),
},
{
input: 'std.parseInt("-123")',
output: std.parseInt('-123'),
},
],
},
{
name: 'parseOctal',
params: ['str'],
description: |||
Parses an unsigned octal integer from the input string. Initial zeroes are tolerated.
|||,
examples: [
{
input: 'std.parseOctal("755")',
output: std.parseOctal('755'),
},
],
},
{
name: 'parseHex',
params: ['str'],
description: |||
Parses an unsigned hexadecimal integer, from the input string. Case insensitive.
|||,
examples: [
{
input: 'std.parseHex("ff")',
output: std.parseHex('ff'),
},
],
},
{
name: 'parseJson',
availableSince: '0.13.0',
params: ['str'],
description: |||
Parses a JSON string.
|||,
examples: [
{
input: 'std.parseJson(\'{"foo": "bar"}\')',
output: std.parseJson('{"foo": "bar"}'),
},
],
},
{
name: 'encodeUTF8',
params: ['str'],
availableSince: '0.13.0',
description: |||
Encode a string using <a href="https://en.wikipedia.org/wiki/UTF-8">UTF8</a>. Returns an array of numbers
representing bytes.
|||,
},
{
name: 'decodeUTF8',
params: ['arr'],
availableSince: '0.13.0',
description: |||
Decode an array of numbers representing bytes using <a href="https://en.wikipedia.org/wiki/UTF-8">UTF8</a>.
Returns a string.
|||,
},
],
},
{
name: 'Manifestation',
id: 'manifestation',
// TODO(sbarzowski): Clean up the example's representation
fields: [
{
name: 'manifestIni',
params: ['ini'],
description: [
html.p({}, |||
Convert the given structure to a string in <a href="http://en.wikipedia.org/wiki/INI_file">INI format</a>. This
allows using Jsonnet's
object model to build a configuration to be consumed by an application expecting an INI
file. The data is in the form of a set of sections, each containing a key/value mapping.
These examples should make it clear:
|||),
html.pre({}, |||
{
main: { a: "1", b: "2" },
sections: {
s1: {x: "11", y: "22", z: "33"},
s2: {p: "yes", q: ""},
empty: {},
}
}
|||),
html.p({}, |||
Yields a string containing this INI file:
|||),
html.pre({}, |||
a = 1
b = 2
[empty]
[s1]
x = 11
y = 22
z = 33
[s2]
p = yes
q =
|||),
]
},
{
name: 'manifestPython',
params: ['v'],
description: [
html.p({}, |||
Convert the given value to a JSON-like form that is compatible with Python. The chief
differences are True / False / None instead of true / false / null.
|||),
html.pre({}, |||
{
b: ["foo", "bar"],
c: true,
d: null,
e: { f1: false, f2: 42 },
}
|||),
html.p({}, |||
Yields a string containing Python code like:
|||),
html.pre({}, |||
{
"b": ["foo", "bar"],
"c": True,
"d": None,
"e": {"f1": False, "f2": 42}
}
|||),
]
},
{
name: 'manifestPythonVars',
params: ['conf'],
description: [
html.p({}, |||
Convert the given object to a JSON-like form that is compatible with Python. The key
difference to <code>std.manifestPython</code> is that the top level is represented as a list
of Python global variables.
|||),
html.pre({}, |||
{
b: ["foo", "bar"],
c: true,
d: null,
e: { f1: false, f2: 42 },
}
|||),
html.p({}, |||
Yields a string containing this Python code:
|||),
html.pre({}, |||
b = ["foo", "bar"]
c = True
d = None
e = {"f1": False, "f2": 42}
|||),
],
},
{
name: 'manifestJsonEx',
params: ['value', 'indent'],
description: [
html.p({}, |||
Convert the given object to a JSON form. <code>indent</code> is a string containing
one or more whitespaces that are used for indentation:
|||),
html.pre({}, |||
std.manifestJsonEx(
{
x: [1, 2, 3, true, false, null,
"string\nstring"],
y: { a: 1, b: 2, c: [1, 2] },
}, " ")
|||),
html.p({}, |||
Yields a string containing this JSON object:
|||),
html.pre({}, |||
{
"x": [
1,
2,
3,
true,
false,
null,
"string\nstring"
],
"y": {
"a": 1,
"b": 2,
"c": [
1,
2
]
}
}
|||),
]
},
{
name: 'manifestYamlDoc',
params: ['value', 'indent_array_in_object=false'],
description: [
html.p({}, |||
Convert the given value to a YAML form. Note that <code>std.manifestJson</code> could also
be used for this purpose, because any JSON is also valid YAML. But this function will
produce more canonical-looking YAML.
|||),
html.pre({}, |||
std.manifestYamlDoc(
{
x: [1, 2, 3, true, false, null,
"string\nstring\n"],
y: { a: 1, b: 2, c: [1, 2] },
},
indent_array_in_object=false)
|||),
html.p({}, |||
Yields a string containing this YAML:
|||),
html.pre({}, |||
"x":
- 1
- 2
- 3
- true
- false
- null
- |
string
string
"y":
"a": 1
"b": 2
"c":
- 1
- 2
|||),
html.p({}, |||
The <code>indent_array_in_object</code> param adds additional indentation which some people
may find easier to read.
|||),
],
},
{
name: 'manifestYamlStream',
params: ['value', 'indent_array_in_object=false', 'c_document_end=false'],
description: [
html.p({}, |||
Given an array of values, emit a YAML "stream", which is a sequence of documents separated
by <code>---</code> and ending with <code>...</code>.
|||),
html.pre({}, |||
std.manifestYamlStream(
['a', 1, []],
indent_array_in_object=false,
c_document_end=true)
|||),
html.p({}, |||
Yields this string:
|||),
html.pre({}, |||
---
"a"
---
1
---
[]
...
|||),
html.p({}, |||
The <code>indent_array_in_object</code> param is the same as in <code>manifestYamlDoc</code>.
|||),
html.p({}, |||
The <code>c_document_end</code> param adds the optional terminating <code>...</code>.
|||),
],
},
{
name: 'manifestXmlJsonml',
params: ['value'],
description: [
html.p({}, |||
Convert the given <a href="http://www.jsonml.org/">JsonML</a>-encoded value to a string
containing the XML.
|||),
html.pre({}, |||
std.manifestXmlJsonml([
'svg', { height: 100, width: 100 },
[
'circle', {
cx: 50, cy: 50, r: 40,
stroke: 'black', 'stroke-width': 3,
fill: 'red',
}
],
])
|||),
html.p({}, |||
Yields a string containing this XML (all on one line):
|||),
html.pre({}, html.escape(|||
<svg height="100" width="100">
<circle cx="50" cy="50" fill="red" r="40"
stroke="black" stroke-width="3"></circle>;
</svg>;
|||)),
html.p({}, |||
Which represents the following image:
|||),
|||
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Sorry, your browser does not support inline SVG.
</svg>
|||,
html.p({}, |||
JsonML is designed to preserve "mixed-mode content" (i.e., textual data outside of or next
to elements). This includes the whitespace needed to avoid having all the XML on one line,
which is meaningful in XML. In order to have whitespace in the XML output, it must be
present in the JsonML input:
|||),
html.pre({}, |||
std.manifestXmlJsonml([
'svg',
{ height: 100, width: 100 },
'\n ',
[
'circle',
{
cx: 50, cy: 50, r: 40, stroke: 'black',
'stroke-width': 3, fill: 'red',
}
],
'\n',
])
|||),
],
},
],
},
{
name: 'Arrays',
id: 'arrays',
fields: [
{
name: 'makeArray',
params: ['sz', 'func'],
description: |||
Create a new array of <code>sz</code> elements by calling <code>func(i)</code> to initialize
each element. Func is expected to be a function that takes a single parameter, the index of
the element it should initialize.
|||,
examples: [
{
input: 'std.makeArray(3,function(x) x * x)',
output: std.makeArray(3, function(x) x * x),
},
],
},
{
name: 'member',
params: ['arr', 'x'],
availableSince: '0.15.0',
description: |||
Returns whether <code>x</code> occurs in <code>arr</code>.
Argument <code>arr</code> may be an array or a string.
|||,
},
{
name: 'count',
params: ['arr', 'x'],
description: |||
Return the number of times that <code>x</code> occurs in <code>arr</code>.
|||,
},
{
name: 'find',
params: ['value', 'arr'],
description: |||
Returns an array that contains the indexes of all occurances of <code>value</code> in
<code>arr</code>.
|||,
},
{
name: 'map',
params: ['func', 'arr'],
description: |||
Apply the given function to every element of the array to form a new array.
|||,
},
{
name: 'mapWithIndex',
params: ['func', 'arr'],
description: |||
Similar to <a href="#map">map</a> above, but it also passes to the function the element's
index in the array. The function <code>func</code> is expected to take the index as the
first parameter and the element as the second.
|||,
},
{
name: 'filterMap',
params: ['filter_func', 'map_func', 'arr'],
description: |||
It first filters, then maps the given array, using the two functions provided.
|||,
},
{
name: 'flatMap',
params: ['func', 'arr'],
description: html.paragraphs([
|||
Apply the given function to every element of <code>arr</code> to form a new array then flatten the result.
The argument <code>arr</code> must be an array or a string. If <code>arr</code> is an array, function <code>func</code> must return an array.
If <code>arr</code> is a string, function <code>func</code> must return an string.
|||,
|||
The <code>std.flatMap</code> function can be thought of as a generalized <code>std.map</code>,
with each element mapped to 0, 1 or more elements.
|||
]),
examples: [
{
input: 'std.flatMap(function(x) [x, x], [1, 2, 3])',
output: std.flatMap(function(x) [x, x], [1, 2, 3]),
},
{
input: 'std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3])',
output: std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3]),
},
{
input: 'std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3])',
output: std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3]),
},
{
input: 'std.flatMap(function(x) x+x, "foo")',
output: std.flatMap(function(x) x+x, "foo")
},
],
},
{
name: 'filter',
params: ['func', 'arr'],
description: |||
Return a new array containing all the elements of <code>arr</code> for which the
<code>func</code> function returns true.
|||,
},
{
name: 'foldl',
params: ['func', 'arr', 'init'],
description: |||
Classic foldl function. Calls the function on the result of the previous function call and
each array element, or <code>init</code> in the case of the initial element. Traverses the
array from left to right.
|||,
},
{
name: 'foldr',
params: ['func', 'arr', 'init'],
description: |||
Classic foldr function. Calls the function on the result of the previous function call and
each array element, or <code>init</code> in the case of the initial element. Traverses the
array from right to left.
|||,
},
{
name: 'range',
params: ['from', 'to'],
description: |||
Return an array of ascending numbers between the two limits, inclusively.
|||,
},
{
name: 'repeat',
params: ['what', 'count'],
availableSince: '0.15.0',
description: |||
Repeats an array or a string <code>what</code> a number of times specified by an integer <code>count</code>.
|||,
examples: [
{
input: 'std.repeat([1, 2, 3], 3)',
output: std.repeat([1, 2, 3], 3),
},
{
input: 'std.repeat("blah", 2)',
output: std.repeat('blah', 2),
},
],
},
{
name: 'slice',
params: ['indexable', 'index', 'end', 'step'],
description: html.paragraphs([
|||
Selects the elements of an array or a string from <code>index</code> to <code>end</code> with <code>step</code> and returns an array or a string respectively.
|||,
|||
Note that it's recommended to use dedicated slicing syntax both for arrays and strings (e.g. <code>arr[0:4:1]</code> instead of <code>std.slice(arr, 0, 4, 1)</code>).
|||,
]),
examples: [
{
input: 'std.slice([1, 2, 3, 4, 5, 6], 0, 4, 1)',
output: std.slice([1, 2, 3, 4, 5, 6], 0, 4, 1),
},
{
input: 'std.slice([1, 2, 3, 4, 5, 6], 1, 6, 2)',
output: std.slice([1, 2, 3, 4, 5, 6], 1, 6, 2),
},
{
input: 'std.slice("jsonnet", 0, 4, 1)',
output: std.slice('jsonnet', 0, 4, 1),
},
],
},
{
name: 'join',
params: ['sep', 'arr'],
description: |||
If <code>sep</code> is a string, then <code>arr</code> must be an array of strings, in which
case they are concatenated with <code>sep</code> used as a delimiter. If <code>sep</code>
is an array, then <code>arr</code> must be an array of arrays, in which case the arrays are
concatenated in the same way, to produce a single array.
|||,
examples: [
{
input: 'std.join(".", ["www", "google", "com"])',
output: std.join('.', ['www', 'google', 'com']),
},
{
input: 'std.join([9, 9], [[1], [2, 3]])',
output: std.join([9, 9], [[1], [2, 3]]),
},
],
},
{
name: 'lines',
params: ['arr'],
description: |||
Concatenate an array of strings into a text file with newline characters after each string.
This is suitable for constructing bash scripts and the like.
|||,
},
{
name: 'flattenArrays',
params: ['arr'],
description: |||
Concatenate an array of arrays into a single array.
|||,
examples: [
{
input: 'std.flattenArrays([[1, 2], [3, 4], [[5, 6], [7, 8]]])',
output: std.flattenArrays([[1, 2], [3, 4], [[5, 6], [7, 8]]]),
},
],
},
{
name: 'reverse',
params: ['arrs'],
availableSince: '0.13.0',
description: |||
Reverses an array.
|||,
},
{
name: 'sort',
params: ['arr', 'keyF=id'],
description: html.paragraphs([
|||
Sorts the array using the <= operator.
|||,
|||
Optional argument <code>keyF</code> is a single argument function used to extract comparison key from each array element.
Default value is identity function <code>keyF=function(x) x</code>.
|||,
]),
},
{
name: 'uniq',
params: ['arr', 'keyF=id'],
description: html.paragraphs([
|||
Removes successive duplicates. When given a sorted array, removes all duplicates.
|||,
|||
Optional argument <code>keyF</code> is a single argument function used to extract comparison key from each array element.
Default value is identity function <code>keyF=function(x) x</code>.
|||,
]),
},
],
},
{
name: 'Sets',
id: 'sets',
intro: html.paragraphs([
|||
Sets are represented as ordered arrays without duplicates.
|||,
|||
Note that the <code>std.set*</code> functions rely on the uniqueness and ordering
on arrays passed to them to work. This can be guarenteed by using <code>std.set(arr)</code>.
If that is not the case, the functions will quietly return non-meaningful results.
|||,
|||
All <code>set.set*</code> functions accept <code>keyF</code> function of one argument, which can be
used to extract key to use from each element. All Set operations then use extracted key for the purpose
of identifying uniqueness. Default value is identity function <code>local id = function(x) x</code>.
|||,
]),
fields: [
{
name: 'set',
params: ['arr', 'keyF=id'],
description: |||
Shortcut for std.uniq(std.sort(arr)).
|||,
},
{
name: 'setInter',
params: ['a', 'b', 'keyF=id'],
description: |||
Set intersection operation (values in both a and b).
|||,
},
{
name: 'setUnion',
params: ['a', 'b', 'keyF=id'],
description: |||
Set union operation (values in any of <code>a</code> or <code>b</code>). Note that + on sets will simply
concatenate
the arrays, possibly forming an array that is not a set (due to not being ordered without
duplicates).
|||,
examples: [
{
input: 'std.setUnion([1, 2], [2, 3])',
output: std.setUnion([1, 2], [2, 3]),
},
{
input: 'std.setUnion([{n:"A", v:1}, {n:"B"}], [{n:"A", v: 9999}, {n:"C"}], keyF=function(x) x.n)',
output: std.setUnion([{ n: 'A', v: 1 }, { n: 'B' }], [{ n: 'A', v: 9999 }, { n: 'C' }], keyF=function(x) x.n),
},
],
},
{
name: 'setDiff',
params: ['a', 'b', 'keyF=id'],
description: |||
Set difference operation (values in a but not b).
|||,
},
{
name: 'setMember',
params: ['x', 'arr', 'keyF=id'],
description: |||
Returns <code>true</code> if x is a member of array, otherwise <code>false</code>.
|||,
},
],
},
{
name: 'Encoding',
id: 'encoding',
fields: [
{
name: 'base64',
params: ['input'],
description: |||
Encodes the given value into a base64 string. The encoding sequence is <code>A-Za-z0-9+/</code> with
<code>=</code>
to pad the output to a multiple of 4 characters. The value can be a string or an array of
numbers, but the codepoints / numbers must be in the 0 to 255 range. The resulting string
has no line breaks.
|||,
},
{
name: 'base64DecodeBytes',
params: ['str'],
description: |||
Decodes the given base64 string into an array of bytes (number values). Currently assumes
the input string has no linebreaks and is padded to a multiple of 4 (with the = character).
In other words, it consumes the output of std.base64().
|||,
},
{
name: 'base64Decode',
params: ['str'],
description: html.paragraphs([
|||
<em>Deprecated, use <code>std.base64DecodeBytes</code> and decode the string explicitly (e.g. with <code>std.decodeUTF8</code>) instead.</code></em>
|||,
|||
Behaves like std.base64DecodeBytes() except returns a naively encoded string instead of an array of bytes.
|||,
]),
},
{
name: 'md5',
params: ['s'],
description: |||
Encodes the given value into an MD5 string.
|||,
},
],
},
{
name: 'JSON Merge Patch',
id: 'json_merge_patch',
fields: [
{
name: 'mergePatch',
params: ['target', 'patch'],
description: |||
Applies <code>patch</code> to <code>target</code>
according to <a href="https://tools.ietf.org/html/rfc7396">RFC7396</a>
|||,
},
],
},
{
name: 'Debugging',
id: 'debugging',
fields: [
{
name: 'trace',
params: ['target', 'patch'],
availableSince: '0.11.0',
description: [
html.p({}, |||
Outputs the given string <code>str</code> to stderr and
returns <code>rest</code> as the result.
|||),
html.p({}, |||
Example:
|||),
html.p({},
html.pre({}, |||
local conditionalReturn(cond, in1, in2) =
if (cond) then
std.trace('cond is true returning '
+ std.toString(in1), in1)
else
std.trace('cond is false returning '
+ std.toString(in2), in2);
{
a: conditionalReturn(true, { b: true }, { c: false }),
}
|||),
),
html.p({}, |||
Prints:
|||),
html.p({},
html.pre({}, |||
TRACE: test.jsonnet:3 cond is true returning {"b": true}
{
"a": {
"b": true
}
}
|||),
),
],
},
],
},
],
}
|