Skip to content

Commit

Permalink
Allow any type inside array and map; fix single-letter namespaced IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
seriyps committed Mar 14, 2020
1 parent 8142a71 commit 88a4cf8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/avro_idl_lexer.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ error|throws|oneway|void|import|idl|protocol|schema : {token, {list_to_atom(Toke

[A-Za-z_][A-Za-z0-9_]* : {token, {id, TokenLine, TokenChars}}.
%% namespaced will only be allowed in data type spec
[A-Za-z_][A-Za-z0-9_]+(\.[A-Za-z_][A-Za-z0-9_]+)+ : {token, {ns_id, TokenLine, TokenChars}}.
[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)+ : {token, {ns_id, TokenLine, TokenChars}}.

%% https://blog.ostermiller.org/finding-comments-in-source-code-using-regular-expressions/
%% `/** .. */` is a docstring for the following object
Expand Down
8 changes: 4 additions & 4 deletions src/avro_idl_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ union_tail ->

%% -- Array typedef
array ->
array_t '<' primitive_t '>' :
{array, value_of('$3')}. %FIXME: not just primitives!
array_t '<' type '>' :
{array, '$3'}.

%% -- Map typedef
map ->
map_t '<' primitive_t '>' :
{map, value_of('$3')}. %FIXME: not just primitives!
map_t '<' type '>' :
{map, '$3'}.

%% == Function (message) definitions

Expand Down
46 changes: 45 additions & 1 deletion test/avro_idl_parse_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,54 @@ protocol_with_typedefs_test() ->
#function{name = "ping", extra = undefined}]},
parse_idl("protocol_with_typedefs")).

array_types_test() ->
Probes =
[{int, "int"},
{{decimal, 1, 2}, "decimal(1, 2)"},
{null, "null"},
{{custom, "MyType"}, "MyType"},
{{custom, "my_ns.MyType"}, "my_ns.MyType"},
{{union, [int, null]}, "union{int, null}"},
{{array, int}, "array<int>"},
{{map, int}, "map<int>"}],
lists:foreach(
fun({ExpectType, IdlType}) ->
test_field_type({array, ExpectType}, "array<" ++ IdlType ++ ">")
end, Probes).

map_types_test() ->
Probes =
[{int, "int"},
{{custom, "MyType"}, "MyType"},
{{array, int}, "array<int>"},
{{map, int}, "map<int>"}],
lists:foreach(
fun({ExpectType, IdlType}) ->
test_field_type({map, ExpectType}, "map<" ++ IdlType ++ ">")
end, Probes).

%% Helpers

test_field_type(ExpectType, IdlType) ->
Idl = ("protocol P {"
" record R { " ++ IdlType ++ " f; }"
"}"),
#protocol{
definitions =
[#record{
fields =
[#field{type = Type}]}]} = parse_str(Idl),
?assertEqual(ExpectType, Type,
#{proto => Idl,
type => IdlType}).

parse_idl(Name) ->
File = "test/data/" ++ Name ++ ".avdl",
{ok, B} = file:read_file(File),
{ok, T0, _} = avro_idl_lexer:string(binary_to_list(B)),
parse_str(binary_to_list(B)).

parse_str(Str) ->
{ok, T0, _} = avro_idl_lexer:string(Str),
%% ?debugFmt("Name: ~p~nTokens:~n~p", [Name, T0]),
T = avro_idl_lexer:preprocess(T0, [drop_comments, trim_doc]),
{ok, Tree} = avro_idl_parser:parse(T),
Expand Down
21 changes: 21 additions & 0 deletions test/avro_idl_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ duplicate_annotation_test() ->
"@my_decorator(\"a\") @my_decorator(\"b\") protocol MyProto{}", "")
).

nested_complex_types_test() ->
?assertEqual(
#{protocol => "P",
messages => [],
types =>
[#{type => record,
name => "R",
fields =>
[#{name => "f",
type =>
#{type => array,
items =>
#{type => map,
values => [null, "ns.T"]}
}
}
]}]},
avro_idl:str_to_avpr(
"protocol P { record R { array<map<union{null, ns.T}>> f; }}", "")
).

%% Helpers

idl_to_avpr(Name) ->
Expand Down

0 comments on commit 88a4cf8

Please sign in to comment.