Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support maps for definitions #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/gpb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ decode_type(FieldType, Bin, MsgDefs) ->
{N =/= 0, Rest};
{enum, _EnumName}=Key ->
{N, Rest} = decode_type(int32, Bin, MsgDefs),
{value, {Key, EnumValues}} = lists:keysearch(Key, 1, MsgDefs),
EnumValues = keyfetch(Key, MsgDefs),
case lists:keyfind(N, 2, EnumValues) of
{EnumName, N, _} -> {EnumName, Rest}; % proto_defs_version 3
{EnumName, N} -> {EnumName, Rest}; % proto_defs_version 2
Expand Down Expand Up @@ -1221,11 +1221,11 @@ encode_zigzag(N) when N < 0 -> N * -2 - 1.
-spec verify_msg(tuple() | term(), gpb_defs:defs()) -> ok.
verify_msg(Msg, MsgDefs) when is_tuple(Msg), tuple_size(Msg) >= 1 ->
MsgName = element(1, Msg),
case lists:keysearch({msg,MsgName}, 1, MsgDefs) of
{value, _} ->
verify_msg2(Msg, MsgName, MsgDefs, [top_level]);
false ->
mk_type_error(not_a_known_message, MsgName, [top_level])
case keyfetch_2({msg,MsgName}, MsgDefs) of
undefined ->
mk_type_error(not_a_known_message, MsgName, [top_level]);
_ ->
verify_msg2(Msg, MsgName, MsgDefs, [top_level])
end;
verify_msg(Msg, _MsgDefs) ->
mk_type_error(expected_a_message, Msg, []).
Expand All @@ -1234,7 +1234,7 @@ verify_msg(Msg, _MsgDefs) ->
verify_msg2(Msg, MsgName, MsgDefs, Path) when is_tuple(Msg),
element(1, Msg) == MsgName ->
MsgKey = {msg, MsgName},
{value, {MsgKey, Fields}} = lists:keysearch(MsgKey, 1, MsgDefs),
Fields = keyfetch_2(MsgKey, MsgDefs),
if tuple_size(Msg) == length(Fields) + 1 ->
Path2 = if Path == [top_level] -> [MsgName];
true -> Path
Expand All @@ -1249,7 +1249,7 @@ verify_msg2(V, MsgName, _MsgDefs, Path) ->
verify_group(Msg, GName, MsgDefs, Path) when is_tuple(Msg),
element(1, Msg) == GName ->
Key = {group, GName},
{value, {Key, Fields}} = lists:keysearch(Key, 1, MsgDefs),
Fields = keyfetch_2(Key, MsgDefs),
if tuple_size(Msg) == length(Fields) + 1 ->
Path2 = if Path == [top_level] -> [GName];
true -> Path
Expand Down Expand Up @@ -1898,12 +1898,19 @@ fetch_field_by_name(Name, Fields) ->

-endif. % -ifndef(NO_HAVE_MAPS).

keyfetch(Key, KVPairs) ->
keyfetch(Key, MsgDefs) ->
case keyfetch_2(Key, MsgDefs) of
undefined -> erlang:error({error, {no_such_key, Key, MsgDefs}});
Value -> Value
end.

keyfetch_2(Key, MsgDefs) when is_map(MsgDefs) ->
maps:get(Key, MsgDefs, undefined);

keyfetch_2(Key, KVPairs) when is_list(KVPairs) ->
case lists:keysearch(Key, 1, KVPairs) of
{value, {Key, Value}} ->
Value;
false ->
erlang:error({error, {no_such_key, Key, KVPairs}})
{value, {Key, Value}} -> Value;
false -> undefined
end.

is_field_for_unknowns(#?gpb_field{type=unknown, occurrence=repeated}) ->
Expand Down
2 changes: 1 addition & 1 deletion src/gpb_defs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

-define(is_non_empty_string(Str), (is_list(Str) andalso is_integer(hd(Str)))).

-type defs() :: [def()].
-type defs() :: [def()] | map().

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with | map() being a bit unspecified for the time being, but I would like the | map() to be added to the relevant function in gpb.erl instead of here, since not every function that accept defs() can handle a map().

-type def() :: {proto_defs_version, version()} |
{{msg, Name::atom()}, [field()]} |
{{group, Name::atom()}, [field()]} |
Expand Down