Skip to content

Commit 88a4cf8

Browse files
committed
Allow any type inside array and map; fix single-letter namespaced IDs
1 parent 8142a71 commit 88a4cf8

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎src/avro_idl_lexer.xrl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ error|throws|oneway|void|import|idl|protocol|schema : {token, {list_to_atom(Toke
5757

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

6262
%% https://blog.ostermiller.org/finding-comments-in-source-code-using-regular-expressions/
6363
%% `/** .. */` is a docstring for the following object

‎src/avro_idl_parser.yrl‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,13 @@ union_tail ->
210210

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

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

221221
%% == Function (message) definitions
222222

‎test/avro_idl_parse_tests.erl‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,54 @@ protocol_with_typedefs_test() ->
142142
#function{name = "ping", extra = undefined}]},
143143
parse_idl("protocol_with_typedefs")).
144144

145+
array_types_test() ->
146+
Probes =
147+
[{int, "int"},
148+
{{decimal, 1, 2}, "decimal(1, 2)"},
149+
{null, "null"},
150+
{{custom, "MyType"}, "MyType"},
151+
{{custom, "my_ns.MyType"}, "my_ns.MyType"},
152+
{{union, [int, null]}, "union{int, null}"},
153+
{{array, int}, "array<int>"},
154+
{{map, int}, "map<int>"}],
155+
lists:foreach(
156+
fun({ExpectType, IdlType}) ->
157+
test_field_type({array, ExpectType}, "array<" ++ IdlType ++ ">")
158+
end, Probes).
159+
160+
map_types_test() ->
161+
Probes =
162+
[{int, "int"},
163+
{{custom, "MyType"}, "MyType"},
164+
{{array, int}, "array<int>"},
165+
{{map, int}, "map<int>"}],
166+
lists:foreach(
167+
fun({ExpectType, IdlType}) ->
168+
test_field_type({map, ExpectType}, "map<" ++ IdlType ++ ">")
169+
end, Probes).
170+
171+
%% Helpers
172+
173+
test_field_type(ExpectType, IdlType) ->
174+
Idl = ("protocol P {"
175+
" record R { " ++ IdlType ++ " f; }"
176+
"}"),
177+
#protocol{
178+
definitions =
179+
[#record{
180+
fields =
181+
[#field{type = Type}]}]} = parse_str(Idl),
182+
?assertEqual(ExpectType, Type,
183+
#{proto => Idl,
184+
type => IdlType}).
185+
145186
parse_idl(Name) ->
146187
File = "test/data/" ++ Name ++ ".avdl",
147188
{ok, B} = file:read_file(File),
148-
{ok, T0, _} = avro_idl_lexer:string(binary_to_list(B)),
189+
parse_str(binary_to_list(B)).
190+
191+
parse_str(Str) ->
192+
{ok, T0, _} = avro_idl_lexer:string(Str),
149193
%% ?debugFmt("Name: ~p~nTokens:~n~p", [Name, T0]),
150194
T = avro_idl_lexer:preprocess(T0, [drop_comments, trim_doc]),
151195
{ok, Tree} = avro_idl_parser:parse(T),

‎test/avro_idl_tests.erl‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ duplicate_annotation_test() ->
119119
"@my_decorator(\"a\") @my_decorator(\"b\") protocol MyProto{}", "")
120120
).
121121

122+
nested_complex_types_test() ->
123+
?assertEqual(
124+
#{protocol => "P",
125+
messages => [],
126+
types =>
127+
[#{type => record,
128+
name => "R",
129+
fields =>
130+
[#{name => "f",
131+
type =>
132+
#{type => array,
133+
items =>
134+
#{type => map,
135+
values => [null, "ns.T"]}
136+
}
137+
}
138+
]}]},
139+
avro_idl:str_to_avpr(
140+
"protocol P { record R { array<map<union{null, ns.T}>> f; }}", "")
141+
).
142+
122143
%% Helpers
123144

124145
idl_to_avpr(Name) ->

0 commit comments

Comments
 (0)