You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by tk...@apache.org on 2022/09/19 06:56:51 UTC

[ignite-3] branch main updated: IGNITE-17712 Fix UUID serialization for hocon in configuration (#1093)

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

tkalkirill pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 818db99667 IGNITE-17712 Fix UUID serialization for hocon in configuration (#1093)
818db99667 is described below

commit 818db99667dc69c621899e012e3981bcd83f7820
Author: Kirill Tkalenko <tk...@yandex.ru>
AuthorDate: Mon Sep 19 09:56:47 2022 +0300

    IGNITE-17712 Fix UUID serialization for hocon in configuration (#1093)
---
 .../hocon/HoconPrimitiveConfigurationSource.java   |  7 +++
 .../configuration/tree/ConverterToMapVisitor.java  |  5 +-
 .../configuration/hocon/HoconConverterTest.java    | 55 +++++++++++++++++++---
 3 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/hocon/HoconPrimitiveConfigurationSource.java b/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/hocon/HoconPrimitiveConfigurationSource.java
index 4bad81cb2a..830b2567e7 100644
--- a/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/hocon/HoconPrimitiveConfigurationSource.java
+++ b/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/hocon/HoconPrimitiveConfigurationSource.java
@@ -25,6 +25,7 @@ import static org.apache.ignite.internal.configuration.util.ConfigurationUtil.jo
 
 import com.typesafe.config.ConfigValue;
 import java.util.List;
+import java.util.UUID;
 import org.apache.ignite.internal.configuration.TypeUtils;
 import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
 import org.apache.ignite.internal.configuration.tree.ConstructableTreeNode;
@@ -163,6 +164,12 @@ class HoconPrimitiveConfigurationSource implements ConfigurationSource {
             } else if (clazz == Double.class) {
                 return clazz.cast(numberValue.doubleValue());
             }
+        } else if (clazz == UUID.class) {
+            if (hoconCfgValue.valueType() != STRING) {
+                throw wrongTypeException(clazz, path, idx);
+            }
+
+            return clazz.cast(UUID.fromString(hoconCfgValue.unwrapped().toString()));
         }
 
         throw new IllegalArgumentException("Unsupported type: " + clazz);
diff --git a/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/tree/ConverterToMapVisitor.java b/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/tree/ConverterToMapVisitor.java
index 2b02c63167..fb2655b8cd 100644
--- a/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/tree/ConverterToMapVisitor.java
+++ b/modules/configuration/src/main/java/org/apache/ignite/internal/configuration/tree/ConverterToMapVisitor.java
@@ -26,6 +26,7 @@ import java.util.Deque;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
@@ -54,7 +55,7 @@ public class ConverterToMapVisitor implements ConfigurationVisitor<Object> {
     public Object visitLeafNode(String key, Serializable val) {
         Object valObj = val;
 
-        if (val instanceof Character) {
+        if (val instanceof Character || val instanceof UUID) {
             valObj = val.toString();
         } else if (val != null && val.getClass().isArray()) {
             valObj = toListOfObjects(val);
@@ -126,7 +127,7 @@ public class ConverterToMapVisitor implements ConfigurationVisitor<Object> {
     private List<?> toListOfObjects(Serializable val) {
         Stream<?> stream = IntStream.range(0, Array.getLength(val)).mapToObj(i -> Array.get(val, i));
 
-        if (val.getClass().getComponentType() == char.class) {
+        if (val.getClass().getComponentType() == char.class || val.getClass().getComponentType() == UUID.class) {
             stream = stream.map(Object::toString);
         }
 
diff --git a/modules/configuration/src/test/java/org/apache/ignite/internal/configuration/hocon/HoconConverterTest.java b/modules/configuration/src/test/java/org/apache/ignite/internal/configuration/hocon/HoconConverterTest.java
index 47a3e865ab..4934ec9824 100644
--- a/modules/configuration/src/test/java/org/apache/ignite/internal/configuration/hocon/HoconConverterTest.java
+++ b/modules/configuration/src/test/java/org/apache/ignite/internal/configuration/hocon/HoconConverterTest.java
@@ -34,6 +34,7 @@ import com.typesafe.config.ConfigValue;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -105,6 +106,9 @@ public class HoconConverterTest {
 
         @Value(hasDefault = true)
         public String[] strings = {""};
+
+        @Value(hasDefault = true)
+        public UUID[] uuids = {new UUID(1111, 2222)};
     }
 
     /**
@@ -138,6 +142,9 @@ public class HoconConverterTest {
 
         @Value(hasDefault = true)
         public String stringVal = "";
+
+        @Value(hasDefault = true)
+        public UUID uuidVal = new UUID(100, 200);
     }
 
     /**
@@ -272,7 +279,7 @@ public class HoconConverterTest {
     }
 
     /**
-     * Tests that the {@code HoconConverter} supports serialization of Strings and primitives.
+     * Tests that the {@code HoconConverter} supports serialization of {@link String}, {@link UUID} and primitives.
      */
     @Test
     public void testHoconPrimitivesSerialization() throws Exception {
@@ -285,8 +292,11 @@ public class HoconConverterTest {
 
         var basePath = List.of("root", "primitivesList", "name");
 
+        UUID uuid = new UUID(100, 200);
+
         assertEquals(
-                "booleanVal=false,byteVal=0,charVal=\"\\u0000\",doubleVal=0.0,floatVal=0,intVal=0,longVal=0,shortVal=0,stringVal=\"\"",
+                "booleanVal=false,byteVal=0,charVal=\"\\u0000\",doubleVal=0.0,floatVal=0,intVal=0,longVal=0,shortVal=0,stringVal=\"\""
+                        + ",uuidVal=\"" + uuid + "\"",
                 asHoconStr(basePath)
         );
 
@@ -299,10 +309,11 @@ public class HoconConverterTest {
         assertEquals("0", asHoconStr(basePath, "floatVal"));
         assertEquals("0.0", asHoconStr(basePath, "doubleVal"));
         assertEquals("\"\"", asHoconStr(basePath, "stringVal"));
+        assertEquals("\"" + uuid + "\"", asHoconStr(basePath, "uuidVal"));
     }
 
     /**
-     * Tests that the {@code HoconConverter} supports serialization of arrays of Strings and primitives.
+     * Tests that the {@code HoconConverter} supports serialization of arrays of {@link String}, {@link UUID} and primitives.
      */
     @Test
     public void testHoconArraysSerialization() throws Exception {
@@ -315,8 +326,11 @@ public class HoconConverterTest {
 
         var basePath = List.of("root", "arraysList", "name");
 
+        UUID uuid = new UUID(1111, 2222);
+
         assertEquals(
-                "booleans=[false],bytes=[0],chars=[\"\\u0000\"],doubles=[0.0],floats=[0],ints=[0],longs=[0],shorts=[0],strings=[\"\"]",
+                "booleans=[false],bytes=[0],chars=[\"\\u0000\"],doubles=[0.0],floats=[0],ints=[0],longs=[0],shorts=[0],strings=[\"\"]"
+                        + ",uuids=[\"" + uuid + "\"]",
                 asHoconStr(basePath)
         );
 
@@ -329,6 +343,7 @@ public class HoconConverterTest {
         assertEquals("[0]", asHoconStr(basePath, "floats"));
         assertEquals("[0.0]", asHoconStr(basePath, "doubles"));
         assertEquals("[\"\"]", asHoconStr(basePath, "strings"));
+        assertEquals("[\"" + uuid + "\"]", asHoconStr(basePath, "uuids"));
     }
 
     /**
@@ -394,7 +409,7 @@ public class HoconConverterTest {
     }
 
     /**
-     * Tests that the {@code HoconConverter} supports deserialization of Strings and primitives.
+     * Tests that the {@code HoconConverter} supports deserialization of {@link String}, {@link UUID} and primitives.
      */
     @Test
     public void testHoconPrimitivesDeserialization() throws Throwable {
@@ -429,6 +444,15 @@ public class HoconConverterTest {
 
         change("root.primitivesList.name.stringVal = foo");
         assertThat(primitives.stringVal().value(), is("foo"));
+
+        UUID newUuid0 = UUID.randomUUID();
+        UUID newUuid1 = UUID.randomUUID();
+
+        change("root.primitivesList.name.uuidVal = " + newUuid0);
+        assertThat(primitives.uuidVal().value(), is(newUuid0));
+
+        change("root.primitivesList.name.uuidVal = \"" + newUuid1 + "\"");
+        assertThat(primitives.uuidVal().value(), is(newUuid1));
     }
 
     /**
@@ -495,10 +519,15 @@ public class HoconConverterTest {
                 () -> change("root.primitivesList.name.stringVal = 10"),
                 "'String' is expected as a type for the 'root.primitivesList.name.stringVal' configuration value"
         );
+
+        assertThrowsIllegalArgException(
+                () -> change("root.primitivesList.name.uuidVal = 123"),
+                "'UUID' is expected as a type for the 'root.primitivesList.name.uuidVal' configuration value"
+        );
     }
 
     /**
-     * Tests that the {@code HoconConverter} supports deserialization of arrays of Strings and primitives.
+     * Tests that the {@code HoconConverter} supports deserialization of arrays of {@link String}, {@link UUID} and primitives.
      */
     @Test
     public void testHoconArraysDeserialization() throws Throwable {
@@ -533,6 +562,15 @@ public class HoconConverterTest {
 
         change("root.arraysList.name.strings = [foo]");
         assertThat(arrays.strings().value(), is(new String[]{"foo"}));
+
+        UUID newUuid0 = UUID.randomUUID();
+        UUID newUuid1 = UUID.randomUUID();
+
+        change("root.arraysList.name.uuids = [" + newUuid0 + "]");
+        assertThat(arrays.uuids().value(), is(new UUID[]{newUuid0}));
+
+        change("root.arraysList.name.uuids = [\"" + newUuid1 + "\"]");
+        assertThat(arrays.uuids().value(), is(new UUID[]{newUuid1}));
     }
 
     /**
@@ -609,6 +647,11 @@ public class HoconConverterTest {
                 () -> change("root.arraysList.name.strings = 10"),
                 "'String[]' is expected as a type for the 'root.arraysList.name.strings' configuration value"
         );
+
+        assertThrowsIllegalArgException(
+                () -> change("root.arraysList.name.uuids = uuids"),
+                "'UUID[]' is expected as a type for the 'root.arraysList.name.uuids' configuration value"
+        );
     }
 
     @Test