Skip to content

Commit

Permalink
Issue #82 (#83)
Browse files Browse the repository at this point in the history
* Erlang/Elixir nil/null issue addressed similar to jiffy use_nil approach.

* README updated.

* use_nil additionality implemented for unpack method.
  • Loading branch information
grepz authored Jun 9, 2023
1 parent 0dea2a4 commit 14df9be
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ bin binary() binary()
str binary() string()
```

### `{use_nil, boolean()}`

Handles Elixir `nil` as `null` Erlang atom. Default value is `false`.

### `{validate_string, boolean()}`

Only in unpacking, UTF-8 validation at unpacking from `str` type will
Expand Down
4 changes: 4 additions & 0 deletions src/msgpack.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{allow_atom, none|pack} |
{known_atoms, [atom()]} |
{unpack_str, as_binary|as_list|as_tagged_list} |
{use_nil, boolean()} |
{validate_string, boolean()} |
{pack_str, from_binary|from_list|from_tagged_list|none} |
{map_format, map|jiffy|jsx} |
Expand Down Expand Up @@ -171,6 +172,9 @@ parse_options([{known_atoms, Atoms}|T], Opt0) when is_list(Atoms) ->
parse_options([{unpack_str, As}|T], Opt0) when As =:= as_binary orelse As =:= as_list orelse As =:= as_tagged_list ->
parse_options(T, Opt0?OPTION{unpack_str=As});

parse_options([{use_nil, Bool}|T], Opt) when is_boolean(Bool) ->
parse_options(T, Opt?OPTION{use_nil=Bool});

parse_options([{validate_string, Bool}|T], Opt) when is_boolean(Bool) ->
parse_options(T, Opt?OPTION{validate_string=Bool});

Expand Down
1 change: 1 addition & 0 deletions src/msgpack.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
allow_atom = pack :: none | pack, %% allows atom when packing
known_atoms = [] :: [atom()|binary()],
unpack_str = as_list :: as_binary | as_list | as_tagged_list,
use_nil = false :: boolean(),
validate_string = false :: boolean(),
pack_str = from_list :: from_binary | from_list | from_tagged_list | none,
map_format = ?DEFAULT_MAP_FORMAT :: format_type(),
Expand Down
2 changes: 2 additions & 0 deletions src/msgpack_packer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pack(F, _) when is_float(F) ->
pack_double(F);
pack(null, _Opt) ->
<< 16#C0:8 >>;
pack(nil, ?OPTION{use_nil = true}) ->
<< 16#C0:8 >>;
pack(false, _) ->
<< 16#C2:8 >>;
pack(true, _) ->
Expand Down
2 changes: 2 additions & 0 deletions src/msgpack_unpacker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
%% unpack them all
-spec unpack_stream(Bin::binary(), ?OPTION{}) -> {msgpack:object(), binary()} | {error, any()} | no_return().
%% ATOMS
unpack_stream(<<16#C0, Rest/binary>>, ?OPTION{use_nil = true}) ->
{nil, Rest};
unpack_stream(<<16#C0, Rest/binary>>, _) ->
{null, Rest};
unpack_stream(<<16#C2, Rest/binary>>, _) ->
Expand Down
21 changes: 21 additions & 0 deletions test/msgpack_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,27 @@ unpack_str_validation_test_() ->
[{spec,new},{unpack_str,from_list},{validate_string,true}]))]}
].

pack_nil_test_() ->
ElixirStyleNil = nil,
NilConversionOpt = [{use_nil, true}],
UseNilDefaultResult = msgpack:pack(ElixirStyleNil),
UseNilTrueResult = msgpack:pack(ElixirStyleNil, NilConversionOpt),
[{"use_nil default false",
[?_assertEqual({ok, <<"nil">>}, msgpack:unpack(UseNilDefaultResult))]},
{"use_nil explicitly set to true",
[?_assertEqual({ok, null}, msgpack:unpack(UseNilTrueResult))]}
].

unpack_nil_test_() ->
NullPacked = <<16#C0>>,
NilConversionOpt = [{use_nil, true}],
UseNilDefaultResult = msgpack:unpack(NullPacked),
UseNilTrueResult = msgpack:unpack(NullPacked, NilConversionOpt),
[{"use_nil default false",
[?_assertEqual({ok, null}, UseNilDefaultResult)]},
{"use_nil explicitly set to true",
[?_assertEqual({ok, nil}, UseNilTrueResult)]}
].

float_unpacking_test_() ->
%% float 32
Expand Down

0 comments on commit 14df9be

Please sign in to comment.