Skip to content

Commit cbe9d6e

Browse files
Adds printer behaviour
1 parent f986d33 commit cbe9d6e

11 files changed

+121
-157
lines changed

lib/printers/all_of_printer.ex

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule JS2E.Printers.AllOfPrinter do
2+
@behaviour JS2E.Printers.PrinterBehaviour
23
@moduledoc """
34
A printer for printing an 'all of' type decoder.
45
"""
@@ -22,11 +23,9 @@ defmodule JS2E.Printers.AllOfPrinter do
2223
EEx.function_from_file(:defp, :encoder_template, @encoder_location,
2324
[:encoder_name, :type_name, :argument_name, :properties])
2425

25-
@spec print_type(
26-
Types.typeDefinition,
27-
SchemaDefinition.t,
28-
Types.schemaDictionary
29-
) :: String.t
26+
@impl JS2E.Printers.PrinterBehaviour
27+
@spec print_type(Types.typeDefinition, SchemaDefinition.t,
28+
Types.schemaDictionary) :: String.t
3029
def print_type(%AllOfType{name: name,
3130
path: _path,
3231
types: types}, schema_def, schema_dict) do
@@ -37,20 +36,14 @@ defmodule JS2E.Printers.AllOfPrinter do
3736
type_template(type_name, fields)
3837
end
3938

40-
@spec create_type_fields(
41-
[TypePath.t],
42-
SchemaDefinition.t,
43-
Types.schemaDictionary
44-
) :: [map]
39+
@spec create_type_fields([TypePath.t], SchemaDefinition.t,
40+
Types.schemaDictionary) :: [map]
4541
defp create_type_fields(types, schema_def, schema_dict) do
4642
types |> Enum.map(&(create_type_field(&1, schema_def, schema_dict)))
4743
end
4844

49-
@spec create_type_field(
50-
TypePath.t,
51-
SchemaDefinition.t,
52-
Types.schemaDictionary
53-
) :: map
45+
@spec create_type_field(TypePath.t, SchemaDefinition.t,
46+
Types.schemaDictionary) :: map
5447
defp create_type_field(type_path, schema_def, schema_dict) do
5548

5649
field_type =
@@ -64,11 +57,9 @@ defmodule JS2E.Printers.AllOfPrinter do
6457
type: field_type}
6558
end
6659

67-
@spec print_decoder(
68-
Types.typeDefinition,
69-
SchemaDefinition.t,
70-
Types.schemaDictionary
71-
) :: String.t
60+
@impl JS2E.Printers.PrinterBehaviour
61+
@spec print_decoder(Types.typeDefinition, SchemaDefinition.t,
62+
Types.schemaDictionary) :: String.t
7263
def print_decoder(%AllOfType{name: name,
7364
path: _path,
7465
types: type_paths},
@@ -144,11 +135,9 @@ defmodule JS2E.Printers.AllOfPrinter do
144135
decoder_name: decoder_name}
145136
end
146137

