fix bytes coerce logic

This commit is contained in:
Jarvis Carroll
2026-05-28 00:41:51 +00:00
parent 75bc52ede3
commit ea3a5453f2
+17 -11
View File
@@ -926,7 +926,10 @@ erlang_to_fate({O, N, char}, Str) ->
single_error({invalid, O, N, Str}) single_error({invalid, O, N, Str})
end; end;
erlang_to_fate({O, N, {bytes, [Count]}}, Bytes) when is_bitstring(Bytes) -> 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) -> erlang_to_fate({_, _, bits}, Num) when is_integer(Num) ->
{ok, {bits, Num}}; {ok, {bits, Num}};
erlang_to_fate({_, _, bits}, Bits) when is_bitstring(Bits) -> erlang_to_fate({_, _, bits}, Bits) when is_bitstring(Bits) ->
@@ -988,14 +991,14 @@ decode_chain_object(Tag, S) ->
error:incorrect_size -> {error, incorrect_size} error:incorrect_size -> {error, incorrect_size}
end. 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)}); single_error({partial_bytes, O, N, bit_size(Bytes)});
coerce_bytes(_, _, any, Bytes) -> check_bytes(_, _, any, _) ->
{ok, Bytes}; ok;
coerce_bytes(O, N, Count, Bytes) when byte_size(Bytes) /= Count -> check_bytes(O, N, Count, Bytes) when byte_size(Bytes) /= Count ->
single_error({incorrect_size, O, N, Bytes}); single_error({incorrect_size, O, N, Bytes});
coerce_bytes(_, _, _, Bytes) -> check_bytes(_, _, _, _) ->
{ok, Bytes}. ok.
coerce_zipped_bindings(Bindings, Direction, Tag) -> coerce_zipped_bindings(Bindings, Direction, Tag) ->
coerce_zipped_bindings(Bindings, Direction, Tag, [], []). coerce_zipped_bindings(Bindings, Direction, Tag, [], []).
@@ -1261,8 +1264,11 @@ fate_to_erlang({_, _, string}, Bin) ->
{ok, Str}; {ok, Str};
fate_to_erlang({_, _, char}, Val) -> fate_to_erlang({_, _, char}, Val) ->
{ok, Val}; {ok, Val};
fate_to_erlang({O, N, {bytes, [Count]}}, Bytes) when is_bitstring(Bytes) -> fate_to_erlang({O, N, {bytes, [Count]}}, {bytes, Bytes}) when is_bitstring(Bytes) ->
coerce_bytes(O, N, Count, Bytes); case check_bytes(O, N, Count, Bytes) of
ok -> {ok, Bytes};
{error, Reason} -> {error, Reason}
end;
fate_to_erlang({_, _, bits}, {bits, Num}) -> fate_to_erlang({_, _, bits}, {bits, Num}) ->
{ok, Num}; {ok, Num};
fate_to_erlang({_, _, {list, [Type]}}, Data) when is_list(Data) -> fate_to_erlang({_, _, {list, [Type]}}, Data) when is_list(Data) ->
@@ -1452,7 +1458,7 @@ coerce_record_test() ->
coerce_bytes_test() -> coerce_bytes_test() ->
{ok, Type} = annotate_type({tuple, [{bytes, [4]}, {bytes, [any]}]}, #{}), {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() -> coerce_bits_test() ->
{ok, Type} = annotate_type(bits, #{}), {ok, Type} = annotate_type(bits, #{}),
@@ -1471,7 +1477,7 @@ coerce_unicode_test() ->
coerce_hash_test() -> coerce_hash_test() ->
{ok, Type} = annotate_type("hash", builtin_typedefs()), {ok, Type} = annotate_type("hash", builtin_typedefs()),
Hash = list_to_binary(lists:seq(1,32)), Hash = list_to_binary(lists:seq(1,32)),
check_roundtrip(Type, Hash, Hash), check_roundtrip(Type, Hash, {bytes, Hash}),
ok. ok.