You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:57:19 UTC

[sling-org-apache-sling-pipes] 03/19: SLING-6799 - add support for MV expressions in write pipe

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

rombert pushed a commit to annotated tag org.apache.sling.pipes-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git

commit 0b66cbc244c789c5c84821a3a54ecaca64b4c360
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Thu Apr 27 11:43:20 2017 +0000

    SLING-6799 - add support for MV expressions in write pipe
    
    This closes #219
    
    Submitted-By: Nicolas Peltier
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/sling-pipes@1792869 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/sling/pipes/internal/WritePipe.java | 52 +++++++++++++---------
 .../apache/sling/pipes/internal/WritePipeTest.java |  4 +-
 src/test/resources/write.json                      |  3 +-
 3 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/internal/WritePipe.java b/src/main/java/org/apache/sling/pipes/internal/WritePipe.java
index a03f3ba..149803c 100644
--- a/src/main/java/org/apache/sling/pipes/internal/WritePipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/WritePipe.java
@@ -64,8 +64,9 @@ public class WritePipe extends BasePipe {
         resourceExpression = getPath();
     }
 
+
     /**
-     * convert the configured value (can be an expression) in a value that can be written in a resource.
+     * convert the configured string value (can be an expression) in a value that can be written in a resource.
      * also handles patch for multivalue properties like <code>+[value]</code> in which case <code>value</code>
      * is added to the MV property
      * @param resource resource to which value will be written
@@ -73,28 +74,39 @@ public class WritePipe extends BasePipe {
      * @param expression configured value to write
      * @return actual value to write to the resource
      */
+    protected Object computeValue(Resource resource, String key, String expression){
+        Object value = bindings.instantiateObject((String) expression);
+        if (value != null && value instanceof String) {
+            //in that case we treat special case like MV or patches
+            String sValue = (String)value;
+            Matcher patch = addPatch.matcher(sValue);
+            if (patch.matches()) {
+                String newValue = patch.group(1);
+                String[] actualValues = resource.adaptTo(ValueMap.class).get(key, String[].class);
+                List<String> newValues = actualValues != null ? new LinkedList<>(Arrays.asList(actualValues)) : new ArrayList<String>();
+                if (!newValues.contains(newValue)) {
+                    newValues.add(newValue);
+                }
+                return newValues.toArray(new String[newValues.size()]);
+            }
+            Matcher multiMatcher = multi.matcher(sValue);
+            if (multiMatcher.matches()) {
+                return multiMatcher.group(1).split(",");
+            }
+        }
+        return value;
+    }
+
+
     protected Object computeValue(Resource resource, String key, Object expression) {
         if (expression instanceof String) {
-            Object value = bindings.instantiateObject((String) expression);
-            if (value != null && value instanceof String) {
-                //in that case we treat special case like MV or patches
-                String sValue = (String)value;
-                Matcher patch = addPatch.matcher(sValue);
-                if (patch.matches()) {
-                    String newValue = patch.group(1);
-                    String[] actualValues = resource.adaptTo(ValueMap.class).get(key, String[].class);
-                    List<String> newValues = actualValues != null ? new LinkedList<>(Arrays.asList(actualValues)) : new ArrayList<String>();
-                    if (!newValues.contains(newValue)) {
-                        newValues.add(newValue);
-                    }
-                    return newValues.toArray(new String[newValues.size()]);
-                }
-                Matcher multiMatcher = multi.matcher(sValue);
-                if (multiMatcher.matches()) {
-                    return multiMatcher.group(1).split(",");
-                }
+            return computeValue(resource, key, (String)expression);
+        } else if (expression instanceof String[]){
+            List<String> values = new ArrayList<>();
+            for (String expr : (String[])expression){
+                values.add((String)computeValue(resource, key, expr));
             }
-            return value;
+            return values.toArray(new String[values.size()]);
         }
         return expression;
     }
diff --git a/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java b/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java
index fdb1d79..d464c96 100644
--- a/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java
@@ -64,8 +64,10 @@ public class WritePipeTest extends AbstractPipeTest {
      */
     public static void assertPiped(Resource resource) {
         ValueMap properties = resource.adaptTo(ValueMap.class);
+        String[] array = new String[]{"cabbage","carrot"};
         assertArrayEquals("Second fruit should have been correctly instantiated & patched, added to the first", new String[]{"apple","banana"}, properties.get("fruits", String[].class));
-        assertArrayEquals("Fixed mv should be there", new String[]{"cabbage","carrot"}, properties.get("fixedVegetables", String[].class));
+        assertArrayEquals("Fixed mv should be there", array, properties.get("fixedVegetables", String[].class));
+        assertArrayEquals("Expr fixed mv should there and computed", array, properties.get("computedVegetables", String[].class));
     }
 
     @Test
diff --git a/src/test/resources/write.json b/src/test/resources/write.json
index 4af4e1c..6be4f2c 100644
--- a/src/test/resources/write.json
+++ b/src/test/resources/write.json
@@ -31,7 +31,8 @@
         "conf":{
           "jcr:primaryType":"nt:unstructured",
           "fruits": "+[${fruit.name}]",
-          "fixedVegetables":"[cabbage,carrot]"
+          "fixedVegetables":"[cabbage,carrot]",
+          "computedVegetables":["${'cabbage'}","${'carrot'}"]
         }
       }
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.