Skip to content

Commit

Permalink
Merge pull request #138 from NelsonVides/slim_dependencies
Browse files Browse the repository at this point in the history
Remove dependencies if not needed
  • Loading branch information
andreineculau authored Sep 3, 2024
2 parents b6119b0 + 76345bc commit 37fee73
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 21 deletions.
7 changes: 1 addition & 6 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%-*- mode: erlang -*-
{profiles, [{test, [{deps, [{ proper, "1.4.0"}]}]}]}.
{profiles, [{test, [{deps, [{jsx, "3.1.0"}, {rfc3339, "0.9.0"}, {proper, "1.4.0"}]}]}]}.
{erl_opts, [ {platform_define, "^R[0-9]+", erlang_deprecated_types}
%% , warn_export_all
, warn_export_vars
Expand All @@ -18,11 +18,6 @@
, undefined_function_calls
, deprecated_function_calls
]}.
{ deps
, [ {jsx, "3.1.0"}
, {rfc3339, "0.9.0"}
]}.

{ project_plugins
, [ {rebar3_lint, "3.2.6"}
, {rebar3_proper, "0.12.1"}
Expand Down
15 changes: 15 additions & 0 deletions rebar.config.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{Deps0, Config0} = case lists:keytake(deps, 1, CONFIG) of
false -> {[], CONFIG};
{value, {deps, D}, Cfg} -> {D, Cfg}
end,

Deps = case list_to_integer(erlang:system_info(otp_release)) of
N when N >= 27 ->
Deps0;
N when N >= 21 ->
[{jsx, "3.1.0"} | Deps0];
_ ->
[{jsx, "3.1.0"}, {rfc3339, "0.9.0"} | Deps0]
end,

[{deps, Deps} | Config0].
2 changes: 0 additions & 2 deletions src/jesse.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
, public_key
, ssl
, inets
, jsx
, rfc3339
]}
, {env, [ {re_options, [unicode, ucp]}
]}
Expand Down
19 changes: 19 additions & 0 deletions src/jesse.app.src.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[{application, jesse, Config}] = CONFIG,

{Apps0, Config0} = case lists:keytake(applications, 1, Config) of
false ->
{[], CONFIG};
{value, {applications, A}, Cfg} ->
{A, Cfg}
end,

Apps = case list_to_integer(erlang:system_info(otp_release)) of
N when N >= 27 ->
Apps0;
N when N >= 21 ->
[jsx | Apps0];
_ ->
[jsx, rfc3339 | Apps0]
end,

[{application, jesse, [{applications, Apps} | Config]}].
7 changes: 4 additions & 3 deletions src/jesse_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ run(Options, [Schema|_] = Schemata, [JsonInstance|JsonInstances]) ->
undefined ->
io:fwrite("~p\n\n", [Result]);
true ->
io:fwrite("~s\n\n", [jsx:encode(Result)])
io:fwrite("~s\n\n", [jesse_lib:json_encode(Result)])
end,
case JesseResult of
{ok, _} ->
Expand All @@ -86,11 +86,12 @@ run(Options, [Schema|_] = Schemata, [JsonInstance|JsonInstances]) ->
halt(1)
end.


jesse_run(JsonInstance, Schema, Schemata) ->
{ok, _} = application:ensure_all_started(jesse),
ok = add_schemata(Schemata),
{ok, JsonInstanceBinary} = file:read_file(JsonInstance),
JsonInstanceJsx = jsx:decode(JsonInstanceBinary, [{return_maps, false}]),
JsonInstanceJsx = jesse_lib:json_decode(JsonInstanceBinary),
jesse:validate( Schema
, JsonInstanceJsx
).
Expand All @@ -99,7 +100,7 @@ add_schemata([]) ->
ok;
add_schemata([SchemaFile|Rest]) ->
{ok, SchemaBin} = file:read_file(SchemaFile),
Schema0 = jsx:decode(SchemaBin, [{return_maps, false}]),
Schema0 = jesse_lib:json_decode(SchemaBin),
Schema = maybe_fill_schema_id(SchemaFile, Schema0),
ok = jesse:add_schema(SchemaFile, Schema),
add_schemata(Rest).
Expand Down
4 changes: 2 additions & 2 deletions src/jesse_database.erl
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ add_file_uri(Key0) ->
"file://" ++ File = Key,
{ok, SchemaBin} = file:read_file(File),
{ok, #file_info{mtime = Mtime}} = file:read_file_info(File),
Schema = jsx:decode(SchemaBin, [{return_maps, false}]),
Schema = jesse_lib:json_decode(SchemaBin),
SchemaInfos = [{Key, Mtime, Schema}],
ValidationFun = fun jesse_lib:is_json_object/1,
store_schemas(SchemaInfos, ValidationFun).
Expand All @@ -321,7 +321,7 @@ add_http_uri(Key0) ->
, HttpOptions
, [{body_format, binary}]),
{{_Line, 200, _}, Headers, SchemaBin} = Response,
Schema = jsx:decode(SchemaBin, [{return_maps, false}]),
Schema = jesse_lib:json_decode(SchemaBin),
SchemaInfos = [{Key, get_http_mtime(Headers), Schema}],
ValidationFun = fun jesse_lib:is_json_object/1,
store_schemas(SchemaInfos, ValidationFun).
Expand Down
10 changes: 2 additions & 8 deletions src/jesse_error.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
, handle_data_invalid/3
, handle_schema_invalid/2
, to_json/1
, to_json/2
, reason_to_jsx/1
]).

