Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deprecated http_uri functions for OTP 21+ #86

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
, "deps"
]}.
{erl_opts, [ {platform_define, "^R[0-9]+", erlang_deprecated_types}
, {platform_define, "^(R[0-9]+|17|18|19|20)", http_uri_depricated_functions}
shino marked this conversation as resolved.
Show resolved Hide resolved
%% , warn_export_all
, warn_export_vars
, warn_obsolete_guard
Expand Down
15 changes: 14 additions & 1 deletion src/jesse_json_path.erl
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ normalize(K, undefined) ->

-spec parse_json_pointer_token(Token :: string()) -> binary().
parse_json_pointer_token(Token) ->
DecodedToken = unicode:characters_to_binary(http_uri:decode(Token)),
DecodedToken = unicode:characters_to_binary(hex_decode(Token)),
lists:foldl( fun({From, To}, T) ->
binary:replace(T, From, To)
end
Expand All @@ -302,3 +302,16 @@ parse_json_pointer_token(Token) ->
, {<<"~1">>, <<"/">>}
]
).

%% @private
hex_decode([$%, Hex1, Hex2 | Rest]) ->
shino marked this conversation as resolved.
Show resolved Hide resolved
[hex2dec(Hex1)*16 + hex2dec(Hex2) | hex_decode(Rest)];
hex_decode([First | Rest]) ->
[First | hex_decode(Rest)];
hex_decode([]) ->
[].

%% @private
hex2dec(X) when (X>=$0) andalso (X=<$9) -> X-$0;
hex2dec(X) when (X>=$A) andalso (X=<$F) -> X-$A+10;
hex2dec(X) when (X>=$a) andalso (X=<$f) -> X-$a+10.
39 changes: 31 additions & 8 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,11 @@ load_local_schema(Schema, [Key | Keys]) ->
combine_id(Id, undefined) ->
Id;
combine_id(Id, RefBin) ->
Ref = unicode:characters_to_list(RefBin),
case http_uri:parse(Ref) of
%% Absolute http/s:
{ok, _} ->
Ref;
%% Absolute file:
{error, {no_default_port, file, Ref}} ->
case parse_ref(RefBin) of
{absolute, Ref} ->
Ref;
%% Relative
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the %% Relative comment. it is now nicely readable in the returned tuple 👏

_ ->
{relative, Ref} ->
combine_relative_id(Id, Ref)
end.

Expand Down Expand Up @@ -403,3 +398,31 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
%% @private
get_external_validator(#state{external_validator = Fun}) ->
Fun.

%% @private
-ifdef(http_uri_depricated_functions).
parse_ref(RefBin) ->
Ref = unicode:characters_to_list(RefBin),
case http_uri:parse(Ref) of
%% Absolute http/s:
{ok, _} ->
{absolute, Ref};
%% Absolute file:
{error, {no_default_port, file, Ref}} ->
{absolute, Ref};
%% Relative
_ ->
{relative, Ref}
end.
-else.
parse_ref(RefBin) ->
Ref = unicode:characters_to_list(RefBin),
case uri_string:parse(Ref) of
%% Absolute
#{scheme := _} ->
{absolute, Ref};
%% Relative
_ ->
{relative, Ref}
end.
-endif.