aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/jsonnet/README.md
blob: 7d28f151336915f053752ee52641b6cd4a132653 (plain)
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
# Jsonnet Rules

## Rules

* [`jsonnet_library`](#jsonnet_library)
* [`jsonnet_to_json`](#jsonnet_to_json)
* [`jsonnet_to_json_test`](#jsonnet_to_json_test)

## Overview

These are build rules for working with [Jsonnet][jsonnet] files with Bazel.

[jsonnet]: http://google.github.io/jsonnet/doc/

## Setup

To use the Jsonnet rules, simply copy the contents of `jsonnet.WORKSPACE` into
your `WORKSPACE` file.

<a name="#jsonnet_library"></a>
## jsonnet_library

```python
jsonnet_library(name, srcs, deps, imports)
```

<table>
  <thead>
    <tr>
      <th>Attribute</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>name</code></td>
      <td>
        <code>Name, required</code>
        <p>A unique name for this rule.</p>
      </td>
    </tr>
    <tr>
      <td><code>srcs</code></td>
      <td>
        <code>List of Labels, required</code>
        <p>
          List of <code>.jsonnet</code> files that comprises this Jsonnet
          library.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>deps</code></td>
      <td>
        <code>List of labels, optional</code>
        <p>
          List of targets that are required by the <code>srcs</code> Jsonnet
          files.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>imports</code></td>
      <td>
        <code>List of strings, optional</code>
        <p>
          List of import <code>-J</code> flags to be passed to the
          <code>jsonnet</code> compiler.
        </p>
      </td>
    </tr>
  </tbody>
</table>

### Example

Suppose you have the following directory structure:

```
[workspace]/
    WORKSPACE
    configs/
        BUILD
        backend.jsonnet
        frontend.jsonnet
```

You can use the `jsonnet_library` rule to build a collection of `.jsonnet`
files that can be imported by other `.jsonnet` files as dependencies:

`configs/BUILD`:

```python
load("/tools/build_defs/jsonnet/jsonnet", "jsonnet_library")

jsonnet_library(
    name = "configs",
    srcs = [
        "backend.jsonnet",
        "frontend.jsonnet",
    ],
)
```

<a name="#jsonnet_to_json"></a>
## jsonnet_to_json

```python
jsonnet_to_json(name, src, deps, outs, multiple_outputs, imports, vars, code_vars)
```

<table>
  <thead>
    <tr>
      <th>Attribute</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>name</code></td>
      <td>
        <code>Name, required</code>
        <p>A unique name for this rule.</p>
        <p>
          This name will be used as the name of the JSON file generated by this
          rule.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>src</code></td>
      <td>
        <code>Label, required</code>
        <p>
          The <code>.jsonnet</code> file to convert to JSON.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>deps</code></td>
      <td>
        <code>List of labels, optional</code>
        <p>
          List of targets that are required by the <code>src</code> Jsonnet
          file.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>outs</code></td>
      <td>
        <code>List of Filenames, optional</code>
        <p>
          Names of the output .json files to be generated by this rule.
        </p>
        <p>
          If you are generating only a single JSON file and are not using
          jsonnet multiple output files, then this attribute should only
          contain the file name of the JSON file you are generating.
        </p>
        <p>
          If you are generating multiple JSON files using jsonnet multiple file
          output (<code>jsonnet -m</code>), then list the file names of all the
          JSON files to be generated. The file names specified here must match
          the file names specified in your <code>src</code> Jsonnet file.
        </p>
        <p>
          For the case where multiple file output is used but only for
          generating one output file, set the <code>multiple_outputs</code>
          attribute to 1 to explicitly enable the <code>-m</code> flag for
          multiple file output.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>multiple_outputs</code></td>
      <td>
        <code>bool, optional, default 0</code>
        <p>
          Set to 1 to explicitly enable multiple file output via the
          <code>jsonnet -m</code> flag.
        </p>
        <p>
          This is used for the case where multiple file output is used but only
          for generating a single output file. For example:
        </p>
<pre>
local foo = import "foo.jsonnet";

{
  "foo.json": foo,
}
</pre>
        </p>
      </td>
    </tr>
    <tr>
      <td><code>imports</code></td>
      <td>
        <code>List of strings, optional</code>
        <p>
          List of import <code>-J</code> flags to be passed to the
          <code>jsonnet</code> compiler.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>vars</code></td>
      <td>
        <code>String dict, optional</code>
        <p>
          Map of variables to pass to jsonnet via <code>--var key=value</code>.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>code_vars</code></td>
      <td>
        <code>String dict, optional</code>
        <p>
          Map of code variables to pass to jsonnet via
          <code>--code-var key=value</code>.
        </p>
      </td>
    </tr>
  </tbody>
</table>

### Example

Suppose you have the following directory structure:

```
[workspace]/
    WORKSPACE
    workflows/
        BUILD
        workflow.jsonnet
        wordcount.jsonnet
        intersection.jsonnet
```

Say that `workflow.jsonnet` is a base configuration library for a workflow
scheduling system and `wordcount.jsonnet` and `intersection.jsonnet` both
import `workflow.jsonnet` to define workflows for performing a wordcount and
intersection of two files, respectively.

First, create a `jsonnet_library` target with `workflow.jsonnet`:

`workflows/BUILD`:

```python
load("/tools/build_defs/jsonnet/jsonnet", "jsonnet_library")

jsonnet_library(
    name = "workflow",
    srcs = ["workflow.jsonnet"],
)
```

To compile `wordcount.jsonnet` and `intersection.jsonnet` to JSON, define two
`jsonnet_to_json` targets:

```python
jsonnet_to_json(
    name = "wordcount",
    src = "wordcount.jsonnet",
    outs = ["wordcount.json"],
    deps = [":workflow"],
)

jsonnet_to_json(
    name = "intersection",
    src = "intersection.jsonnet",
    outs = ["intersection.json"],
    deps = [":workflow"],
)
```

### Example: Multiple output files

To use Jsonnet's [multiple output files][multiple-output-files], suppose you
add a file `shell-workflows.jsonnet` that imports `wordcount.jsonnet` and
`intersection.jsonnet`:

`workflows/shell-workflows.jsonnet`:

```
local wordcount = import "workflows/wordcount.jsonnet";
local intersection = import "workflows/intersection.jsonnet";

{
  "wordcount-workflow.json": wordcount,
  "intersection-workflow.json": intersection,
}
```

To compile `shell-workflows.jsonnet` into the two JSON files,
`wordcount-workflow.json` and `intersection-workflow.json`, first create a
`jsonnet_library` target containing the two files that
`shell-workflows.jsonnet` depends on:

```python
jsonnet_library(
    name = "shell-workflows-lib",
    srcs = [
        "wordcount.jsonnet",
        "intersection.jsonnet",
    ],
    deps = [":workflow"],
)
```

Then, create a `jsonnet_to_json` target and set `outs` to the list of output
files to indicate that multiple output JSON files are generated:

```python
jsonnet_to_json(
    name = "shell-workflows",
    src = "shell-workflows.jsonnet",
    deps = [":shell-workflows-lib"],
    outs = [
        "wordcount-workflow.jsonnet",
        "intersection-workflow.jsonnet",
    ],
)
```

[multiple-output-files]: http://google.github.io/jsonnet/doc/commandline.html

<a name="#jsonnet_to_json_test"></a>
## jsonnet_to_json_test

```python
jsonnet_to_json_test(name, src, deps, imports, golden, error=0, regex=False)
```

<table>
  <thead>
    <tr>
      <th>Attribute</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>name</code></td>
      <td>
        <code>Name, required</code>
        <p>A unique name for this rule.</p>
        <p>
          This name will be used as the name of the JSON file generated by this
          rule.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>src</code></td>
      <td>
        <code>Label, required</code>
        <p>
          The <code>.jsonnet</code> file to convert to JSON.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>deps</code></td>
      <td>
        <code>List of labels, optional</code>
        <p>
          List of targets that are required by the <code>src</code> Jsonnet
          file.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>imports</code></td>
      <td>
        <code>List of strings, optional</code>
        <p>
          List of import <code>-J</code> flags to be passed to the
          <code>jsonnet</code> compiler.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>vars</code></td>
      <td>
        <code>String dict, optional</code>
        <p>
          Map of variables to pass to jsonnet via <code>--var key=value</code>.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>code_vars</code></td>
      <td>
        <code>String dict, optional</code>
        <p>
          Map of code variables to pass to jsonnet via
          <code>--code-var key=value</code>.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>golden</code></td>
      <td>
        <code>Label, optional</code>
        <p>
          The expected (combined stdout and stderr) output to compare to the
          output of running <code>jsonnet</code> on <code>src</code>.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>error</code></td>
      <td>
        <code>Integer, optional, default is 0</code>
        <p>
          The expected error code from running <code>jsonnet</code> on
          <code>src</code>.
        </p>
      </td>
    </tr>
    <tr>
      <td><code>regex</code></td>
      <td>
        <code>bool, optional, default is False</code>
        <p>
          Set to 1 if <code>golden</code> contains a regex used to match
          the output of running <code>jsonnet</code> on <code>src</code>.
        </p>
      </td>
    </tr>
  </tbody>
</table>

### Example

Suppose you have the following directory structure:

```
[workspace]/
    WORKSPACE
    config/
        BUILD
        base_config.jsonnet
        test_config.jsonnet
        test_config.json
```

Suppose that `base_config.jsonnet` is a library Jsonnet file, containing the
base configuration for a service. Suppose that `test_config.jsonnet` is a test
configuration file that is used to test `base_config.jsonnet`, and
`test_config.json` is the expected JSON output from compiling
`test_config.jsonnet`.

The `jsonnet_to_json_test` rule can be used to verify that compiling a Jsonnet
file produces the expected JSON output. Simply define a `jsonnet_to_json_test`
target and provide the input test Jsonnet file and the `golden` file containing
the expected JSON output:

`config/BUILD`:

```python
load(
    "/tools/build_defs/jsonnet/jsonnet",
    "jsonnet_library",
    "jsonnet_to_json_test",
)

jsonnet_library(
    name = "base_config",
    srcs = ["base_config.jsonnet"],
)

jsonnet_to_json_test(
    name = "test_config_test",
    src = "test_config",
    deps = [":base_config"],
    golden = "test_config.json",
)
```

To run the test: `bazel test //config:test_config_test`

### Example: Negative tests

Suppose you have the following directory structure:

```
[workspace]/
    WORKSPACE
    config/
        BUILD
        base_config.jsonnet
        invalid_config.jsonnet
        invalid_config.output
```

Suppose that `invalid_config.jsonnet` is a Jsonnet file used to verify that
an invalid config triggers an assertion in `base_config.jsonnet`, and
`invalid_config.output` is the expected error output.

The `jsonnet_to_json_test` rule can be used to verify that compiling a Jsonnet
file results in an expected error code and error output. Simply define a
`jsonnet_to_json_test` target and provide the input test Jsonnet file, the
expected error code in the `error` attribute, and the `golden` file containing
the expected error output:

`config/BUILD`:

```python
load(
    "/tools/build_defs/jsonnet/jsonnet",
    "jsonnet_library",
    "jsonnet_to_json_test",
)

jsonnet_library(
    name = "base_config",
    srcs = ["base_config.jsonnet"],
)

jsonnet_to_json_test(
    name = "invalid_config_test",
    src = "invalid_config",
    deps = [":base_config"],
    golden = "invalid_config.output",
    error = 1,
)
```

To run the test: `bazel test //config:invalid_config_test`