This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new 4349497  AVRO-2498: Add SpecificCompiler support for `uuid` 
logicalType (#1362)
4349497 is described below

commit 4349497ecc68f66b74e35fea01f07153bf0c9146
Author: feroze daud <[email protected]>
AuthorDate: Tue Nov 16 05:40:04 2021 -0800

    AVRO-2498: Add SpecificCompiler support for `uuid` logicalType (#1362)
    
    Co-authored-by: feroze daud <[email protected]>
---
 .../avro/compiler/specific/SpecificCompiler.java   |  1 +
 .../compiler/specific/TestSpecificCompiler.java    | 32 ++++++++++++++++++----
 .../compiler/src/test/resources/logical-uuid.avsc  | 30 ++++++++++++++++++++
 3 files changed, 57 insertions(+), 6 deletions(-)

diff --git 
a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
 
b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
index f9eb43a..94fb0e2 100644
--- 
a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
+++ 
b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
@@ -107,6 +107,7 @@ public class SpecificCompiler {
     specificData.addLogicalTypeConversion(new 
TimeConversions.TimestampMicrosConversion());
     specificData.addLogicalTypeConversion(new 
TimeConversions.LocalTimestampMicrosConversion());
     specificData.addLogicalTypeConversion(new 
TimeConversions.LocalTimestampMillisConversion());
+    specificData.addLogicalTypeConversion(new Conversions.UUIDConversion());
   }
 
   private final SpecificData specificData = new SpecificData();
diff --git 
a/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
 
b/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
index b140077..d789779 100644
--- 
a/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
+++ 
b/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
@@ -350,7 +350,8 @@ public class TestSpecificCompiler {
     Assert.assertEquals("Should use LocalDateTime for local-timestamp-millis 
type", "java.time.LocalDateTime",
         compiler.javaType(localTimestampSchema));
     Assert.assertEquals("Should use Java BigDecimal type", 
"java.math.BigDecimal", compiler.javaType(decimalSchema));
-    Assert.assertEquals("Should use Java CharSequence type", 
"java.lang.CharSequence", compiler.javaType(uuidSchema));
+    Assert.assertEquals("Should use 
org.apache.avro.Conversions.UUIDConversion() type",
+        "new org.apache.avro.Conversions.UUIDConversion()", 
compiler.conversionInstance(uuidSchema));
   }
 
   @Test
@@ -374,7 +375,8 @@ public class TestSpecificCompiler {
     Assert.assertEquals("Should use DateTime for timestamp-millis type", 
"java.time.Instant",
         compiler.javaType(timestampSchema));
     Assert.assertEquals("Should use ByteBuffer type", "java.nio.ByteBuffer", 
compiler.javaType(decimalSchema));
-    Assert.assertEquals("Should use Java CharSequence type", 
"java.lang.CharSequence", compiler.javaType(uuidSchema));
+    Assert.assertEquals("Should use 
org.apache.avro.Conversions.UUIDConversion() type",
+        "new org.apache.avro.Conversions.UUIDConversion()", 
compiler.conversionInstance(uuidSchema));
   }
 
   @Test
@@ -765,8 +767,8 @@ public class TestSpecificCompiler {
         compiler.conversionInstance(timestampSchema));
     Assert.assertEquals("Should use null for decimal if the flag is off", 
"null",
         compiler.conversionInstance(decimalSchema));
-    Assert.assertEquals("Should use null for decimal if the flag is off", 
"null",
-        compiler.conversionInstance(uuidSchema));
+    Assert.assertEquals("Should use 
org.apache.avro.Conversions.UUIDConversion() for uuid if the flag is off",
+        "new org.apache.avro.Conversions.UUIDConversion()", 
compiler.conversionInstance(uuidSchema));
   }
 
   @Test
@@ -789,8 +791,8 @@ public class TestSpecificCompiler {
         compiler.conversionInstance(timestampSchema));
     Assert.assertEquals("Should use null for decimal if the flag is off",
         "new org.apache.avro.Conversions.DecimalConversion()", 
compiler.conversionInstance(decimalSchema));
-    Assert.assertEquals("Should use null for decimal if the flag is off", 
"null",
-        compiler.conversionInstance(uuidSchema));
+    Assert.assertEquals("Should use 
org.apache.avro.Conversions.UUIDConversion() for uuid if the flag is off",
+        "new org.apache.avro.Conversions.UUIDConversion()", 
compiler.conversionInstance(uuidSchema));
   }
 
   @Test
@@ -906,6 +908,24 @@ public class TestSpecificCompiler {
     assertEquals(1, itWorksFound);
   }
 
+  @Test
+  public void testPojoWithUUID() throws IOException {
+    SpecificCompiler compiler = createCompiler();
+    compiler.setOptionalGettersForNullableFieldsOnly(true);
+    File avsc = new File("src/main/resources/logical-uuid.avsc");
+    compiler.compileToDestination(avsc, OUTPUT_DIR.getRoot());
+    assertTrue(this.outputFile.exists());
+    try (BufferedReader reader = new BufferedReader(new 
FileReader(this.outputFile))) {
+      String line;
+      while ((line = reader.readLine()) != null) {
+        line = line.trim();
+        if (line.contains("guid")) {
+          assertTrue(line.contains("java.util.UUID"));
+        }
+      }
+    }
+  }
+
   public static class StringCustomLogicalTypeFactory implements 
LogicalTypes.LogicalTypeFactory {
     @Override
     public LogicalType fromSchema(Schema schema) {
diff --git a/lang/java/compiler/src/test/resources/logical-uuid.avsc 
b/lang/java/compiler/src/test/resources/logical-uuid.avsc
new file mode 100644
index 0000000..0653a5a
--- /dev/null
+++ b/lang/java/compiler/src/test/resources/logical-uuid.avsc
@@ -0,0 +1,30 @@
+{
+  "namespace": "schema.common",
+  "type": "record",
+  "name": "Action",
+  "fields": [
+    {
+      "name": "name",
+      "type": "string"
+    },
+    {
+      "name": "guid",
+      "type": { "type": "string", "logicalType": "uuid" }
+    },
+    {
+      "name": "time",
+      "type": {
+        "type": "long",
+        "logicalType": "timestamp-millis"
+      }
+    },
+    {
+      "name": "requestId",
+      "type": [
+        "null",
+        "string"
+      ],
+      "default": null
+    }
+  ]
+}

Reply via email to