147-
@spec print_encoder(
148-
Types.typeDefinition,
149-
SchemaDefinition.t,
150-
Types.schemaDictionary
151-
) :: String.t
138+
@impl JS2E.Printers.PrinterBehaviour
139+
@spec print_encoder(Types.typeDefinition, SchemaDefinition.t,
140+
Types.schemaDictionary) :: String.t
152141
def print_encoder(%AllOfType{name: name,
153142
path: _path,
154143
types: type_paths},

lib/printers/any_of_printer.ex

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule JS2E.Printers.AnyOfPrinter do
2+
@behaviour JS2E.Printers.PrinterBehaviour
23
@moduledoc """
34
A printer for printing an 'any of' type decoder.
45
"""
@@ -22,11 +23,9 @@ defmodule JS2E.Printers.AnyOfPrinter do
2223
EEx.function_from_file(:defp, :encoder_template, @encoder_location,
2324
[:encoder_name, :type_name, :argument_name, :properties])
2425

25-
@spec print_type(
26-
Types.typeDefinition,
27-
SchemaDefinition.t,
28-
Types.schemaDictionary
29-
) :: String.t
26+
@impl JS2E.Printers.PrinterBehaviour
27+
@spec print_type(Types.typeDefinition, SchemaDefinition.t,
28+
Types.schemaDictionary) :: String.t
3029
def print_type(%AnyOfType{name: name,
3130
path: _path,
3231
types: types}, schema_def, schema_dict) do
@@ -66,20 +65,18 @@ defmodule JS2E.Printers.AnyOfPrinter do
6665
end
6766

6867
@spec create_type_name({Types.typeDefinition, SchemaDefinition.t}) :: String.t
69-
defp create_type_name({property_type, schema_def}) do
68+
defp create_type_name({property_type, _schema_def}) do
7069

7170
if primitive_type?(property_type) do
7271
determine_primitive_type!(property_type.type)
7372
else
74-
property_type_name = upcase_first property_type.name
73+
upcase_first property_type.name
7574
end
7675
end
7776

78-
@spec print_decoder(
79-
Types.typeDefinition,
80-
SchemaDefinition.t,
81-
Types.schemaDictionary
82-
) :: String.t
77+
@impl JS2E.Printers.PrinterBehaviour
78+
@spec print_decoder(Types.typeDefinition, SchemaDefinition.t,
79+
Types.schemaDictionary) :: String.t
8380
def print_decoder(%AnyOfType{name: name,
8481
path: _path,
8582
types: type_paths},
@@ -112,7 +109,7 @@ defmodule JS2E.Printers.AnyOfPrinter do
112109
) :: map
113110
defp create_decoder_property(type_path, schema_def, schema_dict) do
114111

115-
{property_type, resolved_schema_def} =
112+
{property_type, _resolved_schema_def} =
116113
type_path
117114
|> Printer.resolve_type!(schema_def, schema_dict)
118115

@@ -165,11 +162,9 @@ defmodule JS2E.Printers.AnyOfPrinter do
165162
decoder_name: decoder_name}
166163
end
167164

