From ac9f44afe2830a933413ad29472dd73e6e0d30ac Mon Sep 17 00:00:00 2001 From: Andrei Neculau Date: Sun, 8 May 2022 16:31:50 +0200 Subject: [PATCH] refactor. move get_schema_id_key to common jesse_lib --- src/jesse_database.erl | 24 +----------------------- src/jesse_lib.erl | 31 +++++++++++++++++++++++++++++++ src/jesse_state.erl | 12 ++---------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/jesse_database.erl b/src/jesse_database.erl index 7599ea5e..413ca804 100644 --- a/src/jesse_database.erl +++ b/src/jesse_database.erl @@ -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 -> @@ -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), diff --git a/src/jesse_lib.erl b/src/jesse_lib.erl index 7f99f5f8..06ec617a 100644 --- a/src/jesse_lib.erl +++ b/src/jesse_lib.erl @@ -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 @@ -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 diff --git a/src/jesse_state.erl b/src/jesse_state.erl index 6c124e1f..efd80607 100644 --- a/src/jesse_state.erl +++ b/src/jesse_state.erl @@ -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().