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

Add option to handle unsupported terms encoding #81

Merged
merged 1 commit into from
Aug 5, 2024
Merged
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
3 changes: 2 additions & 1 deletion elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
allow => [
'assert',
'assertEqual',
'assertError'
'assertError',
'assertNotException'
]
}},
{elvis_style, max_function_length, #{max_length => 65}}
Expand Down
54 changes: 33 additions & 21 deletions src/euneus_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
encode_tuple => encode(tuple()),
encode_pid => encode(pid()),
encode_port => encode(port()),
encode_reference => encode(reference())
encode_reference => encode(reference()),
encode_term => encode(term())
}.

-type codec() ::
Expand Down Expand Up @@ -97,7 +98,8 @@
encode_tuple :: encode(tuple()),
encode_pid :: encode(pid()),
encode_port :: encode(port()),
encode_reference :: encode(reference())
encode_reference :: encode(reference()),
encode_term :: encode(term())
}).
-opaque state() :: #state{}.

Expand Down Expand Up @@ -327,11 +329,17 @@
%% <li>
%% `encode_reference' - Overrides the default reference encoder.
%% </li>
%% <li>
%% `encode_term' - Overrides the default encoder for unsuported terms,
%% like functions.
%%
%% By default, the `unsuported_term' error is raised.
%% </li>
%% </ul>
encode(Input, Opts) ->
State = new_state(Opts),
json:encode(Input, fun(Term, Encode) ->
encode_term(Term, Encode, State)
do_encode(Term, Encode, State)
end).

%% --------------------------------------------------------------------
Expand All @@ -357,7 +365,8 @@ new_state(Opts) ->
encode_tuple = maps:get(encode_tuple, Opts, fun encode_tuple/3),
encode_pid = maps:get(encode_pid, Opts, fun encode_pid/3),
encode_port = maps:get(encode_port, Opts, fun encode_port/3),
encode_reference = maps:get(encode_reference, Opts, fun encode_reference/3)
encode_reference = maps:get(encode_reference, Opts, fun encode_reference/3),
encode_term = maps:get(encode_term, Opts, fun encode_term/3)
}.

key_to_binary(Bin) when is_binary(Bin) ->
Expand Down Expand Up @@ -462,33 +471,33 @@ records_codec_callback(_Tuple, _Records) ->

% Encoders

encode_term(Bin, _Encode, State) when is_binary(Bin) ->
do_encode(Bin, _Encode, State) when is_binary(Bin) ->
(State#state.escape)(Bin);
encode_term(Int, Encode, State) when is_integer(Int) ->
do_encode(Int, Encode, State) when is_integer(Int) ->
(State#state.encode_integer)(Int, Encode, State);
encode_term(Float, Encode, State) when is_float(Float) ->
do_encode(Float, Encode, State) when is_float(Float) ->
(State#state.encode_float)(Float, Encode, State);
encode_term(Atom, Encode, State) when is_atom(Atom) ->
do_encode(Atom, Encode, State) when is_atom(Atom) ->
(State#state.encode_atom)(Atom, Encode, State);
encode_term(List, Encode, State) when is_list(List) ->
do_encode(List, Encode, State) when is_list(List) ->
(State#state.encode_list)(List, Encode, State);
encode_term(Map, Encode, State) when is_map(Map) ->
do_encode(Map, Encode, State) when is_map(Map) ->
(State#state.encode_map)(Map, Encode, State);
encode_term(Tuple, Encode, State) when is_tuple(Tuple) ->
do_encode(Tuple, Encode, State) when is_tuple(Tuple) ->
case traverse_codecs(State#state.codecs, Tuple) of
NewTuple when is_tuple(NewTuple) ->
(State#state.encode_tuple)(NewTuple, Encode, State);
NewTerm ->
encode_term(NewTerm, Encode, State)
do_encode(NewTerm, Encode, State)
end;
encode_term(Pid, Encode, State) when is_pid(Pid) ->
do_encode(Pid, Encode, State) when is_pid(Pid) ->
(State#state.encode_pid)(Pid, Encode, State);
encode_term(Port, Encode, State) when is_port(Port) ->
do_encode(Port, Encode, State) when is_port(Port) ->
(State#state.encode_port)(Port, Encode, State);
encode_term(Ref, Encode, State) when is_reference(Ref) ->
do_encode(Ref, Encode, State) when is_reference(Ref) ->
(State#state.encode_reference)(Ref, Encode, State);
encode_term(Term, Encode, State) ->
error(unsuported_term, [Term, Encode, State]).
do_encode(Term, Encode, State) ->
(State#state.encode_term)(Term, Encode, State).

encode_integer(Int, _Encode, _State) ->
erlang:integer_to_binary(Int, 10).
Expand Down Expand Up @@ -528,7 +537,7 @@ encode_list(List, Encode, #state{proplists = {true, IsProplist}} = State) ->
end.

encode_proplist(Proplist, Encode, State) ->
encode_term(proplists:to_map(Proplist), Encode, State).
do_encode(proplists:to_map(Proplist), Encode, State).

is_proplist([]) ->
false;
Expand All @@ -547,7 +556,7 @@ is_proplist_prop(Key) ->
-if(?OTP_RELEASE >= 26).
encode_map(Map, Encode, #state{sort_keys = false, skip_values = ValuesToSkip} = State) ->
do_encode_map([
[$,, escape_map_key(Key, State), $: | encode_term(Value, Encode, State)]
[$,, escape_map_key(Key, State), $: | do_encode(Value, Encode, State)]
|| Key := Value <- Map,
not is_map_key(Value, ValuesToSkip)
]);
Expand All @@ -567,7 +576,7 @@ encode_map(Map, Encode, #state{sort_keys = false, skip_values = ValuesToSkip} =
$,,
escape_map_key(Key, State),
$:
| encode_term(Value, Encode, State)
| do_encode(Value, Encode, State)
]
| Acc
]
Expand All @@ -583,7 +592,7 @@ encode_map(Map, Encode, State) ->

encode_sort_keys_map(Map, Encode, #state{sort_keys = true} = State) ->
do_encode_map([
[$,, escape_map_key(Key, State), $: | encode_term(Value, Encode, State)]
[$,, escape_map_key(Key, State), $: | do_encode(Value, Encode, State)]
|| {Key, Value} <- lists:keysort(1, maps:to_list(Map)),
not is_map_key(Value, State#state.skip_values)
]).
Expand All @@ -605,3 +614,6 @@ encode_port(Port, Encode, State) ->

encode_reference(Ref, Encode, State) ->
error(unsuported_reference, [Ref, Encode, State]).

encode_term(Term, Encode, State) ->
error(unsuported_term, [Term, Encode, State]).
19 changes: 17 additions & 2 deletions test/euneus_encoder_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,23 @@ encode_reference_test(Config) when is_list(Config) ->
)
].

unsuported_term_test(Config) when is_list(Config) ->
?assertError(unsuported_term, encode(fun() -> error end)).
encode_term_test(Config) when is_list(Config) ->
[
?assertError(unsuported_term, encode(fun() -> error end)),
?assertNotException(
error,
unsuported_term,
encode(
fun() -> ok end,
#{
encode_term =>
fun(Fun, Encode, _State) when is_function(Fun) ->
Encode(iolist_to_binary(erlang:fun_to_list(Fun)), Encode)
end
}
)
)
].

%% --------------------------------------------------------------------
%% Test support
Expand Down