Skip to content

Commit 32669f4

Browse files
Adds: Refactorings of outputted Elm code and dependency updates
- Updates elixir, erlang, mix project and dependency versions. - Updates README with updated example output code. - Updates example input/output files. - Fixes some credo warnings regarding application.get_env. - Updates error messages to use properly static pointers to JSON schema standards. - Migrates all types to use `typed_struct` and also adds a proper `error_type` union type. - Simplifies `preamble.elm.eex` by removing all function imports and instead making qualified function calls, e.g. `succeed` becomes `Decode.succeed` in Elm decoders, and similarly for all other library calls expect the decode pipeline calls. - Renames `Util.elm` to `Encode.elm` and removes function imports turning calls such as `encodeRequired` into `Encode.required`. - Splits the parsing and decoding for enums into two so the `enumDecoder` calls a dedicated `parseEnum` function making it easier to reuse the `parseEnum` in other contexts. - Updates dependencies in the generated `.tool-versions`, `package.json` and `elm.json` files. - Adjusts tests accordingly to the changes above.
1 parent 455a445 commit 32669f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+547
-627
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# The packaged executable
22
js2e
33
/js2e_output
4+
node_modules
5+
elm-stuff
46

57
# The directory Mix will write compiled artifacts to.
68
/_build

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
erlang 22.0
2-
elixir 1.9.0
1+
erlang 24.0.6
2+
elixir 1.12.3

README.md

Lines changed: 48 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ JSON schema types are described in the `lib/types` folder.
4343
## Example
4444

