Skip to content

Commit 05238d6

Browse files
Fixes: Printer bugs and updates json_schema version
- Fixes: various minor bugs related to printing Elm code. - Adds: json_schema version 0.3 dependency. - Closes #95.
1 parent 69be52e commit 05238d6

31 files changed

+249
-144
lines changed

.tool-versions

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
erlang 21.3
2-
elixir 1.8.1
1+
erlang 22.0
2+
elixir 1.9.0

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: elixir
22

33
elixir:
4-
- '1.8.1'
4+
- '1.9.0'
55
otp_release:
6-
- '21.3'
6+
- '22.0'
77

88
cache:
99
directories:
@@ -18,7 +18,7 @@ install:
1818

1919
script:
2020
- mix test
21-
# - mix dialyzer --halt-exit-status
21+
- mix dialyzer
2222

2323
# Leave only the .plt files in build
2424
before_cache:

examples/example-output-elm-code/src/Data/Circle.elm

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
module Data.Circle exposing
2-
( Circle
3-
, circleDecoder
4-
, encodeCircle
5-
)
1+
module Data.Circle exposing (..)
62

73
-- Schema for a circle shape
84

9-
import Data.Definitions as Definitions
10-
import Data.Utils
11-
exposing
12-
( encodeNestedOptional
13-
, encodeNestedRequired
14-
, encodeOptional
15-
, encodeRequired
16-
)
175
import Json.Decode as Decode
186
exposing
197
( Decoder
@@ -40,6 +28,14 @@ import Json.Encode as Encode
4028
, list
4129
, object
4230
)
31+
import Data.Definitions as Definitions
32+
import Data.Utils
33+
exposing
34+
( encodeNestedOptional
35+
, encodeNestedRequired
36+
, encodeOptional
37+
, encodeRequired
38+
)
4339

4440

4541
type alias Circle =

examples/example-output-elm-code/src/Data/Definitions.elm

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
module Data.Definitions exposing
2-
( Color(..)
3-
, Point
4-
, colorDecoder
5-
, encodeColor
6-
, encodePoint
7-
, pointDecoder
8-
)
1+
module Data.Definitions exposing (..)
92

103
-- Schema for common types
114

12-
import Data.Utils
13-
exposing
14-
( encodeNestedOptional
15-
, encodeNestedRequired
16-
, encodeOptional
17-
, encodeRequired
18-
)
195
import Json.Decode as Decode
206
exposing
217
( Decoder
@@ -42,6 +28,13 @@ import Json.Encode as Encode
4228
, list
4329
, object
4430
)
31+
import Data.Utils
32+
exposing
33+
( encodeNestedOptional
34+
, encodeNestedRequired
35+
, encodeOptional
36+
, encodeRequired
37+
)
4538

4639

4740
type Color

examples/example-output-elm-code/src/Data/Utils.elm

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
module Data.Utils exposing
2-
( encodeNestedOptional
3-
, encodeNestedRequired
4-
, encodeOptional
5-
, encodeRequired
6-
)
1+
module Data.Utils
2+
exposing
3+
( encodeNestedOptional
4+
, encodeNestedRequired
5+
, encodeOptional
6+
, encodeRequired
7+
)
78

89
-- Util functions for decoding and encoding JSON objects.
910

examples/example-output-elm-code/tests/Data/CircleTests.elm

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
module Data.CircleTests exposing
2-
( circleFuzzer
3-
, encodeDecodeCircleTest
4-
)
1+
module Data.CircleTests exposing (..)
52

63
-- Tests: Schema for a circle shape
74

8-
import Data.Circle exposing (..)
9-
import Data.DefinitionsTests as Definitions
105
import Expect exposing (Expectation)
116
import Fuzz exposing (Fuzzer)
12-
import Json.Decode as Decode
137
import Test exposing (..)
8+
import Json.Decode as Decode
9+
import Data.Circle exposing (..)
10+
import Data.DefinitionsTests as Definitions
1411

1512

1613
circleFuzzer : Fuzzer Circle

examples/example-output-elm-code/tests/Data/DefinitionsTests.elm

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
module Data.DefinitionsTests exposing
2-
( colorFuzzer
3-
, encodeDecodeColorTest
4-
, encodeDecodePointTest
5-
, pointFuzzer
6-
)
1+
module Data.DefinitionsTests exposing (..)
72

83
-- Tests: Schema for common types
94

10-
import Data.Definitions exposing (..)
115
import Expect exposing (Expectation)
126
import Fuzz exposing (Fuzzer)
13-
import Json.Decode as Decode
147
import Test exposing (..)
8+
import Json.Decode as Decode
9+
import Data.Definitions exposing (..)
1510

1611

1712
colorFuzzer : Fuzzer Color

