Skip to content

Commit 01fa892

Browse files
Elixir 1.6 (#56)
- Adds support for Elixir 1.6 (fixes issues related to a breaking change in the URI module). - Adds a .formatter.exs file - Closes #53
1 parent 6f4d432 commit 01fa892

Some content is hidden

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

55 files changed

+908
-325
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"],
3+
line_length: 80
4+
]

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
erlang 20.1
2-
elixir 1.5.1
2+
elixir 1.6.3

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ so it can be fixed.
3939
If we supply `js2e` with the following JSON schema file, `definitions.json`:
4040
``` json
4141
{
42-
"$schema": "http://json-schema.org/draft-04/schema",
42+
"$schema": "http://json-schema.org/draft-04/schema#",
4343
"title": "Definitions",
4444
"id": "http://example.com/definitions.json",
4545
"description": "Schema for common types",
@@ -66,10 +66,10 @@ If we supply `js2e` with the following JSON schema file, `definitions.json`:
6666
}
6767
```
6868

69-
it produces the following Elm file, `Domain/Definitions.elm`:
69+
it produces the following Elm file, `Data/Definitions.elm`:
7070

7171
``` elm
72-
module Domain.Definitions exposing (..)
72+
module Data.Definitions exposing (..)
7373

7474
-- Schema for common types
7575

@@ -177,7 +177,7 @@ that have references across files, e.g.
177177

178178
``` json
179179
{
180-
"$schema": "http://json-schema.org/draft-04/schema",
180+
"$schema": "http://json-schema.org/draft-04/schema#",
181181
"title": "Circle",
182182
"id": "http://example.com/circle.json",
183183
"description": "Schema for a circle shape",
@@ -197,12 +197,12 @@ that have references across files, e.g.
197197
}
198198
```
199199

200-
then the corresponding Elm file, `Domain/Circle.elm`, will import the
200+
then the corresponding Elm file, `Data/Circle.elm`, will import the
201201
definitions (types, encoders and decoders) from the other Elm module,
202-
`Domain/Definitions.elm`.
202+
`Data/Definitions.elm`.
203203

204204
``` elm
205-
module Domain.Circle exposing (..)
205+
module Data.Circle exposing (..)
206206

207207
-- Schema for a circle shape
208208

@@ -233,34 +233,34 @@ import Json.Encode as Encode
233233
, object
234234
, list
235235
)
236-
import Domain.Definitions
236+
import Data.Definitions
237237

238238

239239
type alias Circle =
240-
{ center : Domain.Definitions.Point
241-
, color : Maybe Domain.Definitions.Color
240+
{ center : Data.Definitions.Point
241+
, color : Maybe Data.Definitions.Color
242242
, radius : Float
243243
}
244244

245245

246246
circleDecoder : Decoder Circle
247247
circleDecoder =
248248
decode Circle
249-
|> required "center" Domain.Definitions.pointDecoder
250-
|> optional "color" (Decode.string |> andThen Domain.Definitions.colorDecoder |> maybe) Nothing
249+
|> required "center" Data.Definitions.pointDecoder
250+
|> optional "color" (Decode.string |> andThen Data.Definitions.colorDecoder |> maybe) Nothing
251251
|> required "radius" Decode.float
252252

253253

254254
encodeCircle : Circle -> Value
255255
encodeCircle circle =
256256
let
257257
center =
258-
[ ( "center", Domain.Definitions.encodePoint circle.center ) ]
258+
[ ( "center", Data.Definitions.encodePoint circle.center ) ]
259259

260260
color =
261261
case circle.color of
262262
Just color ->
263-
[ ( "color", Domain.Definitions.encodeColor color ) ]
263+
[ ( "color", Data.Definitions.encodeColor color ) ]
264264

265265
Nothing ->
266266
[]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "http://json-schema.org/draft-04/schema",
2+
"$schema": "http://json-schema.org/draft-04/schema#",
33
"title": "Circle",
44
"id": "http://example.com/circle.json",
55
"description": "Schema for a circle shape",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "http://json-schema.org/draft-04/schema",
2+
"$schema": "http://json-schema.org/draft-04/schema#",
33
"title": "Definitions",
44
"id": "http://example.com/definitions.json",
55
"description": "Schema for common types",

examples/example-output-elm-code/Domain/Circle.elm renamed to examples/example-output-elm-code/Data/Circle.elm

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Domain.Circle exposing (..)
1+
module Data.Circle exposing (..)
22

33
-- Schema for a circle shape
44

@@ -29,34 +29,34 @@ import Json.Encode as Encode
2929
, object
3030
, list
3131
)
32-
import Domain.Definitions
32+
import Data.Definitions
3333

3434

