@@ -67,36 +67,49 @@ If we supply `js2e` with the following JSON schema file, `definitions.json`:
67
67
it produces the following Elm file, ` Data/Definitions.elm ` :
68
68
69
69
``` elm
70
- module Data.Definitions exposing (..)
70
+ module Data.Definitions exposing
71
+ ( Color (..)
72
+ , Point
73
+ , colorDecoder
74
+ , encodeColor
75
+ , encodePoint
76
+ , pointDecoder
77
+ )
71
78
72
79
-- Schema for common types
73
80
81
+ import Data.Utils
82
+ exposing
83
+ ( encodeNestedOptional
84
+ , encodeNestedRequired
85
+ , encodeOptional
86
+ , encodeRequired
87
+ )
74
88
import Json.Decode as Decode
75
89
exposing
76
- ( succeed
90
+ ( Decoder
91
+ , andThen
92
+ , at
77
93
, fail
78
- , map
79
- , maybe
80
94
, field
81
95
, index
82
- , at
83
- , andThen
84
- , oneOf
96
+ , map
97
+ , maybe
85
98
, nullable
86
- , Decoder
99
+ , oneOf
100
+ , succeed
87
101
)
88
102
import Json.Decode.Pipeline
89
103
exposing
90
- ( decode
91
- , required
104
+ ( custom
92
105
, optional
93
- , custom
106
+ , required
94
107
)
95
108
import Json.Encode as Encode
96
109
exposing
97
110
( Value
98
- , object
99
111
, list
112
+ , object
100
113
)
101
114
102
115
@@ -113,8 +126,8 @@ type alias Point =
113
126
}
114
127
115
128
116
- colorDecoder : String -> Decoder Color
117
- colorDecoder color =
129
+ colorDecoder : Decoder Color
130
+ colorDecoder =
118
131
Decode . string
119
132
|> andThen
120
133
( \ color ->
@@ -138,7 +151,7 @@ colorDecoder color =
138
151
139
152
pointDecoder : Decoder Point
140
153
pointDecoder =
141
- decode Point
154
+ succeed Point
142
155
|> required " x" Decode . float
143
156
|> required " y" Decode . float
144
157
@@ -161,14 +174,10 @@ encodeColor color =
161
174
162
175
encodePoint : Point -> Value
163
176
encodePoint point =
164
- let
165
- x =
166
- [ ( " x" , Encode . float point. x ) ]
167
-
168
- y =
169
- [ ( " y" , Encode . float point. y ) ]
170
- in
171
- object <| x ++ y
177
+ []
178
+ |> encodeRequired " x" point. x Encode . float
179
+ |> encodeRequired " y" point. y Encode . float
180
+ |> Encode . object
172
181
```
173
182
174
183
which contains an Elm type for the ` color ` and ` point ` definitions along with
@@ -204,38 +213,48 @@ definitions (types, encoders and decoders) from the other Elm module,
204
213
` Data/Definitions.elm ` .
205
214
206
215
``` elm
207
- module Data.Circle exposing (..)
216
+ module Data.Circle exposing
217
+ ( Circle
218
+ , circleDecoder
219
+ , encodeCircle
220
+ )
208
221
209
222
-- Schema for a circle shape
210
223
224
+ import Data.Definitions as Definitions
225
+ import Data.Utils
226
+ exposing
227
+ ( encodeNestedOptional
228
+ , encodeNestedRequired
229
+ , encodeOptional
230
+ , encodeRequired
231
+ )
211
232
import Json.Decode as Decode
212
233
exposing
213
- ( succeed
234
+ ( Decoder
235
+ , andThen
236
+ , at
214
237
, fail
215
- , map
216
- , maybe
217
238
, field
218
239
, index
219
- , at
220
- , andThen
221
- , oneOf
240
+ , map
241
+ , maybe
222
242
, nullable
223
- , Decoder
243
+ , oneOf
244
+ , succeed
224
245
)
225
246
import Json.Decode.Pipeline
226
247
exposing
227
- ( decode
228
- , required
248
+ ( custom
229
249
, optional
230
- , custom
250
+ , required
231
251
)
232
252
import Json.Encode as Encode
233
253
exposing
234
254
( Value
235
- , object
236
255
, list
256
+ , object
237
257
)
238
- import Data.Definitions as Definitions
239
258
240
259
241
260
type alias Circle =
@@ -247,53 +266,44 @@ type alias Circle =
247
266
248
267
circleDecoder : Decoder Circle
249
268
circleDecoder =
250
- decode Circle
269
+ succeed Circle
251
270
|> required " center" Definitions . pointDecoder
252
271
|> optional " color" ( nullable Definitions . colorDecoder) Nothing
253
272
|> required " radius" Decode . float
254
273
255
274
256
275
encodeCircle : Circle -> Value
257
276
encodeCircle circle =
258
- let
259
- center =
260
- [ ( " center" , Definitions . encodePoint circle. center ) ]
261
-
262
- color =
263
- case circle. color of
264
- Just color ->
265
- [ ( " color" , Definitions . encodeColor color ) ]
266
-
267
- Nothing ->
268
- []
269
-
270
- radius =
271
- [ ( " radius" , Encode . float circle. radius ) ]
272
- in
273
- object <|
274
- center
275
- ++ color
276
- ++ radius
277
+ []
278
+ |> encodeRequired " center" circle. center Definitions . encodePoint
279
+ |> encodeOptional " color" circle. color Definitions . encodeColor
280
+ |> encodeRequired " radius" circle. radius Encode . float
281
+ |> Encode . object
277
282
```
278
283
279
284
Furthermore, ` js2e ` also generates test files for the generated decoders and
280
- encoders, which fuzzes instances of a given Elm type and tests that encoding it
281
- as JSON and decoding it back into Elm returns the original instance of that
282
- generated Elm type. In the above case, the following test files,
285
+ encoders to make the generated code immediately testable. The generated test
286
+ files fuzzes instances of a given Elm type and tests that encoding it as JSON
287
+ and decoding it back into Elm returns the original instance of that generated
288
+ Elm type. In the above case, the following test files,
283
289
` tests/Data/CircleTests.elm ` and ` tests/Data/DefinitionsTests.elm ` , are
284
290
generated:
285
291
286
292
``` elm
287
- module Data.CircleTests exposing (..)
293
+ module Data.CircleTests exposing
294
+ ( circleFuzzer
295
+ , encodeDecodeCircleTest
296
+ )
297
+
288
298
289
299
-- Tests: Schema for a circle shape
290
300
301
+ import Data.Circle exposing (..)
302
+ import Data.DefinitionsTests as Definitions
291
303
import Expect exposing (Expectation )
292
304
import Fuzz exposing (Fuzzer )
293
- import Test exposing (..)
294
305
import Json.Decode as Decode
295
- import Data.Circle exposing (..)
296
- import Data.DefinitionsTests as Definitions
306
+ import Test exposing (..)
297
307
298
308
299
309
circleFuzzer : Fuzzer Circle
@@ -317,15 +327,20 @@ encodeDecodeCircleTest =
317
327
and
318
328
319
329
``` elm
320
- module Data.DefinitionsTests exposing (..)
330
+ module Data.DefinitionsTests exposing
331
+ ( colorFuzzer
332
+ , encodeDecodeColorTest
333
+ , encodeDecodePointTest
334
+ , pointFuzzer
335
+ )
321
336
322
337
-- Tests: Schema for common types
323
338
339
+ import Data.Definitions exposing (..)
324
340
import Expect exposing (Expectation )
325
341
import Fuzz exposing (Fuzzer )
326
- import Test exposing (..)
327
342
import Json.Decode as Decode
328
- import Data.Definitions exposing (..)
343
+ import Test exposing (..)
329
344
330
345
331
346
colorFuzzer : Fuzzer Color
@@ -364,13 +379,18 @@ encodeDecodePointTest =
364
379
|> encodePoint
365
380
|> Decode . decodeValue pointDecoder
366
381
|> Expect . equal ( Ok point)
367
-
368
382
```
369
383
370
384
Finally, ` js2e ` also generates package config files, ` package.json ` and
371
- ` elm-package.json ` making it easy to test that the generated Elm code is
372
- behaving as expected. Thus, if we supply the following directory structure to
373
- ` js2e ` in the above case:
385
+ ` elm.json ` , and a ` .tool-versions ` file, making it easy to test that the
386
+ generated Elm code is behaving as expected. Note that the ` .tool-versions ` file
387
+ is not a file required by ` elm ` nor ` elm-test ` but instead a file used by the
388
+ ` asdf ` version manager, https://github.com/asdf-vm/asdf , to install and run the
389
+ correct compiler versions of ` node ` and ` elm ` specified in the ` .tool-versions `
390
+ file for a given project.
391
+
392
+ Thus, if we supply the following directory structure to ` js2e ` in the above
393
+ case:
374
394
375
395
```
376
396
.
@@ -384,13 +404,14 @@ the following new directory structure is generated:
384
404
```
385
405
.
386
406
└── js2e_output/
407
+ ├── .tool-versions
387
408
├── package.json
388
- ├── elm-package.json
389
- ├── Data/
390
- │ ├── Circle.elm
391
- │ └── Definitions.elm
409
+ ├── elm.json
410
+ ├── tests/
411
+ │ └── Data/
412
+ │ ├── Circle.elm
413
+ │ └── Definitions.elm
392
414
└── tests/
393
- ├── elm-package.json
394
415
└── Data/
395
416
├── CircleTests.elm
396
417
└── DefinitionsTests.elm
@@ -444,6 +465,9 @@ the error.
444
465
## Contributing
445
466
446
467
If you feel like something is missing/wrong or if I've misinterpreted the JSON
447
- schema spec, feel free to open an issue so we can discuss a solution.
468
+ schema spec, feel free to open an issue so we can discuss a solution. Note that
469
+ the JSON schema parser has been moved to the new project,
470
+ https://github.com/dragonwasrobot/json_schema , so this repo only implements the
471
+ Elm code generators.
448
472
449
473
Please consult ` CONTRIBUTING.md ` first before opening an issue.
0 commit comments