[ 
https://issues.apache.org/jira/browse/AVRO-3257?focusedWorklogId=685375&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-685375
 ]

ASF GitHub Bot logged work on AVRO-3257:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 23/Nov/21 15:59
            Start Date: 23/Nov/21 15:59
    Worklog Time Spent: 10m 
      Work Description: 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: issues-unsubscr...@avro.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 685375)
    Time Spent: 40m  (was: 0.5h)

> IDL: add syntax to create optional fields
> -----------------------------------------
>
>                 Key: AVRO-3257
>                 URL: https://issues.apache.org/jira/browse/AVRO-3257
>             Project: Apache Avro
>          Issue Type: Improvement
>          Components: java, spec
>            Reporter: Oscar Westra van Holthe - Kind
>            Assignee: Oscar Westra van Holthe - Kind
>            Priority: Minor
>              Labels: javacc, pull-request-available
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> To define optional values, Avro has the "union with the null type" 
> construction.
> In IDL, this is a rather verbose construct:
> {code}
> union {null, string} optionalName = null;
> {code}
> Wish: add Kotlin style optional types like this:
> {code}
> string? optionalName = null;
> {code}
> The resulting schema should be identical; it's only a syntax change.
> Note: it would be nice (but not required) if the syntax would also work if 
> the default value is not null:
> {code}
> string? optionalName = "Jane Doe";
> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to