Skip to content

Commit

Permalink
Merge pull request #99 from ferd/skip-booleans
Browse files Browse the repository at this point in the history
Do not convert boolean atoms to strings
  • Loading branch information
mikpe authored Apr 10, 2020
2 parents cd49689 + f00df34 commit 177c3f2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@
* 2.8.3
- Allow arbitrary atoms in record_opt_name()
* 2.9.0
- Encode atom values for string types when not nil or null
- Encode atom values for string types when not `nil` or `null`
* 2.9.1
- Add `true` and `false` to the atom exceptions when converting to strings

5 changes: 3 additions & 2 deletions src/avro_primitive.erl
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ cast(Type, Value) when ?IS_BYTES_TYPE(Type) andalso
cast(Type, Value) when ?IS_STRING_TYPE(Type) andalso
(is_list(Value) orelse is_binary(Value)) ->
{ok, ?AVRO_VALUE(Type, erlang:iolist_to_binary(Value))};
% Encode atom values for string types when not null or nil
% Encode atom values for string types when not null, nil, or booleans
cast(Type, Value) when ?IS_STRING_TYPE(Type) andalso
(is_atom(Value) andalso
(Value =/= null andalso Value =/= nil)) ->
(Value =/= null andalso Value =/= nil andalso
Value =/= true andalso Value =/= false)) ->
{ok, ?AVRO_VALUE(Type, erlang:atom_to_binary(Value, utf8))};
cast(Type, Value) -> {error, {type_mismatch, Type, Value}}.

Expand Down
2 changes: 1 addition & 1 deletion src/erlavro.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, erlavro,
[
{description, "Apache Avro support for Erlang/Elixir"},
{vsn, "2.9.0"},
{vsn, "2.9.1"},
{registered, []},
{applications, [
kernel,
Expand Down
12 changes: 9 additions & 3 deletions test/avro_binary_encoder_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,17 @@ encode_map_error_test() ->
encode(fun(_) -> error(unexpected) end, Type, Value)).

encode_union_test() ->
Type = avro_union:type([null, string]),
Type = avro_union:type([null, string, boolean]),
Value1 = avro_union:new(Type, null),
Value2 = avro_union:new(Type, "a"),
Value2 = avro_union:new(Type, nil),
Value3 = avro_union:new(Type, "a"),
Value4 = avro_union:new(Type, true),
Value5 = avro_union:new(Type, false),
?assertBinEq([0], encode_value(Value1)),
?assertBinEq([long(1), long(1), 97], encode_value(Value2)).
?assertBinEq([0], encode_value(Value2)),
?assertBinEq([long(1), long(1), 97], encode_value(Value3)),
?assertBinEq([long(2), <<1>>], encode_value(Value4)),
?assertBinEq([long(2), <<0>>], encode_value(Value5)).

encode_fixed_test() ->
Type = avro_fixed:type("FooBar", 2),
Expand Down

0 comments on commit 177c3f2

Please sign in to comment.