curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [34/35] curator git commit: support alternate file formats for schema. E.g. yaml
Date Thu, 19 May 2016 19:02:49 GMT
support alternate file formats for schema. E.g. yaml


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/5533d08a
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/5533d08a
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/5533d08a

Branch: refs/heads/CURATOR-3.0
Commit: 5533d08aa5c44cafab39bc77e41fc6176db339e7
Parents: b6b9006
Author: randgalt <randgalt@apache.org>
Authored: Thu May 19 13:35:05 2016 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Thu May 19 13:35:05 2016 -0500

----------------------------------------------------------------------
 curator-framework/pom.xml                       |  7 +++
 .../framework/schema/SchemaSetLoader.java       | 47 +++++++++++++++-----
 .../curator/framework/schema/TestSchema.java    | 19 +++++++-
 .../src/test/resources/schema.yaml              | 14 ++++++
 4 files changed, 74 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/5533d08a/curator-framework/pom.xml
----------------------------------------------------------------------
diff --git a/curator-framework/pom.xml b/curator-framework/pom.xml
index c47e265..45a90b6 100644
--- a/curator-framework/pom.xml
+++ b/curator-framework/pom.xml
@@ -69,6 +69,13 @@
         </dependency>
 
         <dependency>
+            <groupId>com.fasterxml.jackson.dataformat</groupId>
+            <artifactId>jackson-dataformat-yaml</artifactId>
+            <version>${jackson-version}</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/curator/blob/5533d08a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
index 16b387e..bab97c6 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
@@ -78,26 +78,39 @@ public class SchemaSetLoader
         SchemaValidator getSchemaValidator(String name);
     }
 
+    /**
+     * @param json the json to parse
+     * @param schemaValidatorMapper mapper from validator name to instance - can be null
if not needed
+     */
     public SchemaSetLoader(String json, SchemaValidatorMapper schemaValidatorMapper)
     {
-        this(new StringReader(json), schemaValidatorMapper);
+        this(getRoot(new StringReader(json)), schemaValidatorMapper);
+    }
+
+    /**
+     * @param jsonStream the json stream to parse
+     * @param schemaValidatorMapper mapper from validator name to instance - can be null
if not needed
+     */
+    public SchemaSetLoader(Reader jsonStream, SchemaValidatorMapper schemaValidatorMapper)
+    {
+        this(getRoot(jsonStream), schemaValidatorMapper);
     }
 
-    public SchemaSetLoader(Reader in, SchemaValidatorMapper schemaValidatorMapper)
+    /**
+     * @param root a Jackson root node
+     * @param schemaValidatorMapper mapper from validator name to instance - can be null
if not needed
+     */
+    public SchemaSetLoader(JsonNode root, SchemaValidatorMapper schemaValidatorMapper)
     {
         ImmutableList.Builder<Schema> builder = ImmutableList.builder();
-        try
-        {
-            JsonNode root = new ObjectMapper().readTree(in);
-            read(builder, root, schemaValidatorMapper);
-        }
-        catch ( IOException e )
-        {
-            throw new RuntimeException(e);
-        }
+        read(builder, root, schemaValidatorMapper);
         schemas = builder.build();
     }
 
+    /**
+     * @param useDefaultSchema if true, return a default schema when there is no match. Otherwise,
an exception is thrown
+     * @return schema set
+     */
     public SchemaSet toSchemaSet(boolean useDefaultSchema)
     {
         return new SchemaSet(schemas, useDefaultSchema);
@@ -108,6 +121,18 @@ public class SchemaSetLoader
         return schemas;
     }
 
+    private static JsonNode getRoot(Reader in)
+    {
+        try
+        {
+            return new ObjectMapper().readTree(in);
+        }
+        catch ( IOException e )
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
     private void read(ImmutableList.Builder<Schema> builder, JsonNode node, SchemaValidatorMapper
schemaValidatorMapper)
     {
         for ( JsonNode child : node )

http://git-wip-us.apache.org/repos/asf/curator/blob/5533d08a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index 482d17f..6fb946e 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -18,8 +18,10 @@
  */
 package org.apache.curator.framework.schema;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import com.google.common.base.Charsets;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.io.Resources;
 import org.apache.curator.framework.CuratorFramework;
@@ -35,7 +37,6 @@ import org.testng.annotations.Test;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
-import java.util.regex.Pattern;
 
 public class TestSchema extends BaseClassForTests
 {
@@ -236,6 +237,20 @@ public class TestSchema extends BaseClassForTests
     }
 
     @Test
+    public void testYaml() throws Exception
+    {
+        String yaml = Resources.toString(Resources.getResource("schema.yaml"), Charsets.UTF_8);
+        JsonNode root = new ObjectMapper(new YAMLFactory()).readTree(yaml);
+        List<Schema> schemas = new SchemaSetLoader(root, null).getSchemas();
+        Assert.assertEquals(schemas.size(), 2);
+        Assert.assertEquals(schemas.get(0).getName(), "test");
+        Assert.assertEquals(schemas.get(0).getMetadata().size(), 0);
+        Assert.assertEquals(schemas.get(1).getName(), "test2");
+        Assert.assertEquals(schemas.get(1).getMetadata().size(), 2);
+        Assert.assertEquals(schemas.get(1).getMetadata().get("two"), "2");
+    }
+
+    @Test
     public void testOrdering() throws Exception
     {
         SchemaSet schemaSet = loadSchemaSet("schema5.json", null);

http://git-wip-us.apache.org/repos/asf/curator/blob/5533d08a/curator-framework/src/test/resources/schema.yaml
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema.yaml b/curator-framework/src/test/resources/schema.yaml
new file mode 100644
index 0000000..b342d40
--- /dev/null
+++ b/curator-framework/src/test/resources/schema.yaml
@@ -0,0 +1,14 @@
+- name: test
+  path: /a/b/c
+  documentation: This is a schema
+  ephemeral: must
+  sequential: cannot
+
+- name: test2
+  path: /a/.*
+  isRegex: true
+  ephemeral: cannot
+  canBeDeleted: false
+  metadata:
+    one: 1
+    two: 2


Mime
View raw message