Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% == Dependencies ==
{deps, [
{jsx, ".*", {git, "git://github.com/ddossot/jsx.git", {tag, "v1.4.1"}}},
{kvc, ".*", {git, "git://github.com/ddossot/kvc.git", {tag, "v1.3.0"}}},
{kvc, ".*", {git, "https://github.com/etrepum/kvc.git", {tag, "v1.7.0"}}},
{getopt, ".*", {git, "git://github.com/ddossot/getopt.git", {tag, "v0.7.1"}}}
]}.

Expand Down
52 changes: 26 additions & 26 deletions src/jerg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ get_output_io_device(ParsedOpts) when is_list(ParsedOpts) ->
IoDeviceOut
end.

-spec load_source(string(), string() | undefined) -> digraph().
-spec load_source(string(), string() | undefined) -> digraph:digraph().
load_source(Source, Prefix) ->
SchemaGraph = digraph:new(),
case filelib:is_dir(Source) of
Expand All @@ -118,14 +118,14 @@ load_source(Source, Prefix) ->
end,
SchemaGraph.

-spec load_schemas(list(string()), digraph(), string() | undefined) -> ok.
-spec load_schemas(list(string()), digraph:digraph(), string() | undefined) -> ok.
load_schemas([], _, _) ->
ok;
load_schemas([SchemaFile|Rest], SchemaGraph, Prefix) ->
load_single_schema(SchemaFile, SchemaGraph, Prefix),
load_schemas(Rest, SchemaGraph, Prefix).

-spec load_single_schema(string(), digraph(), string() | undefined) -> digraph:vertex().
-spec load_single_schema(string(), digraph:digraph(), string() | undefined) -> digraph:vertex().
load_single_schema(SchemaFile, SchemaGraph, Prefix) ->
{ok, SchemaFileContent} = file:read_file(SchemaFile),
JsonSchema = parse_and_enrich_json_schema(SchemaFileContent, Prefix),
Expand Down Expand Up @@ -153,7 +153,7 @@ ensure_supported_json_schema(FileName, JsonSchema) when is_list(JsonSchema) ->
ensure_supported_json_schema(FileName, _) ->
throw({error, {FileName, "JSON Schema is not an object"}}).

-spec get_schema(digraph(), binary()) -> {binary(), jsx:json_term()}.
-spec get_schema(digraph:digraph(), binary()) -> {binary(), jsx:json_term()}.
get_schema(SchemaGraph, Vertex) ->
{Vertex, {RawSchemaId, JsonSchema}} = digraph:vertex(SchemaGraph, Vertex),
{get_schema_id(RawSchemaId, JsonSchema), JsonSchema}.
Expand All @@ -173,13 +173,13 @@ get_schema_id(RawSchemaId, JsonSchema) ->
end,
fn(ActualSchemaId)).

-spec set_schemas_relationships(digraph()) -> ok.
-spec set_schemas_relationships(digraph:digraph()) -> ok.
set_schemas_relationships(SchemaGraph) ->
set_schemas_relationships(
SchemaGraph,
get_schema_vertices(SchemaGraph)).

-spec set_schemas_relationships(digraph(), list(jsx:json_term())) -> ok.
-spec set_schemas_relationships(digraph:digraph(), list(jsx:json_term())) -> ok.
set_schemas_relationships(_, []) ->
ok;
set_schemas_relationships(SchemaGraph, [Vertex|Vertices]) ->
Expand All @@ -188,7 +188,7 @@ set_schemas_relationships(SchemaGraph, [Vertex|Vertices]) ->
set_schema_relationships(SchemaGraph, Vertex, SchemaDependencies),
set_schemas_relationships(SchemaGraph, Vertices).

-spec set_schema_relationships(digraph(), jsx:json_term(), list(jsx:json_term())) -> ok.
-spec set_schema_relationships(digraph:digraph(), jsx:json_term(), list(jsx:json_term())) -> ok.
set_schema_relationships(_, _, []) ->
ok;
set_schema_relationships(SchemaGraph, Vertex, [DependencyVertex|SchemaDependencies]) ->
Expand All @@ -214,38 +214,38 @@ get_schema_parent_vertex(JsonSchema) ->
get_schema_ref_vertices(JsonSchema) ->
[[kvc:path(['$ref'], S)] ++ [kvc:path([items,'$ref'], S)] || {_, S} <- kvc:path([properties], JsonSchema)].

