Skip to content

Commit 1347de4

Browse files
Contexts (#58)
* Contexts: restructuring -> more encapsulation Trying to restructure the project a bit such that everything parser related is found inside the `lib/parser` folder and that `lib/parser.ex` defines the 'facade' of the these submodules. Likewise, for printer related modules in `lib/printer`. However, this exercise isn't very doable for the `lib/types` folder, so have to consider if I'm gaining anything from going down this road for types. * Adds contexts 'parser' and 'printer' We isolate all parser related code in the 'parser' folder and expose the parser.ex module as the interface for that folder / context. We repeat this exercise for the 'printer' folder / context. Furthermore, we remove all the import statements and replace them with more explicit aliases. Closes #57
1 parent e68dc89 commit 1347de4

Some content is hidden

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

63 files changed

+382
-540
lines changed

lib/js2e.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ defmodule JS2E do
1919

2020
require Logger
2121
alias JS2E.Parser
22+
alias JS2E.Parser.{ParserWarning, ParserError}
2223
alias JS2E.Printer
23-
alias JS2E.Parsers.{ParserWarning, ParserError}
24-
alias JS2E.Printers.PrinterError
24+
alias JS2E.Printer.PrinterError
2525

2626
@spec main([String.t()]) :: :ok
2727
def main(args) do
@@ -135,7 +135,7 @@ defmodule JS2E do
135135
String.replace(module_name, ".", "/")
136136
)
137137

138-
Logger.debug("Writing file '#{file_path}'")
138+
Logger.debug(fn -> "Writing file '#{file_path}'" end)
139139
{:ok, file} = File.open(normalized_file_path, [:write])
140140
IO.binwrite(file, file_content)
141141
File.close(file)

lib/parsers/all_of_parser.ex renamed to lib/parser/all_of_parser.ex

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule JS2E.Parsers.AllOfParser do
2-
@behaviour JS2E.Parsers.ParserBehaviour
1+
defmodule JS2E.Parser.AllOfParser do
2+
@behaviour JS2E.Parser.ParserBehaviour
33
@moduledoc ~S"""
44
Parses a JSON schema allOf type:
55
@@ -30,15 +30,7 @@ defmodule JS2E.Parsers.AllOfParser do
3030
"""
3131

3232
require Logger
33-
34-
import JS2E.Parsers.Util,
35-
only: [
36-
parse_child_types: 3,
37-
create_types_list: 2,
38-
create_type_dict: 3
39-
]
40-
41-
alias JS2E.Parsers.{ErrorUtil, ParserResult}
33+
alias JS2E.Parser.{Util, ErrorUtil, ParserResult}
4234
alias JS2E.{Types, TypePath}
4335
alias JS2E.Types.AllOfType
4436

