Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 52a6c708f -> 5fa14442d


Adds Yamls.getAt utility

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/d3878877
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/d3878877
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/d3878877

Branch: refs/heads/master
Commit: d38788771c5ed67d7ab8fbdf105995a252174932
Parents: ab46e6f
Author: Aled Sage <[email protected]>
Authored: Tue Mar 17 14:46:28 2015 +0000
Committer: Aled Sage <[email protected]>
Committed: Tue Mar 17 14:46:28 2015 +0000

----------------------------------------------------------------------
 .../src/main/java/brooklyn/util/yaml/Yamls.java | 41 +++++++++++++++++
 .../test/java/brooklyn/util/yaml/YamlsTest.java | 47 ++++++++++++++++++++
 2 files changed, 88 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d3878877/utils/common/src/main/java/brooklyn/util/yaml/Yamls.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/yaml/Yamls.java 
b/utils/common/src/main/java/brooklyn/util/yaml/Yamls.java
index cad4395..7c946dd 100644
--- a/utils/common/src/main/java/brooklyn/util/yaml/Yamls.java
+++ b/utils/common/src/main/java/brooklyn/util/yaml/Yamls.java
@@ -66,6 +66,47 @@ public class Yamls {
         return (T)x;
     }
 
+    /**
+     * Parses the given yaml, and walks the given path to return the 
referenced object.
+     * 
+     * @see #getAt(Object, List)
+     */
+    public static Object getAt(String yaml, List<String> path) {
+        Iterable<Object> result = new org.yaml.snakeyaml.Yaml().loadAll(yaml);
+        Object current = result.iterator().next();
+        return getAtPreParsed(current, path);
+    }
+    
+    /** 
+     * For pre-parsed yaml, walks the maps/lists to return the given sub-item.
+     * In the given path:
+     * <ul>
+     *   <li>A vanilla string is assumed to be a key into a map.
+     *   <li>A string in the form like "[0]" is assumed to be an index into a 
list
+     * </ul>
+     * 
+     * Returns {@code null} if that path does not exist. 
+     */
+    @SuppressWarnings("unchecked")
+    public static Object getAtPreParsed(Object current, List<String> path) {
+        for (String pathPart : path) {
+            if (pathPart.startsWith("[") && pathPart.endsWith("]")) {
+                String index = pathPart.substring(1, pathPart.length()-1);
+                try {
+                    current = Iterables.get((Iterable<?>)current, 
Integer.parseInt(index));
+                } catch (NumberFormatException e) {
+                    throw new IllegalArgumentException("Invalid index 
'"+index+"', in path "+path);
+                } catch (IndexOutOfBoundsException e) {
+                    throw new IllegalArgumentException("Invalid index 
'"+index+"', in path "+path);
+                }
+            } else {
+                current = ((Map<String, ?>)current).get(pathPart);
+            }
+            if (current == null) return null;
+        }
+        return current;
+    }
+
     @SuppressWarnings("rawtypes")
     public static void dump(int depth, Object r) {
         if (r instanceof Iterable) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d3878877/utils/common/src/test/java/brooklyn/util/yaml/YamlsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/yaml/YamlsTest.java 
b/utils/common/src/test/java/brooklyn/util/yaml/YamlsTest.java
new file mode 100644
index 0000000..27437d9
--- /dev/null
+++ b/utils/common/src/test/java/brooklyn/util/yaml/YamlsTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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
+ *
+ *     http://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.
+ */
+package brooklyn.util.yaml;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+public class YamlsTest {
+
+    @Test
+    public void testGetAt() throws Exception {
+        // leaf of map
+        assertEquals(Yamls.getAt("k1: v", ImmutableList.of("k1")), "v");
+        assertEquals(Yamls.getAt("k1: {k2: v}", ImmutableList.of("k1", "k2")), 
"v");
+        
+        // get list
+        assertEquals(Yamls.getAt("k1: [v1, v2]", 
ImmutableList.<String>of("k1")), ImmutableList.of("v1", "v2"));
+
+        // get map
+        assertEquals(Yamls.getAt("k1: v", ImmutableList.<String>of()), 
ImmutableMap.of("k1", "v"));
+        assertEquals(Yamls.getAt("k1: {k2: v}", ImmutableList.of("k1")), 
ImmutableMap.of("k2", "v"));
+        
+        // get array index
+        assertEquals(Yamls.getAt("k1: [v1, v2]", 
ImmutableList.<String>of("k1", "[0]")), "v1");
+        assertEquals(Yamls.getAt("k1: [v1, v2]", 
ImmutableList.<String>of("k1", "[1]")), "v2");
+    }
+}

Reply via email to