-spec ensure_no_cyclicity(digraph()) -> ok.
-spec ensure_no_cyclicity(digraph:digraph()) -> ok.
ensure_no_cyclicity(SchemaGraph) ->
ensure(fun() -> digraph_utils:is_acyclic(SchemaGraph) end,
fun() -> {schema_cyclic, digraph_utils:cyclic_strong_components(SchemaGraph)} end).

-spec output_records(digraph(), file:io_device()) -> ok.
-spec output_records(digraph:digraph(), file:io_device()) -> ok.
output_records(SchemaGraph, IoDevice) ->
{{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
output("%%~n%% Generated by jerg v~s on ~B-~2..0B-~2..0B at ~2..0B:~2..0B:~2..0B~n%%~n%% DO NOT EDIT - CHANGES WILL BE OVERWRITTEN!~n%%~n~n",
[get_jerg_version(), Year, Month, Day, Hour, Min, Sec],
IoDevice),
output_records(SchemaGraph, get_schema_vertices(SchemaGraph), IoDevice).

-spec get_schema_vertices(digraph()) -> list(digraph:vertex()).
-spec get_schema_vertices(digraph:digraph()) -> list(digraph:vertex()).
get_schema_vertices(SchemaGraph) ->
get_schema_vertices(SchemaGraph, digraph:no_edges(SchemaGraph)).

-spec get_schema_vertices(digraph(), non_neg_integer()) -> list(digraph:vertex()).
-spec get_schema_vertices(digraph:digraph(), non_neg_integer()) -> list(digraph:vertex()).
get_schema_vertices(SchemaGraph, 0) ->
digraph:vertices(SchemaGraph);
get_schema_vertices(SchemaGraph, _NumberOfEdges) ->
digraph_utils:postorder(SchemaGraph).

-spec output_records(digraph(), list(digraph:vertex()), file:io_device()) -> ok.
-spec output_records(digraph:digraph(), list(digraph:vertex()), file:io_device()) -> ok.
output_records(_, [], _) ->
ok;
output_records(SchemaGraph, [Vertex|Vertices], IoDevice) when is_list(Vertices) ->
{SchemaId, JsonSchema} = get_schema(SchemaGraph, Vertex),
output_record(SchemaGraph, SchemaId, JsonSchema, IoDevice),
output_records(SchemaGraph, Vertices, IoDevice).

-spec output_record(digraph(), binary(), jsx:json_term(), file:io_device()) -> ok.
-spec output_record(digraph:digraph(), binary(), jsx:json_term(), file:io_device()) -> ok.
output_record(SchemaGraph, SchemaId, JsonSchema, IoDevice) ->
case {kvc:path([type], JsonSchema), kvc:path([abstract], JsonSchema)} of
{<<"object">>, Abstract} when Abstract =:= false ; Abstract =:= [] ->
Expand All @@ -254,7 +254,7 @@ output_record(SchemaGraph, SchemaId, JsonSchema, IoDevice) ->
ok
end.

-spec output_object_record(digraph(), binary(), jsx:json_term(), file:io_device()) -> no_return().
-spec output_object_record(digraph:digraph(), binary(), jsx:json_term(), file:io_device()) -> no_return().
output_object_record(SchemaGraph, SchemaId, JsonSchema, IoDevice) ->
output_title(JsonSchema, IoDevice),
output_description(JsonSchema, IoDevice),
Expand All @@ -278,14 +278,14 @@ output_description(JsonObject, IoDevice) ->
output_description(JsonObject, IoDevice, Margin) ->
output_path_value([description], JsonObject, Margin ++ "% ~s~n", IoDevice).

-spec output_properties(digraph(), jsx:json_term(), file:io_device()) -> no_return().
-spec output_properties(digraph:digraph(), jsx:json_term(), file:io_device()) -> no_return().
output_properties(SchemaGraph, JsonSchema, IoDevice) ->
Properties = collect_properties(SchemaGraph, JsonSchema),
NameWidth = find_longest_property_name(Properties),
DefaultValueWidth = find_longest_default_value(SchemaGraph, Properties),
output_property(SchemaGraph, Properties, IoDevice, NameWidth, DefaultValueWidth).

-spec output_property(digraph(), list(jsx:json_term()), file:io_device(), integer(), integer()) -> ok.
-spec output_property(digraph:digraph(), list(jsx:json_term()), file:io_device(), integer(), integer()) -> ok.
output_property(_, [], _, _, _) ->
ok;
output_property(SchemaGraph, [{PropertyName, PropertySchema}|Properties], IoDevice, NameWidth, DefaultValueWidth) ->
Expand All @@ -310,7 +310,7 @@ output_property(SchemaGraph, [{PropertyName, PropertySchema}|Properties], IoDevi
end,
output_property(SchemaGraph, Properties, IoDevice, NameWidth, DefaultValueWidth).

-spec format_default_value(digraph(), jsx:json_term(), integer()) -> binary().
-spec format_default_value(digraph:digraph(), jsx:json_term(), integer()) -> binary().
format_default_value(_, _, 0) ->
<<>>;
format_default_value(SchemaGraph, PropertySchema, DefaultValueWidth) ->
Expand All @@ -323,7 +323,7 @@ format_default_value(<<>>, DefaultValueWidth) ->
format_default_value(DefaultValue, DefaultValueWidth) ->
format_to_binary("= ~-" ++ integer_to_list(DefaultValueWidth) ++ "s ", [DefaultValue]).

-spec collect_properties(digraph(), jsx:json_term()) -> list(jsx:json_term()).
-spec collect_properties(digraph:digraph(), jsx:json_term()) -> list(jsx:json_term()).
collect_properties(SchemaGraph, JsonSchema) ->
Properties = kvc:path([properties], JsonSchema),

Expand All @@ -339,11 +339,11 @@ collect_properties(SchemaGraph, JsonSchema) ->
find_longest_property_name(Properties) ->
lists:max([size(PropertyName) || {PropertyName, _} <- Properties]).

-spec find_longest_default_value(digraph(), jsx:json_term()) -> binary().
-spec find_longest_default_value(digraph:digraph(), jsx:json_term()) -> binary().
find_longest_default_value(SchemaGraph, Properties) ->
lists:max([size(get_property_default_value(SchemaGraph, PropertySchema)) || {_, PropertySchema} <- Properties]).

-spec get_property_default_value(digraph(), jsx:json_term()) -> binary().
-spec get_property_default_value(digraph:digraph(), jsx:json_term()) -> binary().
get_property_default_value(SchemaGraph, PropertySchema) ->
case_path_has_value(
['default'],
Expand All @@ -353,18 +353,18 @@ get_property_default_value(SchemaGraph, PropertySchema) ->
end,
fn(<<>>)).

-spec convert_property_default_value(digraph(), jsx:json_term(), term()) -> binary().
-spec convert_property_default_value(digraph:digraph(), jsx:json_term(), term()) -> binary().
convert_property_default_value(SchemaGraph, PropertySchema, DefaultValue) ->
TypeSpec = get_property_type_spec(SchemaGraph, PropertySchema),
convert_property_value(DefaultValue, TypeSpec).

-spec convert_property_value(digraph(), binary()) -> binary().
-spec convert_property_value(digraph:digraph(), binary()) -> binary().
convert_property_value(_, <<"term()">>) ->
<<>>;
convert_property_value(DefaultValue, _) ->
format_to_binary("~p", [DefaultValue]).

-spec get_property_type_spec(digraph(), jsx:json_term()) -> binary().
-spec get_property_type_spec(digraph:digraph(), jsx:json_term()) -> binary().
get_property_type_spec(SchemaGraph, PropertySchema) ->
case_path_has_value(
['$ref'],
Expand All @@ -376,7 +376,7 @@ get_property_type_spec(SchemaGraph, PropertySchema) ->
convert_property_type_spec(SchemaGraph, kvc:path([type], PropertySchema), PropertySchema)
end).

-spec get_schema_reference_type_spec(digraph(), binary()) -> binary().
-spec get_schema_reference_type_spec(digraph:digraph(), binary()) -> binary().
get_schema_reference_type_spec(SchemaGraph, Ref) ->
{RawSchemaId, JsonSchema} = get_schema(SchemaGraph, Ref),
SchemaId = get_schema_id(RawSchemaId, JsonSchema),
Expand All @@ -392,7 +392,7 @@ get_enum_type_spec([Value|Values]) ->
" | ",
(get_enum_type_spec(Values))/binary>>.

-spec convert_property_type_spec(digraph(), jsx:json_term(), jsx:json_term()) -> binary().
-spec convert_property_type_spec(digraph:digraph(), jsx:json_term(), jsx:json_term()) -> binary().
convert_property_type_spec(_, [], _) ->
<<"term()">>; % default type
convert_property_type_spec(SchemaGraph, [UnionedType], PropertySchema) ->
Expand Down Expand Up @@ -517,4 +517,4 @@ get_jerg_version() ->
id(T) -> T.

-spec fn(term()) -> fun(() -> term()).
fn(T) -> fun() -> T end.
fn(T) -> fun() -> T end.