Skip to content

Commit bb37100

Browse files
Updates generated code to Elm 0.19 (#74)
* Updates generated code to Elm 0.19 Now generates Elm 0.19 code, without any option for generating backwards compatible Elm 0.18 code. In the process, some dependencies and the README have been updated.
1 parent 9d5bd53 commit bb37100

38 files changed

+640
-489
lines changed

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
erlang 21.0
2-
elixir 1.7.0
2+
elixir 1.7.3-otp-21

README.md

+100-76
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,49 @@ If we supply `js2e` with the following JSON schema file, `definitions.json`:
6767
it produces the following Elm file, `Data/Definitions.elm`:
6868

6969
``` elm
70-
module Data.Definitions exposing (..)
70+
module Data.Definitions exposing
71+
( Color(..)
72+
, Point
73+
, colorDecoder
74+
, encodeColor
75+
, encodePoint
76+
, pointDecoder
77+
)
7178

7279
-- Schema for common types
7380

81+
import Data.Utils
82+
exposing
83+
( encodeNestedOptional
84+
, encodeNestedRequired
85+
, encodeOptional
86+
, encodeRequired
87+
)
7488
import Json.Decode as Decode
7589
exposing
76-
( succeed
90+
( Decoder
91+
, andThen
92+
, at
7793
, fail
78-
, map
79-
, maybe
8094
, field
8195
, index
82-
, at
83-
, andThen
84-
, oneOf
96+
, map
97+
, maybe
8598
, nullable
86-
, Decoder
99+
, oneOf
100+
, succeed
87101
)
88102
import Json.Decode.Pipeline
89103
exposing
90-
( decode
91-
, required
104+
( custom
92105
, optional
93-
, custom
106+
, required
94107
)
95108
import Json.Encode as Encode
96109
exposing
97110
( Value
98-
, object
99111
, list
112+
, object
100113
)
101114

102115

@@ -113,8 +126,8 @@ type alias Point =
113126
}
114127

115128

116-
colorDecoder : String -> Decoder Color
117-
colorDecoder color =
129+
colorDecoder : Decoder Color
130+
colorDecoder =
118131
Decode.string
119132
|> andThen
120133
(\color ->
@@ -138,7 +151,7 @@ colorDecoder color =
138151

139152
pointDecoder : Decoder Point
140153
pointDecoder =
141-
decode Point
154+
succeed Point
142155
|> required "x" Decode.float
143156
|> required "y" Decode.float
144157

@@ -161,14 +174,10 @@ encodeColor color =
161174

162175
encodePoint : Point -> Value
163176
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
172181
```
173182

174183
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,
204213
`Data/Definitions.elm`.
205214

206215
``` elm
207-
module Data.Circle exposing (..)
216+
module Data.Circle exposing
217+
( Circle
218+
, circleDecoder
219+
, encodeCircle
220+
)
208221

209222
-- Schema for a circle shape
210223

224+
import Data.Definitions as Definitions
225+
import Data.Utils
226+
exposing
227+
( encodeNestedOptional
228+
, encodeNestedRequired
229+
, encodeOptional
230+
, encodeRequired
231+
)
211232
import Json.Decode as Decode
212233
exposing
213-
( succeed
234+
( Decoder
235+
, andThen
236+
, at
214237
, fail
215-
, map
216-
, maybe
217238
, field
218239
, index
219-
, at
220-
, andThen
221-
, oneOf
240+
, map
241+
, maybe
222242
, nullable
223-
, Decoder
243+
, oneOf
244+
, succeed
224245
)
225246
import Json.Decode.Pipeline
226247
exposing
227-
( decode
228-
, required
248+
( custom
229249
, optional
230-
, custom
250+
, required
231251
)
232252
import Json.Encode as Encode
233253
exposing
234254
( Value
235-
, object
236255
, list
256+
, object
237257
)
238-
import Data.Definitions as Definitions
239258

240259

241260
type alias Circle =
@@ -247,53 +266,44 @@ type alias Circle =
247266

248267
circleDecoder : Decoder Circle
249268
circleDecoder =
250-
decode Circle
269+
succeed Circle
251270
|> required "center" Definitions.pointDecoder
252271
|> optional "color" (nullable Definitions.colorDecoder) Nothing
253272
|> required "radius" Decode.float
254273

255274

256275
encodeCircle : Circle -> Value
257276
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
277282
```
278283

279284
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,
283289
`tests/Data/CircleTests.elm` and `tests/Data/DefinitionsTests.elm`, are
284290
generated:
285291

286292
``` elm
287-
module Data.CircleTests exposing (..)
293+
module Data.CircleTests exposing
294+
( circleFuzzer
295+
, encodeDecodeCircleTest
296+
)
297+
288298

289299
-- Tests: Schema for a circle shape
290300

301+
import Data.Circle exposing (..)
302+
import Data.DefinitionsTests as Definitions
291303
import Expect exposing (Expectation)
292304
import Fuzz exposing (Fuzzer)
293-
import Test exposing (..)
294305
import Json.Decode as Decode
295-
import Data.Circle exposing (..)
296-
import Data.DefinitionsTests as Definitions
306+
import Test exposing (..)
297307

298308

299309
circleFuzzer : Fuzzer Circle
@@ -317,15 +327,20 @@ encodeDecodeCircleTest =
317327
and
318328

319329
``` elm
320-
module Data.DefinitionsTests exposing (..)
330+
module Data.DefinitionsTests exposing
331+
( colorFuzzer
332+
, encodeDecodeColorTest
333+
, encodeDecodePointTest
334+
, pointFuzzer
335+
)
321336

322337
-- Tests: Schema for common types
323338

339+
import Data.Definitions exposing (..)
324340
import Expect exposing (Expectation)
325341
import Fuzz exposing (Fuzzer)
326-
import Test exposing (..)
327342
import Json.Decode as Decode
328-
import Data.Definitions exposing (..)
343+
import Test exposing (..)
329344

330345

331346
colorFuzzer : Fuzzer Color
@@ -364,13 +379,18 @@ encodeDecodePointTest =
364379
|> encodePoint
365380
|> Decode.decodeValue pointDecoder
366381
|> Expect.equal (Ok point)
367-
368382
```
369383

370384
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:
374394

375395
```
376396
.
@@ -384,13 +404,14 @@ the following new directory structure is generated:
384404
```
385405
.
386406
└── js2e_output/
407+
├── .tool-versions
387408
├── 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
392414
└── tests/
393-
├── elm-package.json
394415
└── Data/
395416
├── CircleTests.elm
396417
└── DefinitionsTests.elm
@@ -444,6 +465,9 @@ the error.
444465
## Contributing
445466

446467
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.
448472

449473
Please consult `CONTRIBUTING.md` first before opening an issue.

examples/example-input-json-schemas/circle.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"description": "Schema for a circle shape",
66
"type": "object",
77
"properties": {
8-
"center": {
9-
"$ref": "http://example.com/definitions.json#point"
10-
},
118
"radius": {
129
"type": "number"
1310
},
11+
"center": {
12+
"$ref": "http://example.com/definitions.json#point"
13+
},
1414
"color": {
1515
"$ref": "http://example.com/definitions.json#color"
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
elm 0.19.0
2+
node 8.12.0

0 commit comments

Comments
 (0)