168-
@spec print_encoder(
169-
Types.typeDefinition,
170-
SchemaDefinition.t,
171-
Types.schemaDictionary
172-
) :: String.t
165+
@impl JS2E.Printers.PrinterBehaviour
166+
@spec print_encoder(Types.typeDefinition, SchemaDefinition.t,
167+
Types.schemaDictionary) :: String.t
173168
def print_encoder(%AnyOfType{name: name,
174169
path: _path,
175170
types: type_paths},

lib/printers/array_printer.ex

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule JS2E.Printers.ArrayPrinter do
2+
@behaviour JS2E.Printers.PrinterBehaviour
23
@moduledoc """
34
A printer for printing an 'array' type decoder.
45
"""
@@ -18,27 +19,23 @@ defmodule JS2E.Printers.ArrayPrinter do
1819
EEx.function_from_file(:defp, :encoder_template, @encoder_location,
1920
[:encoder_name, :argument_name, :items_type_name, :items_encoder_name])
2021

21-
@spec print_type(
22-
Types.typeDefinition,
23-
SchemaDefinition.t,
24-
Types.schemaDictionary
25-
) :: String.t
22+
@impl JS2E.Printers.PrinterBehaviour
23+
@spec print_type(Types.typeDefinition, SchemaDefinition.t,
24+
Types.schemaDictionary) :: String.t
2625
def print_type(%ArrayType{name: _name,
2726
path: _path,
2827
items: _items_path}, _schema_def, _schema_dict) do
2928
""
3029
end
3130

32-
@spec print_decoder(
33-
Types.typeDefinition,
34-
SchemaDefinition.t,
35-
Types.schemaDictionary
36-
) :: String.t
31+
@impl JS2E.Printers.PrinterBehaviour
32+
@spec print_decoder(Types.typeDefinition, SchemaDefinition.t,
33+
Types.schemaDictionary) :: String.t
3734
def print_decoder(%ArrayType{name: name,
3835
path: _path,
3936
items: items_path}, schema_def, schema_dict) do
4037

41-
{items_type, resolved_schema_def} =
38+
{items_type, _resolved_schema_def} =
4239
items_path
4340
|> Printer.resolve_type!(schema_def, schema_dict)
4441

@@ -83,15 +80,13 @@ defmodule JS2E.Printers.ArrayPrinter do
8380
end
8481
end
8582

86-
@spec print_encoder(
87-
Types.typeDefinition,
88-
SchemaDefinition.t,
89-
Types.schemaDictionary
90-
) :: String.t
83+
@impl JS2E.Printers.PrinterBehaviour
84+
@spec print_encoder(Types.typeDefinition, SchemaDefinition.t,
85+
Types.schemaDictionary) :: String.t
9186
def print_encoder(%ArrayType{name: name, path: _path, items: items_path},
9287
schema_def, schema_dict) do
9388

94-
{items_type, resolved_schema_def} =
89+
{items_type, _resolved_schema_def} =
9590
items_path
9691
|> Printer.resolve_type!(schema_def, schema_dict)
9792

lib/printers/enum_printer.ex

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule JS2E.Printers.EnumPrinter do
2+
@behaviour JS2E.Printers.PrinterBehaviour
23
@moduledoc """
34
Prints the Elm type, JSON decoder and JSON eecoder for a JSON schema 'enum'.
45
"""
@@ -22,11 +23,9 @@ defmodule JS2E.Printers.EnumPrinter do
2223
EEx.function_from_file(:defp, :encoder_template, @encoder_location,
2324
[:encoder_name, :argument_name, :argument_type, :cases])
2425

25-
@spec print_type(
26-
Types.typeDefinition,
27-
SchemaDefinition.t,
28-
Types.schemaDictionary
29-
) :: String.t
26+
@impl JS2E.Printers.PrinterBehaviour
27+
@spec print_type(Types.typeDefinition, SchemaDefinition.t,
28+
Types.schemaDictionary) :: String.t
3029
def print_type(%EnumType{name: name,
3130
path: _path,
3231
type: type,
@@ -38,11 +37,9 @@ defmodule JS2E.Printers.EnumPrinter do
3837
type_template(type_name, clauses)
3938
end
4039

41-
@spec print_decoder(
42-
Types.typeDefinition,
43-
SchemaDefinition.t,
44-
Types.schemaDictionary
45-
) :: String.t
40+
@impl JS2E.Printers.PrinterBehaviour
41+
@spec print_decoder(Types.typeDefinition, SchemaDefinition.t,
42+
Types.schemaDictionary) :: String.t
4643
def print_decoder(%EnumType{name: name,
4744
path: _path,
4845
type: type,
@@ -85,11 +82,9 @@ defmodule JS2E.Printers.EnumPrinter do
8582
end
8683
end
8784

88-
@spec print_encoder(
89-
Types.typeDefinition,
90-
SchemaDefinition.t,
91-
Types.schemaDictionary
92-
) :: String.t
85+
@impl JS2E.Printers.PrinterBehaviour
86+
@spec print_encoder(Types.typeDefinition, SchemaDefinition.t,
87+
Types.schemaDictionary) :: String.t
9388
def print_encoder(%EnumType{name: name,
9489
path: _path,
9590
type: type,

lib/printers/object_printer.ex

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule JS2E.Printers.ObjectPrinter do
2+
@behaviour JS2E.Printers.PrinterBehaviour
23
@moduledoc """
34
A printer for printing an 'object' type decoder.
45
"""
@@ -22,11 +23,9 @@ defmodule JS2E.Printers.ObjectPrinter do
2223
EEx.function_from_file(:defp, :encoder_template, @encoder_location,
2324
[:encoder_name, :type_name, :argument_name, :properties])
2425

25-
@spec print_type(
26-
Types.typeDefinition,
27-
SchemaDefinition.t,
28-
Types.schemaDictionary
29-
) :: String.t
26+
@impl JS2E.Printers.PrinterBehaviour
27+
@spec print_type(Types.typeDefinition, SchemaDefinition.t,
28+
Types.schemaDictionary) :: String.t
3029
def print_type(%ObjectType{name: name,
3130
path: _path,
3231
properties: properties,
@@ -86,11 +85,9 @@ defmodule JS2E.Printers.ObjectPrinter do
8685
type: field_type}
8786
end
8887

89-
@spec print_decoder(
90-
Types.typeDefinition,
91-
SchemaDefinition.t,
92-
Types.schemaDictionary
93-
) :: String.t
88+
@impl JS2E.Printers.PrinterBehaviour
89+
@spec print_decoder(Types.typeDefinition, SchemaDefinition.t,
90+
Types.schemaDictionary) :: String.t
9491
def print_decoder(%ObjectType{name: name,
9592
path: _path,
9693
properties: properties,
@@ -203,11 +200,9 @@ defmodule JS2E.Printers.ObjectPrinter do
203200
end
204201
end
205202

206-
@spec print_encoder(
207-
Types.typeDefinition,
208-
SchemaDefinition.t,
209-
Types.schemaDictionary
210-
) :: String.t
203+
@impl JS2E.Printers.PrinterBehaviour
204+
@spec print_encoder(Types.typeDefinition, SchemaDefinition.t,
205+
Types.schemaDictionary) :: String.t
211206
def print_encoder(%ObjectType{name: name,
212207
path: _path,
213208
properties: properties,

lib/printers/one_of_printer.ex

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule JS2E.Printers.OneOfPrinter do
2+
@behaviour JS2E.Printers.PrinterBehaviour
23
@moduledoc """
34
A printer for printing a 'one of' type decoder.
45
"""
@@ -22,11 +23,9 @@ defmodule JS2E.Printers.OneOfPrinter do
2223
EEx.function_from_file(:defp, :encoder_template, @encoder_location,
2324
[:encoder_name, :type_name, :argument_name, :cases])
2425

25-
@spec print_type(
26-
Types.typeDefinition,
27-
SchemaDefinition.t,
28-
Types.schemaDictionary
29-
) :: String.t
26+
@impl JS2E.Printers.PrinterBehaviour
27+
@spec print_type(Types.typeDefinition, SchemaDefinition.t,
28+
Types.schemaDictionary) :: String.t
3029
def print_type(%OneOfType{name: name,
3130
path: _path,
3231
types: types}, schema_def, schema_dict) do
@@ -48,7 +47,7 @@ defmodule JS2E.Printers.OneOfPrinter do
4847
type_name = upcase_first name
4948

5049
create_type_clause = fn type_path ->
51-
{clause_type, resolved_schema_def} =
50+
{clause_type, _resolved_schema_def} =
5251
type_path
5352
|> Printer.resolve_type!(schema_def, schema_dict)
5453

@@ -67,11 +66,9 @@ defmodule JS2E.Printers.OneOfPrinter do
6766
|> Enum.map(create_type_clause)
6867
end
6968

70-
@spec print_decoder(
71-
Types.typeDefinition,
72-
SchemaDefinition.t,
73-
Types.schemaDictionary
74-
) :: String.t
69+
@impl JS2E.Printers.PrinterBehaviour
70+
@spec print_decoder(Types.typeDefinition, SchemaDefinition.t,
71+
Types.schemaDictionary) :: String.t
7572
def print_decoder(%OneOfType{name: name,
7673
path: _path,
7774
types: types}, schema_def, schema_dict) do
@@ -91,7 +88,7 @@ defmodule JS2E.Printers.OneOfPrinter do
9188
defp create_decoder_clauses(types, schema_def, schema_dict) do
9289

9390
create_decoder_clause = fn type_path ->
94-
{clause_type, resolved_schema_def} =
91+
{clause_type, _resolved_schema_def} =
9592
type_path
9693
|> Printer.resolve_type!(schema_def, schema_dict)
9794

@@ -102,11 +99,9 @@ defmodule JS2E.Printers.OneOfPrinter do
10299
|> Enum.map(create_decoder_clause)
103100
end
104101

105-
@spec print_encoder(
106-
Types.typeDefinition,
107-
SchemaDefinition.t,
108-
Types.schemaDictionary
109-
) :: String.t
102+
@impl JS2E.Printers.PrinterBehaviour
103+
@spec print_encoder(Types.typeDefinition, SchemaDefinition.t,
104+
Types.schemaDictionary) :: String.t
110105
def print_encoder(%OneOfType{name: name,
111106
path: _path,
112107
types: types}, schema_def, schema_dict) do
@@ -143,7 +138,7 @@ defmodule JS2E.Printers.OneOfPrinter do
143138
type_name = upcase_first name
144139

145140
create_type_clause = fn type_path ->
146-
{clause_type, resolved_schema_def} =
141+
{clause_type, _resolved_schema_def} =
147142
type_path
148143
|> Printer.resolve_type!(schema_def, schema_dict)
149144

0 commit comments

Comments
 (0)