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="multi">Multiple File 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 The Jsonnet commandline tool has a special mode for generating multiple JSON files from a
121 single Jsonnet file. This can be useful if you want to avoid writing lots of small Jsonnet
122 files, or if you want to take advantage of cross-references and interdependencies between
123 the files. The idea is to create a single JSON structure, the top level of which defines
124 the various files:
125 </p>
126
127 <pre>// multiple_output.jsonnet
128 {
129 "a.json": {
130 x: 1,
131 y: $["b.json"].y,
132 },
133 "b.json": {
134 x: $["a.json"].x,
135 y: 2,
136 },
137 }</pre>
138
139 <p>
140 When executed using <tt>jsonnet -m &lt;dir&gt;</tt>, this will write the generated JSON to
141 files <tt>a.json</tt> and <tt>b.json</tt> in the given directory, instead of the whole thing
142 being written to stdout. In order to integrate nicely with build tools like <tt>make</tt>,
143 the files are not touched if they already contain the given content. To stdout is printed
144 the list of target files, one per line. This makes it easy to drive other tools that
145 operate on the JSON files, e.g. via <tt>xarg</tt>.
146 </p>
147
148 <pre>$ jsonnet -m . multiple_output.jsonnet
149 a.json
150 b.json
151 $ cat a.json
152 {
153 "x": 1,
154 "y": 2
155 }
156 $ cat b.json
157 {
158 "x": 1,
159 "y": 2
160 }</pre>
161
162 </div>
163 <div style="clear: both"></div>
164 </div>
165 </div>
166
167
168 <div class="hgroup">
169 <div class="hgroup-inline">
170 <div class="panel">
171 <h3 id="stream">YAML Stream Output</h3>
172 </div>
173 <div style="clear: both"></div>
174 </div>
175 </div>
176
177
178 <div class="hgroup">
179 <div class="hgroup-inline">
180 <div class="panel">
181 <p>
182 Unlike JSON, YAML can represent several objects in the same file, separated by <tt>---</tt>.
183 The Jsonnet commandline parameter <tt>-y</tt> causes the tool to expect the Jsonnet
184 execution to yield an array. Config designed for this mode typically looks like this:
185 It then outputs that array as a sequence of JSON documents
186 separated by <tt>---</tt>, which any YAML parser will interpret as a YAML stream.
187 </p>
188 <pre>// yaml_stream.jsonnet
189 local
190 a = {
191 x: 1,
192 y: b.y,
193 },
194 b = {
195 x: a.x,
196 y: 2,
197 };
198
199 [a, b]</pre>
200 <p>
201 When executed using <tt>-y</tt>, this will output that array as a sequence of JSON documents
202 separated by <tt>---</tt> and terminated with <tt>...</tt>. Any YAML parser <i>should</i>
203 interpret this as a YAML stream (people have reported broken parsers, so try it out first).
204 </p>
205 <pre>$ jsonnet -y yaml_stream.jsonnet
206 ---
207 {
208 "x": 1,
209 "y": 2
210 }
211 ---
212 {
213 "x": 1,
214 "y": 2
215 }
216 ...</pre>
217 </div>
218 <div style="clear: both"></div>
219 </div>
220 </div>
221
222
223 <div class="hgroup">
224 <div class="hgroup-inline">
225 <div class="panel">
226 <h2 id="cpp-or-go">C++ or Go?</h2>
227 </div>
228 <div style="clear: both"></div>
229 </div>
230 </div>
231
232 <div class="hgroup">
233 <div class="hgroup-inline">
234 <div class="panel">
235 <p>
236 There are two feature-compatible implementations of the Jsonnet interpreter. The first one
237 we built was the C++ interpreter, and it is still the most mature / widely used. The new Go
238 implementation has the benefits of simpler code (due to leveraging goroutines and the GO
239 garbage collector). Our long term plan is to migrate everyone to the Go implementation
240 eventually. Both implementations are tested against the same test suite (which is quite
241 thorough). Please report any deviations. The performance of the C++ implementation is
242 currently a bit better, but for most people this should not be a factor and we're intending
243 to address it over time, anyway.
244 </p>
245 <p>
246 Another factor is that the formatter is currently only implemented in the C++ repo, and the
247 linter is only implemented in the Go repo.
248 </p>
249 </div>
250 <div style="clear: both"></div>
251 </div>
252 </div>
253
254
255 <div class="hgroup">
256 <div class="hgroup-inline">
257 <div class="panel">
258 <h2 id="javascript">JavaScript</h2>
259 </div>
260 <div style="clear: both"></div>
261 </div>
262 </div>
263
264 <div class="hgroup">
265 <div class="hgroup-inline">
266 <div class="panel">
267 <p>
268 The C++ implementation can be compiled with <a
269 href="https://emscripten.org/">emscripten</a> to produce a JavaScript
270 implementation of Jsonnet. This is how we implement the interactive demos on this site. A
271 native implementation of Jsonnet in JavaScript would probably be faster. In fact, a
272 GopherJs transpile of the Go Jsonnet implementation may work better as well. However, the
273 emscripten version is easy to build, and is sufficient for many purposes.
274 </p>
275
276 <p>
277 To compile it, first <a
278 href="https://emscripten.org/docs/getting_started/downloads.html">install
279 </a> emscripten and ensure em++ and emcc are in your path. Then make libjsonnet.js.
280 </p>
281
282 <p>
283 An unofficial <a href="https://github.com/yosuke-furukawa/node-jsonnet">nodejs package</a>
284 of Jsonnet is also available.
285 </p>
286
287 </div>
288 <div style="clear: both"></div>
289 </div>
290 </div>
291