Expand Down Expand Up @@ -105,14 +104,9 @@ handle_schema_invalid(Info, State) ->

%% @doc Convert an error to a JSON string using jsx
-spec to_json(Error :: error()) -> binary().
to_json(Error) ->
to_json(Error, [indent]).

%% @doc Convert an error to a JSON string using jsx
-spec to_json(Error :: error(), JsxOptions :: [atom()]) -> binary().
to_json({error, Reasons}, JsxOptions) ->
to_json({error, Reasons}) ->
JsxReasons = lists:map(fun reason_to_jsx/1, Reasons),
jsx:encode([{reasons, JsxReasons}], JsxOptions).
jesse_lib:json_encode([{reasons, JsxReasons}]).

%% @doc Convert an error reason to jsx structs
-spec reason_to_jsx(Reason :: error_reason()) -> jesse:json_term().
Expand Down
25 changes: 25 additions & 0 deletions src/jesse_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,36 @@
, get_schema_id_key/1
, get_schema_id/1
, get_schema_id/2
, json_encode/1
, json_decode/1
]).

%% Includes
-include("jesse_schema_validator.hrl").

%% Use new json library if available
-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE >= 27).
%% OTP 27 or higher
json_decode(Bin) ->
json:decode(Bin).
json_encode(Bin) ->
json:encode(Bin).
-else.
%% OTP 26 to 21.
json_decode(Bin) ->
jsx:decode(Bin, [{return_maps, false}]).
json_encode(Bin) ->
jsx:encode(Bin).
-endif.
-else.
%% OTP 20 or lower.
json_decode(Bin) ->
jsx:decode(Bin, [{return_maps, false}]).
json_encode(Bin) ->
jsx:encode(Bin).
-endif.

%%% API
%% @doc Returns an empty list if the given value is ?not_found.
-spec empty_if_not_found(Value :: any()) -> any().
Expand Down
10 changes: 10 additions & 0 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1335,13 +1335,23 @@ remove_last_from_path(State) ->
jesse_state:remove_last_from_path(State).

%% @private
-ifdef(OTP_RELEASE).
%% OTP 21 or higher
valid_datetime(DateTimeBin) ->
try calendar:rfc3339_to_system_time(binary_to_list(DateTimeBin)) of
_ -> true
catch error:_Error -> false
end.
-else.
%% OTP 20 or lower.
valid_datetime(DateTimeBin) ->
case rfc3339:parse(DateTimeBin) of
{ok, _} ->
true;
_ ->
false
end.
-endif.

maybe_external_check_value(Value, State) ->
case jesse_state:get_external_validator(State) of
Expand Down
10 changes: 10 additions & 0 deletions src/jesse_validator_draft6.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1285,13 +1285,23 @@ remove_last_from_path(State) ->
jesse_state:remove_last_from_path(State).

%% @private
-ifdef(OTP_RELEASE).
%% OTP 21 or higher
valid_datetime(DateTimeBin) ->
try calendar:rfc3339_to_system_time(binary_to_list(DateTimeBin)) of
_ -> true
catch error:_Error -> false
end.
-else.
%% OTP 20 or lower.
valid_datetime(DateTimeBin) ->
case rfc3339:parse(DateTimeBin) of
{ok, _} ->
true;
_ ->
false
end.
-endif.

maybe_external_check_value(Value, State) ->
case jesse_state:get_external_validator(State) of
Expand Down

0 comments on commit 37fee73

Please sign in to comment.