| 1 |
--- |
| 2 |
layout: default |
| 3 |
title: Tutorial |
| 4 |
--- |
| 5 |
|
| 6 |
<div class="hgroup"> |
| 7 |
<div class="hgroup-inline"> |
| 8 |
<div class="panel"> |
| 9 |
<h1 id="tutorial">Tutorial</h1> |
| 10 |
</div> |
| 11 |
<div style="clear: both"></div> |
| 12 |
</div> |
| 13 |
</div> |
| 14 |
|
| 15 |
<div class="hgroup"> |
| 16 |
<div class="hgroup-inline"> |
| 17 |
<div class="panel"> |
| 18 |
<p> |
| 19 |
Let's explore the various features in Jsonnet, and mix some cocktails. All example codes |
| 20 |
can be edited, allowing you to experience the language interactively. If you're looking at |
| 21 |
Jsonnet for the first time, this is the right place to be. For help using the |
| 22 |
implementation, see <a href="getting_started.html">getting started</a>. For a detailed |
| 23 |
systematic overview of the language, check out the <a |
| 24 |
href="/ref/language.html">reference</a>. |
| 25 |
</p> |
| 26 |
</div> |
| 27 |
<div style="clear: both"></div> |
| 28 |
</div> |
| 29 |
</div> |
| 30 |
|
| 31 |
<div class="hgroup"> |
| 32 |
<div class="hgroup-inline"> |
| 33 |
<div class="panel"> |
| 34 |
<h2 id="syntax">Syntax</h2> |
| 35 |
</div> |
| 36 |
<div style="clear: both"></div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
|
| 40 |
<div class="hgroup"> |
| 41 |
<div class="hgroup-inline"> |
| 42 |
<div class="panel"> |
| 43 |
<p> |
| 44 |
Any <a href="https://json.org">JSON</a> document is a valid Jsonnet program, so we'll focus |
| 45 |
on what Jsonnet adds to JSON. Let's start with an example that does not involve any |
| 46 |
computation but uses new syntax. |
| 47 |
</p> |
| 48 |
<ul> |
| 49 |
<li>Fields that happen to be valid identifiers have no quotes</li> |
| 50 |
<li><i>Trailing</i> commas at the end of arrays / objects</li> |
| 51 |
<li>Comments</li> |
| 52 |
<li> |
| 53 |
String literals use <code>"</code> or <code>'</code>. The single quote is easier on the |
| 54 |
eyes but either can be used to avoid escaping the other, e.g. <code>"Farmer's Gin"</code> |
| 55 |
instead of <code>'Farmer\'s Gin'</code>. |
| 56 |
</li> |
| 57 |
<li> |
| 58 |
Text blocks <code>|||</code> allow verbatim text across multiple lines. |
| 59 |
</li> |
| 60 |
<li> |
| 61 |
Verbatim strings <code>@'foo'</code> and <code>@"foo"</code> are for single lines. |
| 62 |
</li> |
| 63 |
</ul> |
| 64 |
<p> |
| 65 |
Using the interactive demo below, try modifying the strings / quantities. Try adding a "Dry |
| 66 |
Manhattan" which uses dry red vermouth and is garnished with a lemon slice. |
| 67 |
</p> |
| 68 |
</div> |
| 69 |
<div style="clear: both"></div> |
| 70 |
</div> |
| 71 |
</div> |
| 72 |
|
| 73 |
{% include demo.inc name="syntax" %} |
| 74 |
|
| 75 |
<div class="hgroup"> |
| 76 |
<div class="hgroup-inline"> |
| 77 |
<div class="panel"> |
| 78 |
<h2 id="variables">Variables</h2> |
| 79 |
</div> |
| 80 |
<div style="clear: both"></div> |
| 81 |
</div> |
| 82 |
</div> |
| 83 |
|
| 84 |
<div class="hgroup"> |
| 85 |
<div class="hgroup-inline"> |
| 86 |
<div class="panel"> |
| 87 |
<p> |
| 88 |
Variables are the simplest way to avoid duplication. |
| 89 |
</p> |
| 90 |
<ul> |
| 91 |
<li>The <code>local</code> keyword defines a variable.</li> |
| 92 |
<li>Variables defined next to fields end with a comma (<code>,</code>).</li> |
| 93 |
<li>All other cases end with a semicolon (<code>;</code>).</li> |
| 94 |
</ul> |
| 95 |
<p> |
| 96 |
Try factoring out <code>"Simple Syrup"</code> to the top level or to the same level as |
| 97 |
the <code>pour</code> variable. |
| 98 |
</p> |
| 99 |
</div> |
| 100 |
<div style="clear: both"></div> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
|
| 104 |
{% include demo.inc name="variables" %} |
| 105 |
|
| 106 |
<div class="hgroup"> |
| 107 |
<div class="hgroup-inline"> |
| 108 |
<div class="panel"> |
| 109 |
<h2 id="references">References</h2> |
| 110 |
</div> |
| 111 |
<div style="clear: both"></div> |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
|
| 115 |
<div class="hgroup"> |
| 116 |
<div class="hgroup-inline"> |
| 117 |
<div class="panel"> |
| 118 |
<p> |
| 119 |
Another way to avoid duplication is to refer to another part of the structure. |
| 120 |
</p> |
| 121 |
<ul> |
| 122 |
<li><code>self</code> refers to the current object.</li> |
| 123 |
<li><code>$</code> refers to the outer-most object.</li> |
| 124 |
<li><code>['foo']</code> looks up a field.</li> |
| 125 |
<li><code>.foo</code> can be used if the field name is an identifier.</li> |
| 126 |
<li><code>[10]</code> looks up an array element.</li> |
| 127 |
<li>Arbitrarily long paths are allowed.</li> |
| 128 |
<li>Array slices like <code>arr[10:20:2]</code> are allowed, like in Python.</li> |
| 129 |
<li>Strings can be looked up / sliced too, by unicode codepoint.</li> |
| 130 |
</ul> |
| 131 |
<p> |
| 132 |
Another name for a Tom Collins is a Gin Fizz. Try adding an alias for that. |
| 133 |
</p> |
| 134 |
</div> |
| 135 |
<div style="clear: both"></div> |
| 136 |
</div> |
| 137 |
</div> |
| 138 |
|
| 139 |
{% include demo.inc name="references" %} |
| 140 |
|
| 141 |
<div class="hgroup"> |
| 142 |
<div class="hgroup-inline"> |
| 143 |
<div class="panel"> |
| 144 |
<p> |
| 145 |
In order to refer to objects between the current and outer-most object, we use a variable to |
| 146 |
create a name for that level: |
| 147 |
</p> |
| 148 |
</div> |
| 149 |
<div style="clear: both"></div> |
| 150 |
</div> |
| 151 |
</div> |
| 152 |
|
| 153 |
{% include demo.inc name="inner-reference" %} |
| 154 |
|
| 155 |
<div class="hgroup"> |
| 156 |
<div class="hgroup-inline"> |
| 157 |
<div class="panel"> |
| 158 |
<h2 id="arithmetic">Arithmetic</h2> |
| 159 |
</div> |
| 160 |
<div style="clear: both"></div> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
|
| 164 |
<div class="hgroup"> |
| 165 |
<div class="hgroup-inline"> |
| 166 |
<div class="panel"> |
| 167 |
<p> |
| 168 |
Arithmetic includes numeric operations like multiplication but also various operations on |
| 169 |
other types. |
| 170 |
</p> |
| 171 |
<ul> |
| 172 |
<li>Use floating point arithmetic, bitwise ops, boolean logic.</li> |
| 173 |
<li> |
| 174 |
Strings may be concatenated with <code>+</code>, which implicitly converts |
| 175 |
one operand to string if needed. |
| 176 |
</li> |
| 177 |
<li>Two strings can be compared with <code><</code> (unicode codepoint order).</li> |
| 178 |
<li> |
| 179 |
Objects may be combined with <code>+</code> where the right-hand side wins field |
| 180 |
conflicts. |
| 181 |
</li> |
| 182 |
<li>Test if a field is in an object with <code>in</code>.</li> |
| 183 |
<li><code>==</code> is deep value equality.</li> |
| 184 |
<li>Python-compatible string formatting is available via <code>%</code>. When combined with |
| 185 |
<code>|||</code> this can be used for templating text files.</li> |
| 186 |
</ul> |
| 187 |
<p>Try dividing by zero to see what happens.</p> |
| 188 |
</div> |
| 189 |
<div style="clear: both"></div> |
| 190 |
</div> |
| 191 |
</div> |
| 192 |
|
| 193 |
{% include demo.inc name="arith" %} |
| 194 |
|
| 195 |
<div class="hgroup"> |
| 196 |
<div class="hgroup-inline"> |
| 197 |
<div class="panel"> |
| 198 |
<h2 id="functions">Functions</h2> |
| 199 |
</div> |
| 200 |
<div style="clear: both"></div> |
| 201 |
</div> |
| 202 |
</div> |
| 203 |
|
| 204 |
<div class="hgroup"> |
| 205 |
<div class="hgroup-inline"> |
| 206 |
<div class="panel"> |
| 207 |
<p> |
| 208 |
Like Python, functions have positional parameters, named parameters, and default arguments. |
| 209 |
Closures are also supported. The examples below should demonstrate the syntax. Many |
| 210 |
functions are already defined in the <a href=/ref/stdlib.html>standard library</a>. |
| 211 |
</p> |
| 212 |
<p> |
| 213 |
Try writing a function <code>is_even</code> that uses the modulo operator <code>%</code> and |
| 214 |
returns either true or false. |
| 215 |
</p> |
| 216 |
</div> |
| 217 |
<div style="clear: both"></div> |
| 218 |
</div> |
| 219 |
</div> |
| 220 |
|
| 221 |
{% include demo.inc name="functions" %} |
| 222 |
|
| 223 |
<div class="hgroup"> |
| 224 |
<div class="hgroup-inline"> |
| 225 |
<div class="panel"> |
| 226 |
<p> |
| 227 |
And here's an example with cocktails. |
| 228 |
</p> |
| 229 |
</div> |
| 230 |
<div style="clear: both"></div> |
| 231 |
</div> |
| 232 |
</div> |
| 233 |
|
| 234 |
{% include demo.inc name="sours" %} |
| 235 |
|
| 236 |
<div class="hgroup"> |
| 237 |
<div class="hgroup-inline"> |
| 238 |
<div class="panel"> |
| 239 |
<h2 id="conditionals">Conditionals</h2> |
| 240 |
</div> |
| 241 |
<div style="clear: both"></div> |
| 242 |
</div> |
| 243 |
</div> |
| 244 |
|
| 245 |
<div class="hgroup"> |
| 246 |
<div class="hgroup-inline"> |
| 247 |
<div class="panel"> |
| 248 |
<p> |
| 249 |
Conditional expressions look like <code>if b then e else e</code>. The |
| 250 |
<code>else</code> branch is optional and defaults to <code>null</code>. |
| 251 |
</p> |
| 252 |
<p> |
| 253 |
Try adding the missing Large Virgin Mojito: |
| 254 |
</p> |
| 255 |
</div> |
| 256 |
<div style="clear: both"></div> |
| 257 |
</div> |
| 258 |
</div> |
| 259 |
|
| 260 |
{% include demo.inc name="conditionals" %} |
| 261 |
|
| 262 |
<div class="hgroup"> |
| 263 |
<div class="hgroup-inline"> |
| 264 |
<div class="panel"> |
| 265 |
<h2 id="computed_field_names">Computed Field Names</h2> |
| 266 |
</div> |
| 267 |
<div style="clear: both"></div> |
| 268 |
</div> |
| 269 |
</div> |
| 270 |
|
| 271 |
<div class="hgroup"> |
| 272 |
<div class="hgroup-inline"> |
| 273 |
<div class="panel"> |
| 274 |
<p> |
| 275 |
Jsonnet objects can be used like a <code>std::map</code> or similar datastructures from |
| 276 |
regular languages. |
| 277 |
</p> |
| 278 |
<ul> |
| 279 |
<li>Recall that a field lookup can be computed with <code>obj[e]</code></li> |
| 280 |
<li>The definition equivalent is <code>{[e]: </code>... <code>}</code></li> |
| 281 |
<li> |
| 282 |
<code>self</code> or object locals cannot be accessed when field names are being computed, |
| 283 |
since the object is not yet constructed. |
| 284 |
</li> |
| 285 |
<li> |
| 286 |
If a field name evaluates to <code>null</code> during object construction, the field is |
| 287 |
omitted. This works nicely with the default false branch of a conditional (see below). |
| 288 |
</li> |
| 289 |
</ul> |
| 290 |
<p> |
| 291 |
Try adding <code>[self.f]: 'f'</code> to see what happens, while pondering what that |
| 292 |
could even mean! |
| 293 |
</p> |
| 294 |
</div> |
| 295 |
<div style="clear: both"></div> |
| 296 |
</div> |
| 297 |
</div> |
| 298 |
|
| 299 |
{% include demo.inc name="computed-fields" %} |
| 300 |
|
| 301 |
<div class="hgroup"> |
| 302 |
<div class="hgroup-inline"> |
| 303 |
<div class="panel"> |
| 304 |
<h2 id="comprehension">Array and Object Comprehension</h2> |
| 305 |
</div> |
| 306 |
<div style="clear: both"></div> |
| 307 |
</div> |
| 308 |
</div> |
| 309 |
|
| 310 |
<div class="hgroup"> |
| 311 |
<div class="hgroup-inline"> |
| 312 |
<div class="panel"> |
| 313 |
<p> |
| 314 |
What if you want to make an array or object and you don't know how many elements / fields |
| 315 |
they will have at run-time? Jsonnet has Python-style array and object comprehension |
| 316 |
constructs to allow this. |
| 317 |
</p> |
| 318 |
<ul> |
| 319 |
<li>Any nesting of <code>for</code> and <code>if</code> can be used.</li> |
| 320 |
<li>The nest behaves like a loop nest, although the body is written first.</li> |
| 321 |
</ul> |
| 322 |
</div> |
| 323 |
<div style="clear: both"></div> |
| 324 |
</div> |
| 325 |
</div> |
| 326 |
|
| 327 |
{% include demo.inc name="comprehensions" %} |
| 328 |
|
| 329 |
<div class="hgroup"> |
| 330 |
<div class="hgroup-inline"> |
| 331 |
<div class="panel"> |
| 332 |
<p> |
| 333 |
Below, a less contrived example. The first cocktail has equal parts of three ingredients. |
| 334 |
The second iterates over an array of records - name prefix and fruit juice. |
| 335 |
</p> |
| 336 |
<p> |
| 337 |
Try adding a "Red Screwdriver" with cranberry juice. |
| 338 |
</p> |
| 339 |
</div> |
| 340 |
<div style="clear: both"></div> |
| 341 |
</div> |
| 342 |
</div> |
| 343 |
|
| 344 |
{% include demo.inc name="cocktail-comprehensions" %} |
| 345 |
|
| 346 |
<div class="hgroup"> |
| 347 |
<div class="hgroup-inline"> |
| 348 |
<div class="panel"> |
| 349 |
<h2 id="imports">Imports</h2> |
| 350 |
</div> |
| 351 |
<div style="clear: both"></div> |
| 352 |
</div> |
| 353 |
</div> |
| 354 |
|
| 355 |
<div class="hgroup"> |
| 356 |
<div class="hgroup-inline"> |
| 357 |
<div class="panel"> |
| 358 |
<p> |
| 359 |
It's possible to import both code and raw data from other files. |
| 360 |
</p> |
| 361 |
<ul> |
| 362 |
<li>The <code>import</code> construct is like copy/pasting Jsonnet code.</li> |
| 363 |
<li>Files designed for import by convention end with <tt>.libsonnet</tt></li> |
| 364 |
<li>Raw JSON can be imported this way too.</li> |
| 365 |
<li>The <code>importstr</code> construct is for verbatim UTF-8 text.</li> |
| 366 |
<li>The <code>importbin</code> construct is for verbatim binary data.</li> |
| 367 |
</ul> |
| 368 |
<p> |
| 369 |
Usually, imported Jsonnet content is stashed in a top-level local variable. This |
| 370 |
resembles the way other programming languages handle modules. Jsonnet libraries typically |
| 371 |
return an object, so that they can easily be extended. Neither of these conventions are |
| 372 |
enforced. |
| 373 |
</p> |
| 374 |
<p> |
| 375 |
Try including the Cosmopolitan from the library. |
| 376 |
</p> |
| 377 |
</div> |
| 378 |
<div style="clear: both"></div> |
| 379 |
</div> |
| 380 |
</div> |
| 381 |
|
| 382 |
<div class="inverse hgroup"> |
| 383 |
<div class=hgroup-inline> |
| 384 |
<div class="tab-window-input" id="imports-input"> |
| 385 |
<div class="tab-header"> |
| 386 |
</div> |
| 387 |
<textarea id=imports-jsonnet> |
| 388 |
{% include examples/imports.jsonnet %} |
| 389 |
</textarea> |
| 390 |
<textarea id=martinis-libsonnet> |
| 391 |
{% include examples/martinis.libsonnet %} |
| 392 |
</textarea> |
| 393 |
<textarea id=garnish-txt> |
| 394 |
{% include examples/garnish.txt %} |
| 395 |
</textarea> |
| 396 |
</div> |
| 397 |
<div class="bigarrow">➡</div> |
| 398 |
<div class="tab-window-output" id="imports-output"> |
| 399 |
<div class="tab-header"> |
| 400 |
<div class=selected onclick="tab_output_click(this, 'imports-json-output')">output.json</div> |
| 401 |
</div> |
| 402 |
<textarea readonly class="selected code-json" id="imports-json-output"> |
| 403 |
{% include examples/imports.jsonnet.golden %} |
| 404 |
</textarea> |
| 405 |
</div> |
| 406 |
<script> |
| 407 |
demo( |
| 408 |
'imports-input', |
| 409 |
{ |
| 410 |
'imports-jsonnet': 'imports.jsonnet', |
| 411 |
'martinis-libsonnet': 'martinis.libsonnet', |
| 412 |
'garnish-txt': 'garnish.txt', |
| 413 |
}, |
| 414 |
'imports.jsonnet', |
| 415 |
'imports-output', |
| 416 |
false, |
| 417 |
false |
| 418 |
); |
| 419 |
</script> |
| 420 |
<div style="clear: both"></div> |
| 421 |
</div> |
| 422 |
</div> |
| 423 |
|
| 424 |
<div class="hgroup"> |
| 425 |
<div class="hgroup-inline"> |
| 426 |
<div class="panel"> |
| 427 |
<p> |
| 428 |
The following example shows how to write libraries of functions. It also shows how to |
| 429 |
define a local variable in the scope of a function. Try adding the Bee's Knees from above |
| 430 |
using the utility library. |
| 431 |
</p> |
| 432 |
</div> |
| 433 |
<div style="clear: both"></div> |
| 434 |
</div> |
| 435 |
</div> |
| 436 |
|
| 437 |
<div class="inverse hgroup"> |
| 438 |
<div class=hgroup-inline> |
| 439 |
<div class="tab-window-input" id="negroni-input"> |
| 440 |
<div class="tab-header"> |
| 441 |
</div> |
| 442 |
<textarea id=negroni-jsonnet> |
| 443 |
{% include examples/negroni.jsonnet %} |
| 444 |
</textarea> |
| 445 |
<textarea id=utils-libsonnet> |
| 446 |
{% include examples/utils.libsonnet %} |
| 447 |
</textarea> |
| 448 |
</div> |
| 449 |
<div class="bigarrow">➡</div> |
| 450 |
<div class="tab-window-output" id="negroni-output"> |
| 451 |
<div class="tab-header"> |
| 452 |
<div class=selected onclick="tab_output_click(this, 'negroni-json-output')">output.json</div> |
| 453 |
</div> |
| 454 |
<textarea readonly class="selected code-json" id="negroni-json-output"> |
| 455 |
{% include examples/negroni.jsonnet.golden %} |
| 456 |
</textarea> |
| 457 |
</div> |
| 458 |
<script> |
| 459 |
demo( |
| 460 |
'negroni-input', |
| 461 |
{ |
| 462 |
'negroni-jsonnet': 'negroni.jsonnet', |
| 463 |
'utils-libsonnet': 'utils.libsonnet', |
| 464 |
}, |
| 465 |
'negroni.jsonnet', |
| 466 |
'negroni-output', |
| 467 |
false, |
| 468 |
false |
| 469 |
); |
| 470 |
</script> |
| 471 |
<div style="clear: both"></div> |
| 472 |
</div> |
| 473 |
</div> |
| 474 |
|
| 475 |
<div class="hgroup"> |
| 476 |
<div class="hgroup-inline"> |
| 477 |
<div class="panel"> |
| 478 |
<h2 id="errors">Errors</h2> |
| 479 |
</div> |
| 480 |
<div style="clear: both"></div> |
| 481 |
</div> |
| 482 |
</div> |
| 483 |
|
| 484 |
<div class="hgroup"> |
| 485 |
<div class="hgroup-inline"> |
| 486 |
<div class="panel"> |
| 487 |
<p> |
| 488 |
Errors can arise from the language itself (e.g. an array overrun) or thrown from Jsonnet |
| 489 |
code. Stack traces provide context for the error. |
| 490 |
</p> |
| 491 |
<ul> |
| 492 |
<li>To raise an error: <code>error "foo"</code></li> |
| 493 |
<li>To assert a condition before an expression: <code>assert "foo";</code></li> |
| 494 |
<li>A custom failure message: <code>assert "foo" : "message";</code></li> |
| 495 |
<li>Assert fields have a property: <code>assert self.f == 10,</code></li> |
| 496 |
<li>With custom failure message: <code>assert "foo" : "message",</code></li> |
| 497 |
</ul> |
| 498 |
<p> |
| 499 |
Try modifying the code below to trigger the assertion failures, and observe the error |
| 500 |
messages and stack traces that result. |
| 501 |
</p> |
| 502 |
<p> |
| 503 |
You might wonder why the divide-by-zero is not triggered in the equal_parts function, since |
| 504 |
that code occurs before the <code>std.length()</code> check. Jsonnet is a lazy |
| 505 |
language, so variable initializers are not evaluated until the variable is used. Evaluation |
| 506 |
order is very hard to notice. It only becomes relevant when code throws errors, or takes a |
| 507 |
long time to execute. For more discussion of laziness, see <a |
| 508 |
href=/articles/design.html>design rationale</a>. |
| 509 |
</p> |
| 510 |
</div> |
| 511 |
<div style="clear: both"></div> |
| 512 |
</div> |
| 513 |
</div> |
| 514 |
|
| 515 |
{% include demo.inc name="error-examples" %} |
| 516 |
|
| 517 |
<div class="hgroup"> |
| 518 |
<div class="hgroup-inline"> |
| 519 |
<div class="panel"> |
| 520 |
<h2 id="parameterize-entire-config">Parameterize Entire Config</h2> |
| 521 |
</div> |
| 522 |
<div style="clear: both"></div> |
| 523 |
</div> |
| 524 |
</div> |
| 525 |
|
| 526 |
<div class="hgroup"> |
| 527 |
<div class="hgroup-inline"> |
| 528 |
<div class="panel"> |
| 529 |
<p> |
| 530 |
Jsonnet is hermetic: It always generates the same data no matter the execution environment. |
| 531 |
This is an important property but there are times when you want a few carefully chosen |
| 532 |
parameters at the top level. There are two ways to do this, with different properties. |
| 533 |
</p> |
| 534 |
<ul> |
| 535 |
<li> |
| 536 |
<b>External variables</b>, which are accessible anywhere in the config, or any file, |
| 537 |
using <code>std.extVar("foo")</code>. |
| 538 |
</li> |
| 539 |
<li> |
| 540 |
<b>Top-level arguments</b>, where the whole config is expressed as a function. |
| 541 |
</li> |
| 542 |
</ul> |
| 543 |
<p> |
| 544 |
To illustrate the differences, We'll show how the same example is expressed in each case. |
| 545 |
</p> |
| 546 |
</div> |
| 547 |
<div style="clear: both"></div> |
| 548 |
</div> |
| 549 |
</div> |
| 550 |
|
| 551 |
<div class="hgroup"> |
| 552 |
<div class="hgroup-inline"> |
| 553 |
<div class="panel"> |
| 554 |
<h3 id="parameterize-entire-config">External variables</h3> |
| 555 |
</div> |
| 556 |
<div style="clear: both"></div> |
| 557 |
</div> |
| 558 |
</div> |
| 559 |
|
| 560 |
<div class="hgroup"> |
| 561 |
<div class="hgroup-inline"> |
| 562 |
<div class="panel"> |
| 563 |
<p> |
| 564 |
The following example binds two external variables, listed below. Any Jsonnet value can be |
| 565 |
bound to an external variable, even functions. |
| 566 |
</p> |
| 567 |
<ul> |
| 568 |
<li><code>prefix</code> is bound to the string <code>"Happy Hour "</code></li> |
| 569 |
<li><code>brunch</code> is bound to <code>true</code></li> |
| 570 |
</ul> |
| 571 |
<p> |
| 572 |
The values are configured when the Jsonnet virtual machine is initialized, by passing either |
| 573 |
1) Jsonnet code (which evaluates to the value), 2) or a raw string. The latter is just a |
| 574 |
convenience, because escaping a string to pass it as Jsonnet code can be tedious. To make |
| 575 |
this concrete, the above variables can be configured with the following commandline: |
| 576 |
</p> |
| 577 |
<pre>jsonnet --ext-str prefix="Happy Hour " \ |
| 578 |
--ext-code brunch=true ... |
| 579 |
</pre> |
| 580 |
</div> |
| 581 |
<div style="clear: both"></div> |
| 582 |
</div> |
| 583 |
</div> |
| 584 |
|
| 585 |
<div class="inverse hgroup"> |
| 586 |
<div class=hgroup-inline> |
| 587 |
<div class="tab-window-input" id="top-level-ext-input"> |
| 588 |
<div class="tab-header"> |
| 589 |
</div> |
| 590 |
<textarea id=top-level-ext-jsonnet> |
| 591 |
{% include examples/top-level-ext.jsonnet %} |
| 592 |
</textarea> |
| 593 |
<textarea id=library-ext-libsonnet> |
| 594 |
{% include examples/library-ext.libsonnet %} |
| 595 |
</textarea> |
| 596 |
</div> |
| 597 |
<div class="bigarrow">➡</div> |
| 598 |
<div class="tab-window-output" id="top-level-ext-output"> |
| 599 |
<div class="tab-header"> |
| 600 |
<div class=selected onclick="tab_click(this, 'top-level-ext-json-output')">output.json</div> |
| 601 |
</div> |
| 602 |
<textarea readonly class="selected code-json" id="top-level-ext-json-output"> |
| 603 |
{% include examples/top-level-ext.jsonnet.golden %} |
| 604 |
</textarea> |
| 605 |
</div> |
| 606 |
<script> |
| 607 |
demo( |
| 608 |
'top-level-ext-input', |
| 609 |
{ |
| 610 |
'top-level-ext-jsonnet': 'top-level-ext.jsonnet', |
| 611 |
'library-ext-libsonnet': 'library-ext.libsonnet', |
| 612 |
}, |
| 613 |
'top-level-ext.jsonnet', |
| 614 |
'top-level-ext-output', |
| 615 |
false, |
| 616 |
false, |
| 617 |
{ |
| 618 |
ext_str: { |
| 619 |
prefix: 'Happy Hour ', |
| 620 |
}, |
| 621 |
ext_code: { |
| 622 |
brunch: 'true', |
| 623 |
}, |
| 624 |
} |
| 625 |
); |
| 626 |
</script> |
| 627 |
<div style="clear: both"></div> |
| 628 |
</div> |
| 629 |
</div> |
| 630 |
|
| 631 |
<div class="hgroup"> |
| 632 |
<div class="hgroup-inline"> |
| 633 |
<div class="panel"> |
| 634 |
<h3 id="parameterize-entire-config">Top-level arguments</h3> |
| 635 |
</div> |
| 636 |
<div style="clear: both"></div> |
| 637 |
</div> |
| 638 |
</div> |
| 639 |
|
| 640 |
<div class="hgroup"> |
| 641 |
<div class="hgroup-inline"> |
| 642 |
<div class="panel"> |
| 643 |
<p> |
| 644 |
Alternatively the same code can be written using top-level arguments, where the whole config |
| 645 |
is written as a function. How does this differ to using external variables? |
| 646 |
</p> |
| 647 |
<ul> |
| 648 |
<li>Values must be explicitly threaded through files</li> |
| 649 |
<li>Default values can be provided</li> |
| 650 |
<li>A config with top-level arguments can also be imported as a library and called as a |
| 651 |
function with parameters passed in</li> |
| 652 |
</ul> |
| 653 |
<p> |
| 654 |
Generally, top-level arguments are the safer and easier way to parameterize an entire |
| 655 |
config, because the variables are not global and it's clear what parts of the config |
| 656 |
are dependent on their environment. However, they do require more explicit threading of |
| 657 |
the values into other imported code. Here's the equivalent invocation of the Jsonnet |
| 658 |
command-line tool: |
| 659 |
</p> |
| 660 |
<pre>jsonnet --tla-str prefix="Happy Hour " \ |
| 661 |
--tla-code brunch=true ... |
| 662 |
</pre> |
| 663 |
</div> |
| 664 |
<div style="clear: both"></div> |
| 665 |
</div> |
| 666 |
</div> |
| 667 |
|
| 668 |
<div class="inverse hgroup"> |
| 669 |
<div class=hgroup-inline> |
| 670 |
<div class="tab-window-input" id="top-level-tla-input"> |
| 671 |
<div class="tab-header"> |
| 672 |
</div> |
| 673 |
<textarea id=top-level-tla-jsonnet> |
| 674 |
{% include examples/top-level-tla.jsonnet %} |
| 675 |
</textarea> |
| 676 |
<textarea id=library-tla-libsonnet> |
| 677 |
{% include examples/library-tla.libsonnet %} |
| 678 |
</textarea> |
| 679 |
</div> |
| 680 |
<div class="bigarrow">➡</div> |
| 681 |
<div class="tab-window-output" id="top-level-tla-output"> |
| 682 |
<div class="tab-header"> |
| 683 |
<div class=selected onclick="tab_click(this, 'top-level-tla-json-output')">output.json</div> |
| 684 |
</div> |
| 685 |
<textarea readonly class="selected code-json" id="top-level-tla-json-output"> |
| 686 |
{% include examples/top-level-tla.jsonnet.golden %} |
| 687 |
</textarea> |
| 688 |
</div> |
| 689 |
<script> |
| 690 |
demo( |
| 691 |
'top-level-tla-input', |
| 692 |
{ |
| 693 |
'top-level-tla-jsonnet': 'top-level-tla.jsonnet', |
| 694 |
'library-tla-libsonnet': 'library-tla.libsonnet', |
| 695 |
}, |
| 696 |
'top-level-tla.jsonnet', |
| 697 |
'top-level-tla-output', |
| 698 |
false, |
| 699 |
false, |
| 700 |
{ |
| 701 |
tla_str: { |
| 702 |
prefix: 'Happy Hour ', |
| 703 |
}, |
| 704 |
tla_code: { |
| 705 |
brunch: 'true', |
| 706 |
}, |
| 707 |
} |
| 708 |
); |
| 709 |
</script> |
| 710 |
<div style="clear: both"></div> |
| 711 |
</div> |
| 712 |
</div> |
| 713 |
|
| 714 |
<div class="hgroup"> |
| 715 |
<div class="hgroup-inline"> |
| 716 |
<div class="panel"> |
| 717 |
<h2 id="oo">Object-Orientation</h2> |
| 718 |
</div> |
| 719 |
<div style="clear: both"></div> |
| 720 |
</div> |
| 721 |
</div> |
| 722 |
|
| 723 |
<div class="hgroup"> |
| 724 |
<div class="hgroup-inline"> |
| 725 |
<div class="panel"> |
| 726 |
<p> |
| 727 |
In general, object-orientation makes it easy to define many variants from a single "base". |
| 728 |
Unlike Java, C++ and Python, where classes extend other classes, in Jsonnet, objects extend |
| 729 |
other objects. We have already discussed some of the raw ingredients for this, albeit in |
| 730 |
isolation: |
| 731 |
</p> |
| 732 |
<ul> |
| 733 |
<li>Objects (which we inherit from JSON)</li> |
| 734 |
<li>The <dfn>object composition</dfn> operator <code>+</code>, which merges two objects, |
| 735 |
choosing the right hand side when fields collide</li> |
| 736 |
<li>The <code>self</code> keyword, a reference to the current object</li> |
| 737 |
</ul> |
| 738 |
<p> |
| 739 |
When these features are combined together and with the following <i>new</i> features, things |
| 740 |
get a lot more interesting: |
| 741 |
</p> |
| 742 |
<ul> |
| 743 |
<li>Hidden fields, defined with <code>::</code>, which do not appear in generated JSON</li> |
| 744 |
<li>The <code>super</code> keyword is used to access fields on a base object.</li> |
| 745 |
<li>The <code>+:</code> field syntax for overriding deeply nested fields</li> |
| 746 |
</ul> |
| 747 |
</div> |
| 748 |
<div style="clear: both"></div> |
| 749 |
</div> |
| 750 |
</div> |
| 751 |
|
| 752 |
<div class="hgroup"> |
| 753 |
<div class="hgroup-inline"> |
| 754 |
<div class="panel"> |
| 755 |
<p> |
| 756 |
The following example is contrived but demonstrates a few of the features using the |
| 757 |
classical language of object-orientation, "derived" and "base". |
| 758 |
</p> |
| 759 |
</div> |
| 760 |
<div style="clear: both"></div> |
| 761 |
</div> |
| 762 |
</div> |
| 763 |
|
| 764 |
{% include demo.inc name="oo-contrived" %} |
| 765 |
|
| 766 |
<div class="hgroup"> |
| 767 |
<div class="hgroup-inline"> |
| 768 |
<div class="panel"> |
| 769 |
<p> |
| 770 |
Let's make it more concrete by mixing deriving some cocktails that are quite similar from a |
| 771 |
template that draws out their similarities. The <code>+</code> operator is actually |
| 772 |
implicit in these examples. In the common case where you write <code>foo + { |
| 773 |
</code>...<code> }</code>, i.e. the <code>+</code> is immediately followed by a |
| 774 |
<code>{</code>, then the <code>+</code> can be elided. Try explicitly adding the + in the 4 |
| 775 |
cases, below. |
| 776 |
</p> |
| 777 |
</div> |
| 778 |
<div style="clear: both"></div> |
| 779 |
</div> |
| 780 |
</div> |
| 781 |
|
| 782 |
<div class="inverse hgroup"> |
| 783 |
<div class=hgroup-inline> |
| 784 |
<div class="tab-window-input" id="sours-oo-input"> |
| 785 |
<div class="tab-header"> |
| 786 |
</div> |
| 787 |
<textarea id=sours-oo-jsonnet> |
| 788 |
{% include examples/sours-oo.jsonnet %} |
| 789 |
</textarea> |
| 790 |
<textarea id=templates-libsonnet> |
| 791 |
{% include examples/templates.libsonnet %} |
| 792 |
</textarea> |
| 793 |
</div> |
| 794 |
<div class="bigarrow">➡</div> |
| 795 |
<div class="tab-window-output" id="sours-oo-output"> |
| 796 |
<div class="tab-header"> |
| 797 |
<div class=selected onclick="tab_output_click(this, 'sours-oo-json-output')">output.json</div> |
| 798 |
</div> |
| 799 |
<textarea readonly class="selected code-json" id="sours-oo-json-output"> |
| 800 |
{% include examples/sours-oo.jsonnet.golden %} |
| 801 |
</textarea> |
| 802 |
</div> |
| 803 |
<script> |
| 804 |
demo( |
| 805 |
'sours-oo-input', |
| 806 |
{ |
| 807 |
'sours-oo-jsonnet': 'sours-oo.jsonnet', |
| 808 |
'templates-libsonnet': 'templates.libsonnet', |
| 809 |
}, |
| 810 |
'sours-oo.jsonnet', |
| 811 |
'sours-oo-output', |
| 812 |
false, |
| 813 |
false |
| 814 |
); |
| 815 |
</script> |
| 816 |
<div style="clear: both"></div> |
| 817 |
</div> |
| 818 |
</div> |
| 819 |
|
| 820 |
<div class="hgroup"> |
| 821 |
<div class="hgroup-inline"> |
| 822 |
<div class="panel"> |
| 823 |
<p> |
| 824 |
The key to making Jsonnet object-oriented is that the <code>self</code> keyword is "late |
| 825 |
bound". In other words, <code>self.foo</code> can have its meaning altered by overriding it |
| 826 |
on the right hand side of a <code>+</code>. This would not be the case in a simple "object |
| 827 |
merge" semantics of <code>+</code>, where <code>self.foo</code> would be fully evaluated to |
| 828 |
a concrete value before we did the <code>+</code>. In the above example, the left hand side |
| 829 |
of the <code>+</code> is <code>templates.Sour</code> (the <code>+</code> is implicit) and |
| 830 |
the right hand side is an object that overrides <code>spirit</code>. So in the |
| 831 |
<code>templates.Sour</code> definition (in <tt>templates.libsonnet</tt>) that causes the |
| 832 |
reference to <code>drink.spirit</code> in the ingredients list to return |
| 833 |
<code>"Whiskey"</code> rather than raise the error <code>'Must override "spirit"'</code>. |
| 834 |
</p> |
| 835 |
<p> |
| 836 |
Most of the abstraction in the above example could have been achieved with functions instead |
| 837 |
of object-orientation. Indeed, last time we spoke about sours, we used functions for that |
| 838 |
very purpose. In general, the choice of whether to use functions or object-orientation is |
| 839 |
quite a complex one, and can be driven as much by instinct as by anything else. The most |
| 840 |
obvious difference is that with object-orientation, you can override any field from the |
| 841 |
resulting object, whereas with functions you can only pass arguments for the specific |
| 842 |
parameters of the function. So, e.g., if the <code>templates.Sour</code> had been |
| 843 |
implemented as a function that did not parameterize the ingredients list in general way, |
| 844 |
then it would not have been possible to add that extra ingredient to form the Nor'Easter. |
| 845 |
</p> |
| 846 |
<p> |
| 847 |
So far, every example has used a literal object on the right hand side of the |
| 848 |
<code>+</code>. This is consistent with the majority of object-oriented languages. But |
| 849 |
Jsonnet is more general in that is has <a |
| 850 |
href="https://www.bracha.org/oopsla90.pdf">mixins</a>. A Mixin allows you to take some |
| 851 |
"overrides" and instead of just applying them to an existing object to get a new object, you |
| 852 |
can pass them around as a first class value and apply them to any object you want. The |
| 853 |
following example shows mixins being used to modify cocktails in a general way: |
| 854 |
</p> |
| 855 |
</div> |
| 856 |
<div style="clear: both"></div> |
| 857 |
</div> |
| 858 |
</div> |
| 859 |
|
| 860 |
<div class="inverse hgroup"> |
| 861 |
<div class=hgroup-inline> |
| 862 |
<div class="tab-window-input" id="mixins-input"> |
| 863 |
<div class="tab-header"> |
| 864 |
</div> |
| 865 |
<textarea id=mixins-jsonnet> |
| 866 |
{% include examples/mixins.jsonnet %} |
| 867 |
</textarea> |
| 868 |
<textarea id=sours-oo-jsonnet2> |
| 869 |
{% include examples/sours-oo.jsonnet %} |
| 870 |
</textarea> |
| 871 |
<textarea id=templates-libsonnet2> |
| 872 |
{% include examples/templates.libsonnet %} |
| 873 |
</textarea> |
| 874 |
</div> |
| 875 |
<div class="bigarrow">➡</div> |
| 876 |
<div class="tab-window-output" id="mixins-output"> |
| 877 |
<div class="tab-header"> |
| 878 |
<div class=selected onclick="tab_output_click(this, 'mixins-json-output')">output.json</div> |
| 879 |
</div> |
| 880 |
<textarea readonly class="selected code-json" id="mixins-json-output"> |
| 881 |
{% include examples/mixins.jsonnet.golden %} |
| 882 |
</textarea> |
| 883 |
</div> |
| 884 |
<script> |
| 885 |
demo( |
| 886 |
'mixins-input', |
| 887 |
{ |
| 888 |
'mixins-jsonnet': 'mixins.jsonnet', |
| 889 |
'sours-oo-jsonnet2': 'sours-oo.jsonnet', |
| 890 |
'templates-libsonnet2': 'templates.libsonnet', |
| 891 |
}, |
| 892 |
'mixins.jsonnet', |
| 893 |
'mixins-output', |
| 894 |
false, |
| 895 |
false |
| 896 |
); |
| 897 |
</script> |
| 898 |
<div style="clear: both"></div> |
| 899 |
</div> |
| 900 |
</div> |
| 901 |
|
| 902 |
<div class="hgroup"> |
| 903 |
<div class="hgroup-inline"> |
| 904 |
<div class="panel"> |
| 905 |
<p> |
| 906 |
Thank you for completing the Jsonnet tutorial! We hope you enjoyed the accompanying |
| 907 |
beverages. At this point, you should be able to build concoctions of your own. If you want |
| 908 |
to read about the Jsonnet language in greater and more systematic detail, we recommend the |
| 909 |
language <a href="/ref/language.html">reference</a>. |
| 910 |
</p> |
| 911 |
</div> |
| 912 |
<div style="clear: both"></div> |
| 913 |
</div> |
| 914 |
</div> |
| 915 |
|
| 916 |
<div class="hgroup"> |
| 917 |
<div class="hgroup-inline"> |
| 918 |
<div class="panel"> |
| 919 |
</div> |
| 920 |
<div style="clear: both"></div> |
| 921 |
</div> |
| 922 |
</div> |