3535
type alias Circle =
36-
{ center : Domain.Definitions.Point
37-
, color : Maybe Domain.Definitions.Color
36+
{ center : Data.Definitions.Point
37+
, color : Maybe Data.Definitions.Color
3838
, radius : Float
3939
}
4040

4141

4242
circleDecoder : Decoder Circle
4343
circleDecoder =
4444
decode Circle
45-
|> required "center" Domain.Definitions.pointDecoder
46-
|> optional "color" (Decode.string |> andThen Domain.Definitions.colorDecoder |> maybe) Nothing
45+
|> required "center" Data.Definitions.pointDecoder
46+
|> optional "color" (Decode.string |> andThen Data.Definitions.colorDecoder |> maybe) Nothing
4747
|> required "radius" Decode.float
4848

4949

5050
encodeCircle : Circle -> Value
5151
encodeCircle circle =
5252
let
5353
center =
54-
[ ( "center", Domain.Definitions.encodePoint circle.center ) ]
54+
[ ( "center", Data.Definitions.encodePoint circle.center ) ]
5555

5656
color =
5757
case circle.color of
5858
Just color ->
59-
[ ( "color", Domain.Definitions.encodeColor color ) ]
59+
[ ( "color", Data.Definitions.encodeColor color ) ]
6060

6161
Nothing ->
6262
[]

examples/example-output-elm-code/Domain/Definitions.elm renamed to examples/example-output-elm-code/Data/Definitions.elm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Domain.Definitions exposing (..)
1+
module Data.Definitions exposing (..)
22

33
-- Schema for common types
44

lib/js2e.ex

+32-15
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ defmodule JS2E do
1414
## Options
1515
1616
* `--module-name` - the module name prefix for the printed Elm modules \
17-
default value is 'Domain'.
17+
default value is 'Data'.
1818
"""
1919

2020
require Logger
21-
import JS2E.Parser, only: [parse_schema_files: 1]
22-
import JS2E.Printer, only: [print_schemas: 2]
21+
alias JS2E.Parser
22+
alias JS2E.Printer
2323
alias JS2E.Parsers.{ParserWarning, ParserError}
2424
alias JS2E.Printers.PrinterError
2525

2626
@spec main([String.t()]) :: :ok
2727
def main(args) do
28-
{options, paths, errors} = OptionParser.parse(args, switches: [module_name: :string])
28+
{options, paths, errors} =
29+
OptionParser.parse(args, switches: [module_name: :string])
2930

30-
if length(paths) == 0 do
31+
if Enum.empty?(paths) == true do
3132
IO.puts(@moduledoc)
3233
exit(:normal)
3334
end
@@ -39,13 +40,16 @@ defmodule JS2E do
3940

4041
files = resolve_all_paths(paths)
4142

42-
if length(files) == 0 do
43-
print_error("Error: Could not find any " <> "JSON files in path: #{inspect(paths)}")
43+
if Enum.empty?(files) == true do
44+
print_error(
45+
"Error: Could not find any " <> "JSON files in path: #{inspect(paths)}"
46+
)
47+
4448
exit(:no_files)
4549
end
4650

4751
output_path = create_output_dir(options)
48-
JS2E.generate(files, output_path)
52+
generate(files, output_path)
4953
end
5054

5155
@spec resolve_all_paths([String.t()]) :: [Path.t()]
@@ -92,7 +96,7 @@ defmodule JS2E do
9296
if Keyword.has_key?(options, :module_name) do
9397
Keyword.get(options, :module_name)
9498
else
95-
"Domain"
99+
"Data"
96100
end
97101

98102
output_path
@@ -104,14 +108,16 @@ defmodule JS2E do
104108
@spec generate([String.t()], String.t()) :: :ok
105109
def generate(schema_paths, module_name) do
106110
Logger.info("Parsing JSON schema files!")
107-
parser_result = parse_schema_files(schema_paths)
111+
parser_result = Parser.parse_schema_files(schema_paths)
108112
pretty_parser_warnings(parser_result.warnings)
109113

110114
if length(parser_result.errors) > 0 do
111115
pretty_parser_errors(parser_result.errors)
112116
else
113117
Logger.info("Converting to Elm code!")
114-
printer_result = print_schemas(parser_result.schema_dict, module_name)
118+
119+
printer_result =
120+
Printer.print_schemas(parser_result.schema_dict, module_name)
115121

116122
if length(printer_result.errors) > 0 do
117123
pretty_printer_errors(printer_result.errors)
@@ -149,7 +155,11 @@ defmodule JS2E do
149155
padding =
150156
String.duplicate(
151157
"-",
152-
max(0, 74 - String.length(pretty_warning_type) - String.length(file_path))
158+
max(
159+
0,
160+
74 - String.length(pretty_warning_type) -
161+
String.length(file_path)
162+
)
153163
)
154164

155165
warnings
@@ -181,7 +191,10 @@ defmodule JS2E do
181191
padding =
182192
String.duplicate(
183193
"-",
184-
max(0, 74 - String.length(pretty_error_type) - String.length(file_path))
194+
max(
195+
0,
196+
74 - String.length(pretty_error_type) - String.length(file_path)
197+
)
185198
)
186199

187200
errors
@@ -213,7 +226,10 @@ defmodule JS2E do
213226
padding =
214227
String.duplicate(
215228
"-",
216-
max(0, 74 - String.length(pretty_error_type) - String.length(file_path))
229+
max(
230+
0,
231+
74 - String.length(pretty_error_type) - String.length(file_path)
232+
)
217233
)
218234

219235
errors
@@ -237,7 +253,8 @@ defmodule JS2E do
237253
end
238254

239255
defp warning_header do
240-
header = String.duplicate("^", 35) <> " WARNINGS " <> String.duplicate("^", 35)
256+
header =
257+
String.duplicate("^", 35) <> " WARNINGS " <> String.duplicate("^", 35)
241258

242259
IO.puts(IO.ANSI.format([:yellow, header]))
243260
end

lib/parsers/all_of_parser.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ defmodule JS2E.Parsers.AllOfParser do
6969
Parses a JSON schema allOf type into an `JS2E.Types.AllOfType`.
7070
"""
7171
@impl JS2E.Parsers.ParserBehaviour
72-
@spec parse(Types.node(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) :: ParserResult.t()
72+
@spec parse(Types.node(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
73+
ParserResult.t()
7374
def parse(%{"allOf" => all_of}, parent_id, id, path, name)
7475
when is_list(all_of) do
7576
child_path = TypePath.add_child(path, "allOf")

lib/parsers/any_of_parser.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ defmodule JS2E.Parsers.AnyOfParser do
6868
Parses a JSON schema anyOf type into an `JS2E.Types.AnyOfType`.
6969
"""
7070
@impl JS2E.Parsers.ParserBehaviour
71-
@spec parse(Types.schemaNode(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
72-
ParserResult.t()
71+
@spec parse(
72+
Types.schemaNode(),
73+
URI.t(),
74+
URI.t() | nil,
75+
TypePath.t(),
76+
String.t()
77+
) :: ParserResult.t()
7378
def parse(%{"anyOf" => any_of}, parent_id, id, path, name)
7479
when is_list(any_of) do
7580
child_path = TypePath.add_child(path, "anyOf")

lib/parsers/array_parser.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ defmodule JS2E.Parsers.ArrayParser do
5252
Parses a JSON schema array type into an `JS2E.Types.ArrayType`.
5353
"""
5454
@impl JS2E.Parsers.ParserBehaviour
55-
@spec parse(Types.schemaNode(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
56-
ParserResult.t()
55+
@spec parse(
56+
Types.schemaNode(),
57+
URI.t(),
58+
URI.t() | nil,
59+
TypePath.t(),
60+
String.t()
61+
) :: ParserResult.t()
5762
def parse(schema_node, parent_id, id, path, name) do
5863
items_abs_path =
5964
path

lib/parsers/definitions_parser.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ defmodule JS2E.Parsers.DefinitionsParser do
4949
Parses a JSON schema 'definitions' property into a map of types.
5050
"""
5151
@impl JS2E.Parsers.ParserBehaviour
52-
@spec parse(map, URI.t(), URI.t() | nil, TypePath.t(), String.t()) :: ParserResult.t()
52+
@spec parse(map, URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
53+
ParserResult.t()
5354
def parse(%{"definitions" => definitions}, parent_id, _id, path, _name) do
5455
child_path =
5556
path

lib/parsers/enum_parser.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ defmodule JS2E.Parsers.EnumParser do
4646
Parses a JSON schema enum type into an `JS2E.Types.EnumType`.
4747
"""
4848
@impl JS2E.Parsers.ParserBehaviour
49-
@spec parse(Types.schemaNode(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
50-
ParserResult.t()
49+
@spec parse(
50+
Types.schemaNode(),
51+
URI.t(),
52+
URI.t() | nil,
53+
TypePath.t(),
54+
String.t()
55+
) :: ParserResult.t()
5156
def parse(%{"enum" => enum, "type" => type}, _parent_id, id, path, name) do
5257
# TODO: Check that the enum values all have the same type
5358

lib/parsers/error_util.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ defmodule JS2E.Parsers.ErrorUtil do
109109
ParserError.new(identifier, :name_collision, error_msg)
110110
end
111111

112-
@spec invalid_uri(Types.typeIdentifier(), String.t(), String.t()) :: ParserError.t()
112+
@spec invalid_uri(Types.typeIdentifier(), String.t(), String.t()) ::
113+
ParserError.t()
113114
def invalid_uri(identifier, property, actual) do
114115
full_identifier = print_identifier(identifier)
115116
stringified_value = sanitize_value(actual)
@@ -127,7 +128,8 @@ defmodule JS2E.Parsers.ErrorUtil do
127128
ParserError.new(identifier, :invalid_uri, error_msg)
128129
end
129130

130-
@spec unknown_node_type(Types.typeIdentifier(), String.t(), Types.node()) :: ParserError.t()
131+
@spec unknown_node_type(Types.typeIdentifier(), String.t(), Types.node()) ::
132+
ParserError.t()
131133
def unknown_node_type(identifier, name, schema_node) do
132134
full_identifier =
133135
identifier

0 commit comments

Comments
 (0)