opwvhk commented on a change in pull request #1411:
URL: https://github.com/apache/avro/pull/1411#discussion_r755274774
##########
File path:
lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
##########
@@ -1444,15 +1443,42 @@ Schema UnannotatedType(Map<String, JsonNode> props):
{
Schema s;
}
+{
+ (
+ s = NullableType(props)
+ | (
+ s = UnionDefinition()
+ | s = ArrayType()
+ | s = MapType()
+ )
+ {
+ // NullableType also applies properties, inside any union with null it
may create.
+ for (String key : props.keySet())
+ Accessor.addProp(s, key, props.get(key));
+ }
+ )
+ {
+ return s;
+ }
+}
+
+Schema NullableType(Map<String, JsonNode> props):
+{
+ Schema s;
+ boolean optional = false;
+}
{
(
s = ReferenceType() { if (!props.isEmpty()) { throw error("Type
references may not be annotated", token); } }
| s = PrimitiveType()
- | s = UnionDefinition()
- | s = ArrayType()
- | s = MapType()
- )
+ ) [ <QUESTION_MARK> { optional = true; } ]
{
+ // By applying the properties here (before creating the union), type
annotations modify the optional type instead of the union.
+ for (String key : props.keySet())
+ Accessor.addProp(s, key, props.get(key));
+ if (optional) {
+ s = Schema.createUnion(Schema.create(Schema.Type.NULL), s);
+ }
Review comment:
This is the new bit: reference & primitive/logical types are created as
usual (including properties), and if the type is intended as optional, wrapped
in a union with null.
The duplicated code to apply properties is intentional: if merged with the
code in the `UnannotatedType` production, the properties would be applied to
the union, instead of the optional primitive type.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]