AVRO-2925: add support for UUID in compiler (#951)
* AVRO-2925: add support for UUID in compiler
* add license
* fix formatting with doc
diff --git a/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj b/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
index a7053a4..7787b45 100644
--- a/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
+++ b/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
@@ -243,6 +243,7 @@
| < TIMESTAMP: "timestamp_ms" >
| < DECIMAL: "decimal" >
| < LOCAL_TIMESTAMP: "local_timestamp_ms" >
+| < UUID: "uuid" >
}
/* LITERALS */
@@ -254,7 +255,7 @@
( <DECIMAL_LITERAL> (["l","L"])?
| <HEX_LITERAL> (["l","L"])?
| <OCTAL_LITERAL> (["l","L"])?
- )
+ )
>
|
< #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
@@ -1180,7 +1181,7 @@
<IDL> importFile = JsonString() ";"
{
try {
- Idl idl = new Idl(findFile(importFile), this);
+ Idl idl = new Idl(findFile(importFile), this);
try {
return idl.CompilationUnit();
} finally {
@@ -1188,7 +1189,7 @@
}
} catch (IOException e) {
throw error("Error importing "+importFile+": "+e, token);
- }
+ }
}
}
@@ -1208,7 +1209,7 @@
}
} catch (IOException e) {
throw error("Error importing "+importFile+": "+e, token);
- }
+ }
}
}
@@ -1231,7 +1232,7 @@
}
} catch (IOException e) {
throw error("Error importing "+importFile+": "+e, token);
- }
+ }
}
}
@@ -1319,11 +1320,11 @@
}
{
( SchemaProperty(props) )*
-
+
name = Identifier()
[ <EQUALS> defaultValue=Json() ]
-
+
{
Field.Order order = Field.Order.ASCENDING;
for (String key : props.keySet())
@@ -1378,7 +1379,7 @@
return oneWay
? p.createMessage(name, msgDoc, props, request)
: p.createMessage(name, msgDoc, props, request, response, errors);
-
+
}
}
@@ -1500,6 +1501,7 @@
| "timestamp_ms" { return LogicalTypes.timestampMillis().addToSchema(Schema.create(Type.LONG)); }
| "local_timestamp_ms" { return LogicalTypes.localTimestampMillis().addToSchema(Schema.create(Type.LONG)); }
| "decimal" { return DecimalTypeProperties(); }
+| "uuid" {return LogicalTypes.uuid().addToSchema(Schema.create(Type.STRING));}
}
Schema DecimalTypeProperties():
diff --git a/lang/java/compiler/src/test/idl/input/uuid.avdl b/lang/java/compiler/src/test/idl/input/uuid.avdl
new file mode 100644
index 0000000..f0e1ac8
--- /dev/null
+++ b/lang/java/compiler/src/test/idl/input/uuid.avdl
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+/**
+Testing UUID fields
+*/
+
+@namespace("org.apache.avro")
+protocol MyProtocol {
+ record APlaygroundEvent {
+
+ /**
+ * Documentation must be provided for each attribute
+ */
+ uuid identifier;
+
+
+ /**
+ * a nullable uuid field
+ */
+
+ union { null, uuid } optionalString;
+ }
+}
diff --git a/lang/java/compiler/src/test/idl/output/uuid.avpr b/lang/java/compiler/src/test/idl/output/uuid.avpr
new file mode 100644
index 0000000..cffdba8
--- /dev/null
+++ b/lang/java/compiler/src/test/idl/output/uuid.avpr
@@ -0,0 +1,25 @@
+{
+ "protocol" : "MyProtocol",
+ "namespace" : "org.apache.avro",
+ "doc" : "Testing UUID fields",
+ "types" : [ {
+ "type" : "record",
+ "name" : "APlaygroundEvent",
+ "fields" : [ {
+ "name" : "identifier",
+ "type" : {
+ "type" : "string",
+ "logicalType" : "uuid"
+ },
+ "doc" : "* Documentation must be provided for each attribute"
+ }, {
+ "name" : "optionalString",
+ "type" : [ "null", {
+ "type" : "string",
+ "logicalType" : "uuid"
+ } ],
+ "doc" : "* a nullable uuid field"
+ } ]
+ } ],
+ "messages" : { }
+}