fix bytes coerce logic
This commit is contained in:
+17
-11
@@ -926,7 +926,10 @@ erlang_to_fate({O, N, char}, Str) ->
|
||||
single_error({invalid, O, N, Str})
|
||||
end;
|
||||
erlang_to_fate({O, N, {bytes, [Count]}}, Bytes) when is_bitstring(Bytes) ->
|
||||
coerce_bytes(O, N, Count, Bytes);
|
||||
case check_bytes(O, N, Count, Bytes) of
|
||||
ok -> {ok, {bytes, Bytes}};
|
||||
{error, Reason} -> {error, Reason}
|
||||
end;
|
||||
erlang_to_fate({_, _, bits}, Num) when is_integer(Num) ->
|
||||
{ok, {bits, Num}};
|
||||
erlang_to_fate({_, _, bits}, Bits) when is_bitstring(Bits) ->
|
||||
@@ -988,14 +991,14 @@ decode_chain_object(Tag, S) ->
|
||||
error:incorrect_size -> {error, incorrect_size}
|
||||
end.
|
||||
|
||||
coerce_bytes(O, N, _, Bytes) when bit_size(Bytes) rem 8 /= 0 ->
|
||||
check_bytes(O, N, _, Bytes) when bit_size(Bytes) rem 8 /= 0 ->
|
||||
single_error({partial_bytes, O, N, bit_size(Bytes)});
|
||||
coerce_bytes(_, _, any, Bytes) ->
|
||||
{ok, Bytes};
|
||||
coerce_bytes(O, N, Count, Bytes) when byte_size(Bytes) /= Count ->
|
||||
check_bytes(_, _, any, _) ->
|
||||
ok;
|
||||
check_bytes(O, N, Count, Bytes) when byte_size(Bytes) /= Count ->
|
||||
single_error({incorrect_size, O, N, Bytes});
|
||||
coerce_bytes(_, _, _, Bytes) ->
|
||||
{ok, Bytes}.
|
||||
check_bytes(_, _, _, _) ->
|
||||
ok.
|
||||
|
||||
coerce_zipped_bindings(Bindings, Direction, Tag) ->
|
||||
coerce_zipped_bindings(Bindings, Direction, Tag, [], []).
|
||||
@@ -1261,8 +1264,11 @@ fate_to_erlang({_, _, string}, Bin) ->
|
||||
{ok, Str};
|
||||
fate_to_erlang({_, _, char}, Val) ->
|
||||
{ok, Val};
|
||||
fate_to_erlang({O, N, {bytes, [Count]}}, Bytes) when is_bitstring(Bytes) ->
|
||||
coerce_bytes(O, N, Count, Bytes);
|
||||
fate_to_erlang({O, N, {bytes, [Count]}}, {bytes, Bytes}) when is_bitstring(Bytes) ->
|
||||
case check_bytes(O, N, Count, Bytes) of
|
||||
ok -> {ok, Bytes};
|
||||
{error, Reason} -> {error, Reason}
|
||||
end;
|
||||
fate_to_erlang({_, _, bits}, {bits, Num}) ->
|
||||
{ok, Num};
|
||||
fate_to_erlang({_, _, {list, [Type]}}, Data) when is_list(Data) ->
|
||||
@@ -1452,7 +1458,7 @@ coerce_record_test() ->
|
||||
|
||||
coerce_bytes_test() ->
|
||||
{ok, Type} = annotate_type({tuple, [{bytes, [4]}, {bytes, [any]}]}, #{}),
|
||||
check_roundtrip(Type, {<<"abcd">>, <<"efghi">>}, {tuple, {<<"abcd">>, <<"efghi">>}}).
|
||||
check_roundtrip(Type, {<<"abcd">>, <<"efghi">>}, {tuple, {{bytes, <<"abcd">>}, {bytes, <<"efghi">>}}}).
|
||||
|
||||
coerce_bits_test() ->
|
||||
{ok, Type} = annotate_type(bits, #{}),
|
||||
@@ -1471,7 +1477,7 @@ coerce_unicode_test() ->
|
||||
coerce_hash_test() ->
|
||||
{ok, Type} = annotate_type("hash", builtin_typedefs()),
|
||||
Hash = list_to_binary(lists:seq(1,32)),
|
||||
check_roundtrip(Type, Hash, Hash),
|
||||
check_roundtrip(Type, Hash, {bytes, Hash}),
|
||||
ok.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user