@@ -57,7 +49,7 @@ defmodule JS2E.Parsers.AllOfParser do
5749
true
5850
5951
"""
60-
@impl JS2E.Parsers.ParserBehaviour
52+
@impl JS2E.Parser.ParserBehaviour
6153
@spec type?(Types.node()) :: boolean
6254
def type?(%{"allOf" => all_of})
6355
when is_list(all_of) and length(all_of) > 0,
@@ -68,7 +60,7 @@ defmodule JS2E.Parsers.AllOfParser do
6860
@doc ~S"""
6961
Parses a JSON schema allOf type into an `JS2E.Types.AllOfType`.
7062
"""
71-
@impl JS2E.Parsers.ParserBehaviour
63+
@impl JS2E.Parser.ParserBehaviour
7264
@spec parse(Types.node(), URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
7365
ParserResult.t()
7466
def parse(%{"allOf" => all_of}, parent_id, id, path, name)
@@ -77,16 +69,16 @@ defmodule JS2E.Parsers.AllOfParser do
7769

7870
child_types_result =
7971
all_of
80-
|> parse_child_types(parent_id, child_path)
72+
|> Util.parse_child_types(parent_id, child_path)
8173

8274
all_of_types =
8375
child_types_result.type_dict
84-
|> create_types_list(child_path)
76+
|> Util.create_types_list(child_path)
8577

8678
all_of_type = AllOfType.new(name, path, all_of_types)
8779

8880
all_of_type
89-
|> create_type_dict(path, id)
81+
|> Util.create_type_dict(path, id)
9082
|> ParserResult.new()
9183
|> ParserResult.merge(child_types_result)
9284
end

lib/parsers/any_of_parser.ex renamed to lib/parser/any_of_parser.ex

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule JS2E.Parsers.AnyOfParser do
2-
@behaviour JS2E.Parsers.ParserBehaviour
1+
defmodule JS2E.Parser.AnyOfParser do
2+
@behaviour JS2E.Parser.ParserBehaviour
33
@moduledoc ~S"""
44
Parses a JSON schema anyOf type:
55
@@ -30,15 +30,7 @@ defmodule JS2E.Parsers.AnyOfParser do
3030
"""
3131

3232
require Logger
33-
34-
import JS2E.Parsers.Util,
35-
only: [
36-
parse_child_types: 3,
37-
create_types_list: 2,
38-
create_type_dict: 3
39-
]
40-
41-
alias JS2E.Parsers.{ErrorUtil, ParserResult}
33+
alias JS2E.Parser.{Util, ErrorUtil, ParserResult}
4234
alias JS2E.{Types, TypePath}
4335
alias JS2E.Types.AnyOfType
4436

@@ -57,7 +49,7 @@ defmodule JS2E.Parsers.AnyOfParser do
5749
true
5850
5951
"""
60-
@impl JS2E.Parsers.ParserBehaviour
52+
@impl JS2E.Parser.ParserBehaviour
6153
@spec type?(Types.schemaNode()) :: boolean
6254
def type?(schema_node) do
6355
any_of = schema_node["anyOf"]
@@ -67,7 +59,7 @@ defmodule JS2E.Parsers.AnyOfParser do
6759
@doc ~S"""
6860
Parses a JSON schema anyOf type into an `JS2E.Types.AnyOfType`.
6961
"""
70-
@impl JS2E.Parsers.ParserBehaviour
62+
@impl JS2E.Parser.ParserBehaviour
7163
@spec parse(
7264
Types.schemaNode(),
7365
URI.t(),
@@ -81,16 +73,16 @@ defmodule JS2E.Parsers.AnyOfParser do
8173

8274
child_types_result =
8375
any_of
84-
|> parse_child_types(parent_id, child_path)
76+
|> Util.parse_child_types(parent_id, child_path)
8577

8678
any_of_types =
8779
child_types_result.type_dict
88-
|> create_types_list(child_path)
80+
|> Util.create_types_list(child_path)
8981

9082
any_of_type = AnyOfType.new(name, path, any_of_types)
9183

9284
any_of_type
93-
|> create_type_dict(path, id)
85+
|> Util.create_type_dict(path, id)
9486
|> ParserResult.new()
9587
|> ParserResult.merge(child_types_result)
9688
end

lib/parsers/array_parser.ex renamed to lib/parser/array_parser.ex

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule JS2E.Parsers.ArrayParser do
2-
@behaviour JS2E.Parsers.ParserBehaviour
1+
defmodule JS2E.Parser.ArrayParser do
2+
@behaviour JS2E.Parser.ParserBehaviour
33
@moduledoc ~S"""
44
Parses a JSON schema array type:
55
@@ -15,14 +15,8 @@ defmodule JS2E.Parsers.ArrayParser do
1515

1616
require Logger
1717

18-
import JS2E.Parsers.Util,
19-
only: [
20-
create_type_dict: 3,
21-
parse_type: 4
22-
]
23-
2418
alias JS2E.{Types, TypePath}
25-
alias JS2E.Parsers.ParserResult
19+
alias JS2E.Parser.{Util, ParserResult}
2620
alias JS2E.Types.ArrayType
2721

2822
@doc ~S"""
@@ -40,7 +34,7 @@ defmodule JS2E.Parsers.ArrayParser do
4034
true
4135
4236
"""
43-
@impl JS2E.Parsers.ParserBehaviour
37+
@impl JS2E.Parser.ParserBehaviour
4438
@spec type?(Types.schemaNode()) :: boolean
4539
def type?(schema_node) do
4640
type = schema_node["type"]
@@ -51,7 +45,7 @@ defmodule JS2E.Parsers.ArrayParser do
5145
@doc ~S"""
5246
Parses a JSON schema array type into an `JS2E.Types.ArrayType`.
5347
"""
54-
@impl JS2E.Parsers.ParserBehaviour
48+
@impl JS2E.Parser.ParserBehaviour
5549
@spec parse(
5650
Types.schemaNode(),
5751
URI.t(),
@@ -67,12 +61,12 @@ defmodule JS2E.Parsers.ArrayParser do
6761
items_result =
6862
schema_node
6963
|> Map.get("items")
70-
|> parse_type(parent_id, path, "items")
64+
|> Util.parse_type(parent_id, path, "items")
7165

7266
array_type = ArrayType.new(name, path, items_abs_path)
7367

7468
array_type
75-
|> create_type_dict(path, id)
69+
|> Util.create_type_dict(path, id)
7670
|> ParserResult.new()
7771
|> ParserResult.merge(items_result)
7872
end

lib/parsers/definitions_parser.ex renamed to lib/parser/definitions_parser.ex

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule JS2E.Parsers.DefinitionsParser do
2-
@behaviour JS2E.Parsers.ParserBehaviour
1+
defmodule JS2E.Parser.DefinitionsParser do
2+
@behaviour JS2E.Parser.ParserBehaviour
33
@moduledoc ~S"""
44
Parses a 'definitions' property in a JSON schema or subschema.
55
@@ -17,13 +17,8 @@ defmodule JS2E.Parsers.DefinitionsParser do
1717

1818
require Logger
1919

20-
import JS2E.Parsers.Util,
21-
only: [
22-
parse_type: 4
23-
]
24-
2520
alias JS2E.TypePath
26-
alias JS2E.Parsers.ParserResult
21+
alias JS2E.Parser.{Util, ParserResult}
2722

2823
@doc ~S"""
2924
Returns true if the json schema contains a 'definitions' property.
@@ -37,7 +32,7 @@ defmodule JS2E.Parsers.DefinitionsParser do
3732
true
3833
3934
"""
40-
@impl JS2E.Parsers.ParserBehaviour
35+
@impl JS2E.Parser.ParserBehaviour
4136
@spec type?(map) :: boolean
4237
def type?(%{"definitions" => definitions})
4338
when is_map(definitions),
@@ -48,7 +43,7 @@ defmodule JS2E.Parsers.DefinitionsParser do
4843
@doc ~S"""
4944
Parses a JSON schema 'definitions' property into a map of types.
5045
"""
51-
@impl JS2E.Parsers.ParserBehaviour
46+
@impl JS2E.Parser.ParserBehaviour
5247
@spec parse(map, URI.t(), URI.t() | nil, TypePath.t(), String.t()) ::
5348
ParserResult.t()
5449
def parse(%{"definitions" => definitions}, parent_id, _id, path, _name) do
@@ -61,7 +56,9 @@ defmodule JS2E.Parsers.DefinitionsParser do
6156
definitions_types_result =
6257
definitions
6358
|> Enum.reduce(init_result, fn {child_name, child_node}, acc_result ->
64-
child_types = parse_type(child_node, parent_id, child_path, child_name)
59+
child_types =
60+
Util.parse_type(child_node, parent_id, child_path, child_name)
61+
6562
ParserResult.merge(acc_result, child_types)
6663
end)
6764

lib/parsers/enum_parser.ex renamed to lib/parser/enum_parser.ex

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule JS2E.Parsers.EnumParser do
2-
@behaviour JS2E.Parsers.ParserBehaviour
1+
defmodule JS2E.Parser.EnumParser do
2+
@behaviour JS2E.Parser.ParserBehaviour
33
@moduledoc ~S"""
44
Parse a JSON schema enum type:
55
@@ -12,14 +12,8 @@ defmodule JS2E.Parsers.EnumParser do
1212
"""
1313

1414
require Logger
15-
16-
import JS2E.Parsers.Util,
17-
only: [
18-
create_type_dict: 3
19-
]
20-
2115
alias JS2E.{Types, TypePath}
22-
alias JS2E.Parsers.ParserResult
16+
alias JS2E.Parser.{Util, ParserResult}
2317
alias JS2E.Types.EnumType
2418

2519
@doc ~S"""
@@ -34,7 +28,7 @@ defmodule JS2E.Parsers.EnumParser do
3428
true
3529
3630
"""
37-
@impl JS2E.Parsers.ParserBehaviour
31+
@impl JS2E.Parser.ParserBehaviour
3832
@spec type?(Types.schemaNode()) :: boolean
3933
def type?(%{"enum" => enum, "type" => type})
4034
when is_list(enum) and is_binary(type),
@@ -45,7 +39,7 @@ defmodule JS2E.Parsers.EnumParser do
4539
@doc ~S"""
4640
Parses a JSON schema enum type into an `JS2E.Types.EnumType`.
4741
"""
48-
@impl JS2E.Parsers.ParserBehaviour
42+
@impl JS2E.Parser.ParserBehaviour
4943
@spec parse(
5044
Types.schemaNode(),
5145
URI.t(),
@@ -59,7 +53,7 @@ defmodule JS2E.Parsers.EnumParser do
5953
enum_type = EnumType.new(name, path, type, enum)
6054

6155
enum_type
62-
|> create_type_dict(path, id)
56+
|> Util.create_type_dict(path, id)
6357
|> ParserResult.new()
6458
end
6559
end

lib/parsers/error_util.ex renamed to lib/parser/error_util.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
defmodule JS2E.Parsers.ErrorUtil do
1+
defmodule JS2E.Parser.ErrorUtil do
22
@moduledoc ~S"""
33
Contains helper functions for reporting parser errors.
44
"""
55

66
alias JS2E.{Types, TypePath}
7-
alias JS2E.Parsers.ParserError
7+
alias JS2E.Parser.ParserError
88

99
@doc ~S"""
1010
Returns the name of the type of the given value.

lib/parsers/object_parser.ex renamed to lib/parser/object_parser.ex

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule JS2E.Parsers.ObjectParser do
2-
@behaviour JS2E.Parsers.ParserBehaviour
1+
defmodule JS2E.Parser.ObjectParser do
2+
@behaviour JS2E.Parser.ParserBehaviour
33
@moduledoc ~S"""
44
Parses a JSON schema object type:
55
@@ -23,15 +23,8 @@ defmodule JS2E.Parsers.ObjectParser do
2323
"""
2424

2525
require Logger
26-
27-
import JS2E.Parsers.Util,
28-
only: [
29-
create_type_dict: 3,
30-
parse_type: 4
31-
]
32-
3326
alias JS2E.{Types, TypePath}
34-
alias JS2E.Parsers.ParserResult
27+
alias JS2E.Parser.{Util, ParserResult}
3528
alias JS2E.Types.ObjectType
3629

3730
@doc ~S"""
@@ -51,7 +44,7 @@ defmodule JS2E.Parsers.ObjectParser do
5144
true
5245
5346
"""
54-
@impl JS2E.Parsers.ParserBehaviour
47+
@impl JS2E.Parser.ParserBehaviour
5548
@spec type?(map) :: boolean
5649
def type?(schema_node) do
5750
type = schema_node["type"]
@@ -62,7 +55,7 @@ defmodule JS2E.Parsers.ObjectParser do
6255
@doc ~S"""
6356
Parses a JSON schema object type into an `JS2E.Types.ObjectType`.
6457
"""
65-
@impl JS2E.Parsers.ParserBehaviour
58+
@impl JS2E.Parser.ParserBehaviour
6659
@spec parse(Types.schemaNode(), URI.t(), URI.t(), TypePath.t(), String.t()) ::
6760
ParserResult.t()
6861
def parse(schema_node, parent_id, id, path, name) do
@@ -80,7 +73,7 @@ defmodule JS2E.Parsers.ObjectParser do
8073
object_type = ObjectType.new(name, path, type_dict, required)
8174

8275
object_type
83-
|> create_type_dict(path, id)
76+
|> Util.create_type_dict(path, id)
8477
|> ParserResult.new()
8578
|> ParserResult.merge(child_types_result)
8679
end
@@ -91,7 +84,9 @@ defmodule JS2E.Parsers.ObjectParser do
9184

9285
node_properties
9386
|> Enum.reduce(init_result, fn {child_name, child_node}, acc_result ->
94-
child_types = parse_type(child_node, parent_id, child_path, child_name)
87+
child_types =
88+
Util.parse_type(child_node, parent_id, child_path, child_name)
89+
9590
ParserResult.merge(acc_result, child_types)
9691
end)
9792
end
@@ -103,7 +98,7 @@ defmodule JS2E.Parsers.ObjectParser do
10398
10499
iex> type_dict = %{}
105100
...> path = JS2E.TypePath.from_string("#")
106-
...> JS2E.Parsers.ObjectParser.create_property_dict(type_dict, path)
101+
...> JS2E.Parser.ObjectParser.create_property_dict(type_dict, path)
107102
%{}
108103
109104
"""

0 commit comments

Comments
 (0)