4545
If we supply `js2e` with the following JSON schema file, `definitions.json`:
46-
``` javascript
46+
``` json
4747
{
4848
"$schema": "http://json-schema.org/draft-07/schema#",
4949
"title": "Definitions",
@@ -86,39 +86,16 @@ module Data.Definitions exposing
8686

8787
-- Schema for common types
8888

89-
import Data.Utils
90-
exposing
91-
( encodeNestedOptional
92-
, encodeNestedRequired
93-
, encodeOptional
94-
, encodeRequired
95-
)
96-
import Json.Decode as Decode
97-
exposing
98-
( Decoder
99-
, andThen
100-
, at
101-
, fail
102-
, field
103-
, index
104-
, map
105-
, maybe
106-
, nullable
107-
, oneOf
108-
, succeed
109-
)
89+
import Data.Encode as Encode
90+
import Json.Decode as Decode exposing (Decoder)
91+
import Json.Decode.Extra as Decode
11092
import Json.Decode.Pipeline
11193
exposing
11294
( custom
11395
, optional
11496
, required
11597
)
116-
import Json.Encode as Encode
117-
exposing
118-
( Value
119-
, list
120-
, object
121-
)
98+
import Json.Encode as Encode exposing (Value)
12299

123100

124101
type Color
@@ -136,55 +113,61 @@ type alias Point =
136113

137114
colorDecoder : Decoder Color
138115
colorDecoder =
139-
Decode.string
140-
|> andThen
141-
(\color ->
142-
case color of
143-
"red" ->
144-
succeed Red
116+
Decode.string |> Decode.andThen (parseColor >> Decode.fromResult)
117+
118+
119+
parseColor : String -> Result String Color
120+
parseColor color =
121+
case color of
122+
"red" ->
123+
Ok Red
145124

146-
"yellow" ->
147-
succeed Yellow
125+
"yellow" ->
126+
Ok Yellow
148127

149-
"green" ->
150-
succeed Green
128+
"green" ->
129+
Ok Green
151130

152-
"blue" ->
153-
succeed Blue
131+
"blue" ->
132+
Ok Blue
154133

155-
_ ->
156-
fail <| "Unknown color type: " ++ color
157-
)
134+
_ ->
135+
Err <| "Unknown color type: " ++ color
158136

159137

160138
pointDecoder : Decoder Point
161139
pointDecoder =
162-
succeed Point
140+
Decode.succeed Point
163141
|> required "x" Decode.float
164142
|> required "y" Decode.float
165143

166144

167145
encodeColor : Color -> Value
168146
encodeColor color =
147+
color |> colorToString |> Encode.string
148+
149+
150+
colorToString : Color -> String
151+
colorToString color =
169152
case color of
170153
Red ->
171-
Encode.string "red"
154+
"red"
172155

173156
Yellow ->
174-
Encode.string "yellow"
157+
"yellow"
175158

176159
Green ->
177-
Encode.string "green"
160+
"green"
178161

179162
Blue ->
180-
Encode.string "blue"
163+
"blue"
181164

182165

183166
encodePoint : Point -> Value
184167
encodePoint point =
185168
[]
186-
|> encodeRequired "x" point.x Encode.float
187-
|> encodeRequired "y" point.y Encode.float
169+
|> Encode.required "x" point.x Encode.float
170+
|> Encode.required "y" point.y Encode.float
188171
|> Encode.object
189172
```
190173

@@ -194,9 +177,9 @@ their corresponding JSON decoders and encoders.
194177
Furthermore, if we instead supply `js2e` with a directory of JSON schema files
195178
that have references across files, e.g.
196179

197-
``` javascript
180+
``` json
198181
{
199-
"$schema": "http://json-schema.org/draft-04/schema#",
182+
"$schema": "http://json-schema.org/draft-07/schema#",
200183
"$id": "http://example.com/circle.json",
201184
"title": "Circle",
202185
"description": "Schema for a circle shape",
@@ -230,39 +213,16 @@ module Data.Circle exposing
230213
-- Schema for a circle shape
231214

232215
import Data.Definitions as Definitions
233-
import Data.Utils
234-
exposing
235-
( encodeNestedOptional
236-
, encodeNestedRequired
237-
, encodeOptional
238-
, encodeRequired
239-
)
240-
import Json.Decode as Decode
241-
exposing
242-
( Decoder
243-
, andThen
244-
, at
245-
, fail
246-
, field
247-
, index
248-
, map
249-
, maybe
250-
, nullable
251-
, oneOf
252-
, succeed
253-
)
216+
import Data.Encode as Encode
217+
import Json.Decode as Decode exposing (Decoder)
218+
import Json.Decode.Extra as Decode
254219
import Json.Decode.Pipeline
255220
exposing
256221
( custom
257222
, optional
258223
, required
259224
)
260-
import Json.Encode as Encode
261-
exposing
262-
( Value
263-
, list
264-
, object
265-
)
225+
import Json.Encode as Encode exposing (Value)
266226

267227

268228
type alias Circle =
@@ -276,16 +236,16 @@ circleDecoder : Decoder Circle
276236
circleDecoder =
277237
succeed Circle
278238
|> required "center" Definitions.pointDecoder
279-
|> optional "color" (nullable Definitions.colorDecoder) Nothing
239+
|> optional "color" (Decode.nullable Definitions.colorDecoder) Nothing
280240
|> required "radius" Decode.float
281241

282242

283243
encodeCircle : Circle -> Value
284244
encodeCircle circle =
285245
[]
286-
|> encodeRequired "center" circle.center Definitions.encodePoint
287-
|> encodeOptional "color" circle.color Definitions.encodeColor
288-
|> encodeRequired "radius" circle.radius Encode.float
246+
|> Encode.required "center" circle.center Definitions.encodePoint
247+
|> Encode.optional "color" circle.color Definitions.encodeColor
248+
|> Encode.required "radius" circle.radius Encode.float
289249
|> Encode.object
290250
```
291251

@@ -353,12 +313,9 @@ import Test exposing (..)
353313

354314
colorFuzzer : Fuzzer Color
355315
colorFuzzer =
356-
Fuzz.oneOf
357-
[ Fuzz.constant Red
358-
, Fuzz.constant Yellow
359-
, Fuzz.constant Green
360-
, Fuzz.constant Blue
361-
]
316+
[ Red, Yellow, Green, Blue ]
317+
|> List.map Fuzz.constant
318+
|> Fuzz.oneOf
362319

363320

364321
encodeDecodeColorTest : Test
@@ -415,8 +372,9 @@ the following new directory structure is generated:
415372
├── .tool-versions
416373
├── package.json
417374
├── elm.json
418-
├── tests/
375+
├── src/
419376
│ └── Data/
377+
│ ├── Encode.elm
420378
│ ├── Circle.elm
421379
│ └── Definitions.elm
422380
└── tests/

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Mix.Config
1+
import Config
22

33
config :elixir, ansi_enabled: true
44

config/dev.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
use Mix.Config
1+
import Config
22

33
config :logger, level: :debug

config/prod.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
use Mix.Config
1+
import Config
22

33
config :logger, level: :warn

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
use Mix.Config
1+
import Config
22

33
config :logger, level: :debug
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
{
2-
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"$id": "http://example.com/definitions.json",
4-
"title": "Definitions",
5-
"description": "Schema for common types",
6-
"definitions": {
7-
"color": {
8-
"$id": "#color",
9-
"type": "string",
10-
"enum": [ "red", "yellow", "green", "blue" ]
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://example.com/definitions.json",
4+
"title": "Definitions",
5+
"description": "Schema for common types",
6+
"definitions": {
7+
"color": {
8+
"$id": "#color",
9+
"type": "string",
10+
"enum": [ "red", "yellow", "green", "blue" ]
11+
},
12+
"point": {
13+
"$id": "#point",
14+
"type": "object",
15+
"properties": {
16+
"x": {
17+
"type": "number"
1118
},
12-
"point": {
13-
"$id": "#point",
14-
"type": "object",
15-
"properties": {
16-
"x": {
17-
"type": "number"
18-
},
19-
"y": {
20-
"type": "number"
21-
}
22-
},
23-
"required": [ "x", "y" ]
19+
"y": {
20+
"type": "number"
2421
}
22+
},
23+
"required": [ "x", "y" ]
2524
}
25+
}
2626
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
elm 0.19.0
1+
elm 0.19.1
22
node 8.12.0
Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
{
2-
"type": "application",
3-
"source-directories": [
4-
"src"
5-
],
6-
"elm-version": "0.19.0",
7-
"dependencies": {
8-
"direct": {
9-
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
10-
"elm/browser": "1.0.1",
11-
"elm/core": "1.0.0",
12-
"elm/html": "1.0.0",
13-
"elm/json": "1.0.0"
14-
},
15-
"indirect": {
16-
"elm/random": "1.0.0",
17-
"elm/time": "1.0.0",
18-
"elm/url": "1.0.0",
19-
"elm/virtual-dom": "1.0.2"
20-
}
2+
"type": "application",
3+
"source-directories": [
4+
"src"
5+
],
6+
"elm-version": "0.19.1",
7+
"dependencies": {
8+
"direct": {
9+
"NoRedInk/elm-json-decode-pipeline": "1.0.0",
10+
"elm/browser": "1.0.1",
11+
"elm/core": "1.0.0",
12+
"elm/html": "1.0.0",
13+
"elm/json": "1.0.0",
14+
"elm-community/json-extra": "4.3.0"
2115
},
22-
"test-dependencies": {
23-
"direct": {
24-
"elm-explorations/test": "1.2.0"
25-
},
26-
"indirect": {}
16+
"indirect": {
17+
"elm/parser": "1.1.0",
18+
"elm/time": "1.0.0",
19+
"elm/url": "1.0.0",
20+
"elm/virtual-dom": "1.0.2",
21+
"rtfeldman/elm-iso8601-date-strings": "1.1.4"
2722
}
23+
},
24+
"test-dependencies": {
25+
"direct": {
26+
"elm-explorations/test": "1.2.0"
27+
},
28+
"indirect": {
29+
"elm/random": "1.0.0"
30+
}
31+
}
2832
}

0 commit comments

Comments
 (0)