Debian Codegraph @

doc/learning/getting_started.html

raw
1 ---
2 layout: default
3 title: Getting Started
4 ---
5
6 <div class="hgroup">
7 <div class="hgroup-inline">
8 <div class="panel">
9 <h1 id="getting-started">Getting Started</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 While the <a href="/learning/tutorial.html">tutorial</a> is the introduction to the language
20 itself, this page helps you get started with the implementations. If you want to link
21 Jsonnet as a library, see <a href="/ref/bindings.html">bindings</a>.
22 </p>
23 </div>
24 <div style="clear: both"></div>
25 </div>
26 </div>
27
28 <div class="hgroup">
29 <div class="hgroup-inline">
30 <div class="panel">
31 <h2 id="interpreters">Interpreters</h2>
32 </div>
33 <div style="clear: both"></div>
34 </div>
35 </div>
36
37 <div class="hgroup">
38 <div class="hgroup-inline">
39 <div class="panel">
40 <p>
41 The easiest way to execute some Jsonnet code is invoking an interpreter via the commandline
42 as so:
43 </p>
44 <pre>jsonnet -e &lt;code&gt;</pre>
45 <p>
46 Or a filename:
47 </p>
48 <pre>jsonnet &lt;file&gt;</pre>
49 <p>
50 This dumps the JSON on stdout. The tool is self-documenting with <tt>--help</tt>. You can
51 experiment with the Jsonnet files in the <a
52 href="https://github.com/google/jsonnet/tree/master/examples">examples/</a> directory of the
53 C++ Github repo.
54 </p>
55 <p>
56 To get Jsonnet, you can build it from either the <a
57 href="https://github.com/google/jsonnet">C++</a> or the <a
58 href="https://github.com/google/go-jsonnet">Go</a> repositories. See below for the
59 differences between them. Each repo has a README.md containing build instructions.
60 </p>
61 </div>
62 <div style="clear: both"></div>
63 </div>
64 </div>
65
66
67 <div class="hgroup">
68 <div class="hgroup-inline">
69 <div class="panel">
70 <h3 id="example">Example</h3>
71 </div>
72 <div style="clear: both"></div>
73 </div>
74 </div>
75
76 <div class="hgroup">
77 <div class="hgroup-inline">
78 <div class="panel">
79 <p>Evaluating a file.</p>
80
81 <pre>$ jsonnet landingpage.jsonnet
82 {
83 "person1": {
84 "name": "Alice",
85 "welcome": "Hello Alice!"
86 },
87 "person2": {
88 "name": "Bob",
89 "welcome": "Hello Bob!"
90 }
91 }</pre>
92
93 <p>Evaluating a snippet.</p>
94
95 <pre>$ jsonnet -e '{ x: 1 , y: self.x + 1 } { x: 10 }'
96 {
97 "x": 10,
98 "y": 11
99 }</pre>
100 </div>
101 <div style="clear: both"></div>
102 </div>
103 </div>
104
105
106 <div class="hgroup">
107 <div class="hgroup-inline">
108 <div class="panel">
109 <h3 id="text-output">Generating non-JSON output</h3>
110 </div>
111 <div style="clear: both"></div>
112 </div>
113 </div>
114
115
116 <div class="hgroup">
117 <div class="hgroup-inline">
118 <div class="panel">
119 <p>
120 Sometimes it is useful to generate output that is not in JSON format. You can do this by
121 writing Jsonnet code which evaluates to a string containing whatever your desired format
122 is. When executed with <tt>jsonnet -S</tt> or <tt>jsonnet --string</tt> jsonnet will
123 manifest the string result as plain text rather than a JSON-formatted string.
124 </p>
125
126 <pre>// ini_output.jsonnet
127 std.manifestIni({
128 sections: {
129 main: {
130 host: "127.0.0.1",
131 port: "9000",
132 },
133 database: {
134 path: "/var/data",
135 },
136 },
137 })</pre>
138
139 <p>
140 When executed using <tt>jsonnet -S ini_output.jsonnet</tt>, this will output the generated
141 INI formatted text directly.
142 </p>
143
144 <pre>$ jsonnet -S ini_output.jsonnet
145 [database]
146 path = /var/data
147 [main]
148 host = 127.0.0.1
149 port = 9000</pre>
150
151 </div>
152 <div style="clear: both"></div>
153 </div>
154 </div>
155
156
157 <div class="hgroup">
158 <div class="hgroup-inline">
159 <div class="panel">
160 <h3 id="multi">Multiple File Output</h3>
161 </div>
162 <div style="clear: both"></div>
163 </div>
164 </div>
165
166
167 <div class="hgroup">
168 <div class="hgroup-inline">
169 <div class="panel">
170 <p>
171 The Jsonnet commandline tool has a special mode for generating multiple JSON files from a
172 single Jsonnet file. This can be useful if you want to avoid writing lots of small Jsonnet
173 files, or if you want to take advantage of cross-references and interdependencies between
174 the files. The idea is to create a single JSON structure, the top level of which defines
175 the various files:
176 </p>
177
178 <pre>// multiple_output.jsonnet
179 {
180 "a.json": {
181 x: 1,
182 y: $["b.json"].y,
183 },
184 "b.json": {
185 x: $["a.json"].x,
186 y: 2,
187 },
188 }</pre>
189
190 <p>
191 When executed using <tt>jsonnet -m &lt;dir&gt;</tt>, this will write the generated JSON to
192 files <tt>a.json</tt> and <tt>b.json</tt> in the given directory, instead of the whole thing
193 being written to stdout. In order to integrate nicely with build tools like <tt>make</tt>,
194 the files are not touched if they already contain the given content. To stdout is printed
195 the list of target files, one per line. This makes it easy to drive other tools that
196 operate on the JSON files, e.g. via <tt>xarg</tt>.
197 </p>
198
199 <pre>$ jsonnet -m . multiple_output.jsonnet
200 a.json
201 b.json
202 $ cat a.json
203 {
204 "x": 1,
205 "y": 2
206 }
207 $ cat b.json
208 {
209 "x": 1,
210 "y": 2
211 }</pre>
212
213 </div>
214 <div style="clear: both"></div>
215 </div>
216 </div>
217
218
219 <div class="hgroup">
220 <div class="hgroup-inline">
221 <div class="panel">
222 <h3 id="stream">YAML Stream Output</h3>
223 </div>
224 <div style="clear: both"></div>
225 </div>
226 </div>
227
228
229 <div class="hgroup">
230 <div class="hgroup-inline">
231 <div class="panel">
232 <p>
233 Unlike JSON, YAML can represent several objects in the same file, separated by <tt>---</tt>.
234 The Jsonnet commandline parameter <tt>-y</tt> causes the tool to expect the Jsonnet
235 execution to yield an array. Config designed for this mode typically looks like this:
236 It then outputs that array as a sequence of JSON documents
237 separated by <tt>---</tt>, which any YAML parser will interpret as a YAML stream.
238 </p>
239 <pre>// yaml_stream.jsonnet
240 local
241 a = {
242 x: 1,
243 y: b.y,
244 },
245 b = {
246 x: a.x,
247 y: 2,
248 };
249
250 [a, b]</pre>
251 <p>
252 When executed using <tt>-y</tt>, this will output that array as a sequence of JSON documents
253 separated by <tt>---</tt> and terminated with <tt>...</tt>. Any YAML parser <i>should</i>
254 interpret this as a YAML stream (people have reported broken parsers, so try it out first).
255 </p>
256 <pre>$ jsonnet -y yaml_stream.jsonnet
257 ---
258 {
259 "x": 1,
260 "y": 2
261 }
262 ---
263 {
264 "x": 1,
265 "y": 2
266 }
267 ...</pre>
268 </div>
269 <div style="clear: both"></div>
270 </div>
271 </div>
272
273
274 <div class="hgroup">
275 <div class="hgroup-inline">
276 <div class="panel">
277 <h2 id="cpp-or-go">C++, Go, or others?</h2>
278 </div>
279 <div style="clear: both"></div>
280 </div>
281 </div>
282
283 <div class="hgroup">
284 <div class="hgroup-inline">
285 <div class="panel">
286 <p>
287 There are multiple implementations of the Jsonnet interpreter. The original interpreter was
288 written in C++. However, there is a newer implementation written in Go, and this generally
289 has better performance, as well as the benefits of being written in a memory safe language.
290 The Go implementation also includes a linter, which is not available in the C++ implementation.
291 Both the C++ and Go versions are tested against the same test suite.
292 </p>
293 <p>
294 Beyond the 'official' C++ and Go implementations, there are also various implementations written
295 independently by other people, such as Rust and Haskell implementations, along with language
296 bindings / wrappers for some other languages (such as PHP, and JavaScript).
297 These are listed on the <a href="/ref/bindings.html#third_party_apis">Bindings</a> page.
298 </p>
299 </div>
300 <div style="clear: both"></div>
301 </div>
302 </div>
303