lib/js2e.ex

+14-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ defmodule JS2E do
2626

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

3132
if Enum.empty?(paths) == true do
3233
IO.puts(@moduledoc)
@@ -41,7 +42,9 @@ defmodule JS2E do
4142
files = resolve_all_paths(paths)
4243

4344
if Enum.empty?(files) == true do
44-
print_error("Error: Could not find any " <> "JSON files in path: #{inspect(paths)}")
45+
print_error(
46+
"Error: Could not find any JSON files in path: #{inspect(paths)}"
47+
)
4548

4649
exit(:no_files)
4750
end
@@ -122,9 +125,11 @@ defmodule JS2E do
122125
else
123126
Logger.info("Converting to Elm code!")
124127

125-
printer_result = Printer.print_schemas(parser_result.schema_dict, module_name)
128+
printer_result =
129+
Printer.print_schemas(parser_result.schema_dict, module_name)
126130

127-
tests_printer_result = Printer.print_schemas_tests(parser_result.schema_dict, module_name)
131+
tests_printer_result =
132+
Printer.print_schemas_tests(parser_result.schema_dict, module_name)
128133

129134
cond do
130135
length(printer_result.errors) > 0 ->
@@ -163,7 +168,7 @@ defmodule JS2E do
163168
String.replace(module_name, ".", "/")
164169
)
165170

166-
Logger.debug(fn -> "Writing file '#{normalized_file_path}'" end)
171+
Logger.debug("Writing file '#{normalized_file_path}'")
167172
{:ok, file} = File.open(normalized_file_path, [:write])
168173
IO.binwrite(file, file_content)
169174
File.close(file)
@@ -210,7 +215,8 @@ defmodule JS2E do
210215
"-",
211216
max(
212217
0,
213-
74 - String.length(pretty_warning_type) - String.length(file_path)
218+
74 - String.length(pretty_warning_type) -
219+
String.length(file_path)
214220
)
215221
)
216222

@@ -305,7 +311,8 @@ defmodule JS2E do
305311
end
306312

307313
defp warning_header do
308-
header = String.duplicate("^", 35) <> " WARNINGS " <> String.duplicate("^", 35)
314+
header =
315+
String.duplicate("^", 35) <> " WARNINGS " <> String.duplicate("^", 35)
309316

310317
IO.puts(IO.ANSI.format([:yellow, header]))
311318
end

lib/printer/all_of_printer.ex

+11-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ defmodule JS2E.Printer.AllOfPrinter do
5656

5757
{type_fields, errors} =
5858
types
59-
|> Enum.map(&create_type_field(&1, path, schema_def, schema_dict, module_name))
59+
|> Enum.map(
60+
&create_type_field(&1, path, schema_def, schema_dict, module_name)
61+
)
6062
|> CommonOperations.split_ok_and_errors()
6163

6264
type_name
@@ -83,7 +85,7 @@ defmodule JS2E.Printer.AllOfPrinter do
8385
field_type_result =
8486
type_path
8587
|> Resolver.resolve_type(parent, schema_def, schema_dict)
86-
|> ElmTypes.create_type_name(schema_def, module_name)
88+
|> ElmTypes.create_type_name(parent, schema_def, schema_dict, module_name)
8789

8890
case field_type_result do
8991
{:ok, field_type} ->
@@ -121,7 +123,9 @@ defmodule JS2E.Printer.AllOfPrinter do
121123
) do
122124
{decoder_clauses, errors} =
123125
type_paths
124-
|> Enum.map(&create_decoder_property(&1, path, schema_def, schema_dict, module_name))
126+
|> Enum.map(
127+
&create_decoder_property(&1, path, schema_def, schema_dict, module_name)
128+
)
125129
|> CommonOperations.split_ok_and_errors()
126130

127131
normalized_name = Naming.normalize_identifier(name, :downcase)
@@ -191,7 +195,8 @@ defmodule JS2E.Printer.AllOfPrinter do
191195
{:ok, %{property_name: property_name, decoder_name: decoder_name}}
192196
end
193197

194-
@spec create_decoder_enum_clause(String.t(), String.t(), String.t()) :: {:ok, map}
198+
@spec create_decoder_enum_clause(String.t(), String.t(), String.t()) ::
199+
{:ok, map}
195200
defp create_decoder_enum_clause(
196201
property_name,
197202
property_type_decoder,
@@ -329,7 +334,8 @@ defmodule JS2E.Printer.AllOfPrinter do
329334
_schema_dict,
330335
_module_name
331336
) do
332-
error_msg = "allOf printer expected ObjectType but found #{type_def.__struct__}"
337+
error_msg =
338+
"allOf printer expected ObjectType but found #{type_def.__struct__}"
333339

