Skip to content

Commit

Permalink
refactor. move get_schema_id_key to common jesse_lib
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed May 8, 2022
1 parent f433ff7 commit ac9f44a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
24 changes: 1 addition & 23 deletions src/jesse_database.erl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ store_schema(SchemaInfo, {Acc, ValidationFun}) ->
{SourceKey, Mtime, Schema0} = SchemaInfo,
case ValidationFun(Schema0) of
true ->
Id = case get_schema_id(Schema0) of
Id = case jesse_lib:get_schema_id(Schema0) of
undefined ->
SourceKey;
Id0 ->
Expand Down Expand Up @@ -300,28 +300,6 @@ get_schema_info(File, {Acc, ParseFun}) ->
Schema = try_parse(ParseFun, SchemaBin),
{[{SourceKey, Mtime, Schema} | Acc], ParseFun}.

%% @doc Returns "id" or "$id" based on the value of $schema.
%% @private
-spec get_schema_id_key(Schema :: jesse:json_term()) -> string().
get_schema_id_key(Schema) ->
case jesse_json_path:value(?SCHEMA, Schema, undefined) of
?json_schema_draft6 -> ?ID;
_ -> ?ID_OLD
end.

%% @doc Returns value of "id" field from json object `Schema', assuming that
%% the given json object has such a field, otherwise returns undefined.
%% @private
-spec get_schema_id(Schema :: jesse:json_term()) -> string() | undefined.
get_schema_id(Schema) ->
IdKey = get_schema_id_key(Schema),
case jesse_json_path:value(IdKey, Schema, undefined) of
undefined ->
undefined;
Id ->
erlang:binary_to_list(Id)
end.

%% @private
add_file_uri(Key0) ->
Key = jesse_state:canonical_path(Key0, Key0),
Expand Down
31 changes: 31 additions & 0 deletions src/jesse_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
, re_options/0
, normalize_and_sort/1
, is_equal/2
, get_schema_id_key/1
, get_schema_id/1
, get_schema_id/2
]).

%% Includes
Expand Down Expand Up @@ -232,6 +235,34 @@ compare_properties(Value1, Value2) ->
, unwrap(Value1)
).

%%=============================================================================
%% @doc Returns "id" or "$id" based on the value of $schema.
-spec get_schema_id_key(Schema :: jesse:json_term()) -> string().
get_schema_id_key(Schema) ->
case jesse_json_path:value(?SCHEMA, Schema, undefined) of
?json_schema_draft6 -> ?ID;
_ -> ?ID_OLD
end.

%%=============================================================================
%% @doc Returns value of "id" field from json object `Schema', assuming that
%% the given json object has such a field, otherwise returns undefined.
-spec get_schema_id(Schema :: jesse:json_term()) -> string() | undefined.
get_schema_id(Schema) ->
get_schema_id(Schema, undefined).

%% @doc Returns value of "id" field from json object `Schema', assuming that
%% the given json object has such a field, otherwise returns Default.
-spec get_schema_id(Schema :: jesse:json_term(), Default :: string() | undefined) -> string() | undefined.
get_schema_id(Schema, Default) ->
IdKey = get_schema_id_key(Schema),
case jesse_json_path:value(IdKey, Schema, undefined) of
undefined ->
Default;
Id ->
erlang:binary_to_list(Id)
end.

%%=============================================================================
%% Wrappers
%% @private
Expand Down
12 changes: 2 additions & 10 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,8 @@ get_current_schema(#state{current_schema = CurrentSchema}) ->
get_current_schema_id(#state{ current_schema = CurrentSchema
, root_schema = RootSchema
}) ->
Default = get_id(RootSchema, ?not_found),
get_id(CurrentSchema, Default).

get_id(Schema, Default) ->
case jesse_json_path:value(?ID, Schema, undefined) of
undefined ->
jesse_json_path:value(?ID_OLD, Schema, Default);
ID ->
ID
end.
Default = jesse_lib:get_schema_id(RootSchema, ?not_found),
jesse_lib:get_schema_id(CurrentSchema, Default).

%% @doc Getter for `default_schema_ver'.
-spec get_default_schema_ver(State :: state()) -> jesse:schema_ver().
Expand Down

0 comments on commit ac9f44a

Please sign in to comment.