From 2e9f79c44a569b92549345bed1112b4d574613b4 Mon Sep 17 00:00:00 2001 From: "David H. Bronke" Date: Sat, 13 Aug 2011 23:53:39 -0500 Subject: [PATCH] Changed enum type to use int32, since negative values are allowable for enums. --- src/protobuffs.erl | 4 ++-- src/protobuffs_scanner.xrl | 2 +- test/erlang_protobuffs_SUITE_data/enum.proto | 3 +++ test/protobuffs_eqc.erl | 2 +- test/protobuffs_parser_tests.erl | 5 +++++ test/protobuffs_tests.erl | 5 ++++- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/protobuffs.erl b/src/protobuffs.erl index 89b546f..eb16b53 100644 --- a/src/protobuffs.erl +++ b/src/protobuffs.erl @@ -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 -> @@ -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 diff --git a/src/protobuffs_scanner.xrl b/src/protobuffs_scanner.xrl index 254043b..47da942 100644 --- a/src/protobuffs_scanner.xrl +++ b/src/protobuffs_scanner.xrl @@ -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)}}. diff --git a/test/erlang_protobuffs_SUITE_data/enum.proto b/test/erlang_protobuffs_SUITE_data/enum.proto index c149939..4f4e043 100644 --- a/test/erlang_protobuffs_SUITE_data/enum.proto +++ b/test/erlang_protobuffs_SUITE_data/enum.proto @@ -2,6 +2,9 @@ message EnumMsg { enum Values { value1 = 0x01; value2 = 2; + value3 = -1; + value4 = -2147483647; + value5 = 2147483648; } optional Values value = 1; diff --git a/test/protobuffs_eqc.erl b/test/protobuffs_eqc.erl index 73bd498..0a548dd 100644 --- a/test/protobuffs_eqc.erl +++ b/test/protobuffs_eqc.erl @@ -46,7 +46,7 @@ value() -> {sint32(),int32}, {sint64(),int64}, {bool(),bool}, - {uint32(),enum}, + {sint32(),enum}, {string(),string}, {binary(),bytes}]). diff --git a/test/protobuffs_parser_tests.erl b/test/protobuffs_parser_tests.erl index e95571d..ae281d3 100644 --- a/test/protobuffs_parser_tests.erl +++ b/test/protobuffs_parser_tests.erl @@ -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), diff --git a/test/protobuffs_tests.erl b/test/protobuffs_tests.erl index 3dc4651..a7674e8 100644 --- a/test/protobuffs_tests.erl +++ b/test/protobuffs_tests.erl @@ -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"),