Skip to content

Commit

Permalink
Changed enum type to use int32, since negative values are allowable f…
Browse files Browse the repository at this point in the history
…or enums.
  • Loading branch information
whitelynx committed Aug 14, 2011
1 parent 10f46ae commit 2e9f79c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/protobuffs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ encode_internal(FieldID, false, bool) ->
encode_internal(FieldID, true, bool) ->
encode_internal(FieldID, 1, int32);
encode_internal(FieldID, Integer, enum) ->
encode_internal(FieldID, Integer, uint32);
encode_internal(FieldID, Integer, int32);
encode_internal(FieldID, Integer, int32) when Integer >= -16#80000000, Integer < 0 ->
encode_internal(FieldID, Integer, int64);
encode_internal(FieldID, Integer, int64) when Integer >= -16#8000000000000000, Integer < 0 ->
Expand Down Expand Up @@ -299,7 +299,7 @@ decode_value(Value, WireType, ExpectedType) ->
%% @hidden
-spec typecast(Value :: any(), Type :: field_type()) ->
any().
typecast(Value, SignedType) when SignedType =:= int32; SignedType =:= int64 ->
typecast(Value, SignedType) when SignedType =:= int32; SignedType =:= int64; SignedType =:= enum ->
if
Value band 16#8000000000000000 =/= 0 -> Value - 16#10000000000000000;
true -> Value
Expand Down
2 changes: 1 addition & 1 deletion src/protobuffs_scanner.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Rules.
{WS}+ : skip_token.
//.* : skip_token.
/\*([^\*]|\*[^/])*\*/ : skip_token.
{D}+ : {token, {integer, TokenLine, list_to_integer(TokenChars)}}.
-?{D}+ : {token, {integer, TokenLine, list_to_integer(TokenChars)}}.
{F} : {token, {float, TokenLine, list_to_float(TokenChars)}}.
{HEX} : {token, {integer, TokenLine, hex_to_int(TokenChars)}}.

Expand Down
3 changes: 3 additions & 0 deletions test/erlang_protobuffs_SUITE_data/enum.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ message EnumMsg {
enum Values {
value1 = 0x01;
value2 = 2;
value3 = -1;
value4 = -2147483647;
value5 = 2147483648;
}

optional Values value = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/protobuffs_eqc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ value() ->
{sint32(),int32},
{sint64(),int64},
{bool(),bool},
{uint32(),enum},
{sint32(),enum},
{string(),string},
{binary(),bytes}]).

Expand Down
5 changes: 5 additions & 0 deletions test/protobuffs_parser_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ enum_test_() ->
Parsed = protobuffs_parser:parse(Result),
[?_assertMatch({ok,[{enum, "MyEnum", [{'VALUE0',0},{'VALUE1',1}]}]},Parsed)].

enum_negative_test_() ->
{ok,Result,1} = protobuffs_scanner:string("enum MyEnum { VALUE0 = 0; VALUE1 = -1; VALUE2 = 2147483648; VALUE3 = -2147483647;}"),
Parsed = protobuffs_parser:parse(Result),
[?_assertMatch({ok,[{enum, "MyEnum", [{'VALUE0',0},{'VALUE1',-1},{'VALUE2',2147483648},{'VALUE3',-2147483647}]}]},Parsed)].

service_test_() ->
{ok,Result,1} = protobuffs_scanner:string("service SearchService { rpc Search (SearchRequest) returns (SearchResponse);}"),
Parsed = protobuffs_parser:parse(Result),
Expand Down
5 changes: 4 additions & 1 deletion test/protobuffs_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ parse_enum_test_() ->
{enum, "Values", Values} = lists:keyfind(enum,1,EnumMsg),
[?_assertMatch({1,optional,"Values","value",none},lists:keyfind(1,1,EnumMsg)),
?_assertMatch({'value1',1},lists:keyfind('value1',1,Values)),
?_assertMatch({'value2',2},lists:keyfind('value2',1,Values))].
?_assertMatch({'value2',2},lists:keyfind('value2',1,Values)),
?_assertMatch({'value3',-1},lists:keyfind('value3',1,Values)),
?_assertMatch({'value4',-2147483647},lists:keyfind('value4',1,Values)),
?_assertMatch({'value5',2147483648},lists:keyfind('value5',1,Values))].

parse_enum_outside_test_() ->
Path = filename:absname("../test/erlang_protobuffs_SUITE_data/enum_outside.proto"),
Expand Down

0 comments on commit 2e9f79c

Please sign in to comment.