334340
ErrorUtil.unexpected_type(type_def.path, error_msg)
335341
end

lib/printer/any_of_printer.ex

+11-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ defmodule JS2E.Printer.AnyOfPrinter do
5555

5656
{type_fields, errors} =
5757
types
58-
|> Enum.map(&create_type_field(&1, path, schema_def, schema_dict, module_name))
58+
|> Enum.map(
59+
&create_type_field(&1, path, schema_def, schema_dict, module_name)
60+
)
5961
|> CommonOperations.split_ok_and_errors()
6062

6163
type_name
@@ -82,7 +84,7 @@ defmodule JS2E.Printer.AnyOfPrinter do
8284
field_type_result =
8385
type_path
8486
|> Resolver.resolve_type(parent, schema_def, schema_dict)
85-
|> ElmTypes.create_type_name(schema_def, module_name)
87+
|> ElmTypes.create_type_name(parent, schema_def, schema_dict, module_name)
8688

8789
case field_type_result do
8890
{:ok, field_type} ->
@@ -120,7 +122,9 @@ defmodule JS2E.Printer.AnyOfPrinter do
120122
) do
121123
{decoder_clauses, errors} =
122124
type_paths
123-
|> Enum.map(&create_decoder_property(&1, path, schema_def, schema_dict, module_name))
125+
|> Enum.map(
126+
&create_decoder_property(&1, path, schema_def, schema_dict, module_name)
127+
)
124128
|> CommonOperations.split_ok_and_errors()
125129

126130
normalized_name = Naming.normalize_identifier(name, :downcase)
@@ -190,7 +194,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
190194
{:ok, %{property_name: property_name, decoder_name: decoder_name}}
191195
end
192196

193-
@spec create_decoder_enum_clause(String.t(), String.t(), String.t()) :: {:ok, map}
197+
@spec create_decoder_enum_clause(String.t(), String.t(), String.t()) ::
198+
{:ok, map}
194199
defp create_decoder_enum_clause(
195200
property_name,
196201
property_type_decoder,
@@ -323,7 +328,8 @@ defmodule JS2E.Printer.AnyOfPrinter do
323328
_schema_dict,
324329
_module_name
325330
) do
326-
error_msg = "anyOf printer expected ObjectType but found #{type_def.__struct__}"
331+
error_msg =
332+
"anyOf printer expected ObjectType but found #{type_def.__struct__}"
327333

328334
ErrorUtil.unexpected_type(type_def.path, error_msg)
329335
end

lib/printer/array_printer.ex

+6-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ defmodule JS2E.Printer.ArrayPrinter do
104104
ElmDecoders.determine_primitive_type_decoder(items_type.type)
105105

106106
_ ->
107-
items_type_name = Naming.normalize_identifier(items_type.name, :downcase)
107+
items_type_name =
108+
Naming.normalize_identifier(items_type.name, :downcase)
108109

109110
if items_type_name == "hash" do
110111
{:ok, "rootDecoder"}
@@ -141,7 +142,7 @@ defmodule JS2E.Printer.ArrayPrinter do
141142
Resolver.resolve_type(items_path, path, schema_def, schema_dict),
142143
{:ok, items_type_name} <- determine_type_name(items_type),
143144
{:ok, items_encoder_name} <- determine_encoder_name(items_type) do
144-
"encode#{Naming.normalize_identifier(items_type_name, :upcase)}s"
145+
"encode#{Naming.normalize_identifier(name, :upcase)}"
145146
|> encoder_template(name, items_type_name, items_encoder_name)
146147
|> PrinterResult.new()
147148
else
@@ -205,8 +206,7 @@ defmodule JS2E.Printer.ArrayPrinter do
205206
argument_name = Naming.normalize_identifier(name, :downcase)
206207
fuzzer_name = "#{name}Fuzzer"
207208
decoder_name = "#{Naming.normalize_identifier(name, :downcase)}Decoder"
208-
209-
encoder_name = "encode#{Naming.normalize_identifier(items_type_name, :upcase)}s"
209+
encoder_name = "encode#{Naming.normalize_identifier(name, :upcase)}"
210210

211211
array_name
212212
|> fuzzer_template(
@@ -235,7 +235,8 @@ defmodule JS2E.Printer.ArrayPrinter do
235235
ElmFuzzers.determine_primitive_fuzzer_name(items_type.type)
236236

237237
_ ->
238-
items_type_name = Naming.normalize_identifier(items_type.name, :downcase)
238+
items_type_name =
239+
Naming.normalize_identifier(items_type.name, :downcase)
239240

240241
if items_type_name == "hash" do
241242
{:ok, "rootFuzzer"}

0 commit comments

Comments
 (0)