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

npeltier pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git


The following commit(s) were added to refs/heads/master by this push:
     new 697dd7c  SLING-10759 multiline json script
697dd7c is described below

commit 697dd7cfa1645c8838db397f461e55e1c516fe38
Author: Nicolas Peltier <[email protected]>
AuthorDate: Fri Aug 27 10:48:50 2021 +0200

    SLING-10759 multiline json script
---
 .../org/apache/sling/pipes/internal/CommandExecutorImpl.java  | 11 +++++++++++
 .../org/apache/sling/pipes/internal/inputstream/JsonPipe.java |  3 ++-
 .../apache/sling/pipes/internal/CommandExecutorImplTest.java  | 11 ++++++++++-
 src/test/resources/commandsFormats.txt                        |  6 +++++-
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java 
b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
index 4dc200e..b96639b 100644
--- a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
@@ -28,6 +28,7 @@ import java.nio.charset.StandardCharsets;
 import java.security.AccessControlException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -48,6 +49,7 @@ import org.apache.sling.pipes.Pipe;
 import org.apache.sling.pipes.PipeBuilder;
 import org.apache.sling.pipes.PipeExecutor;
 import org.apache.sling.pipes.Plumber;
+import org.apache.sling.pipes.internal.inputstream.JsonPipe;
 import org.jetbrains.annotations.NotNull;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
@@ -64,6 +66,7 @@ import static org.apache.commons.lang3.StringUtils.EMPTY;
 import static org.apache.sling.pipes.internal.CommandUtil.keyValuesToArray;
 import static org.apache.sling.pipes.internal.CommandUtil.writeToMap;
 
+import javax.json.Json;
 import javax.json.JsonException;
 import javax.servlet.Servlet;
 
@@ -86,6 +89,9 @@ public class CommandExecutorImpl extends 
AbstractPlumberServlet implements Comma
     static final String PIPE_SEPARATOR = WHITE_SPACE_SEPARATOR + "*\\" + 
SEPARATOR + WHITE_SPACE_SEPARATOR + "*";
     static final String LINE_SEPARATOR = " ";
     static final String PARAMS = "@";
+    static final List<String> JSON_EXPR_KEYS = 
Arrays.asList(JsonPipe.JSON_KEY);
+    static final String JSON_START = "\"[{";
+
     static final String PARAMS_SEPARATOR = WHITE_SPACE_SEPARATOR + "+" + 
PARAMS + WHITE_SPACE_SEPARATOR + "*";
     static final Pattern SUB_TOKEN_PATTERN = 
Pattern.compile("(([^\"]\\S*)|\"([^\"]+)\")\\s*");
     static final String KEY_NAME = "name";
@@ -428,6 +434,11 @@ public class CommandExecutorImpl extends 
AbstractPlumberServlet implements Comma
                 currentToken.pipeKey = subTokens.get(0);
                 if (subTokens.size() > 1) {
                     currentToken.args = subTokens.subList(1, subTokens.size());
+                    if (JSON_EXPR_KEYS.contains(currentToken.pipeKey) &&
+                            
JSON_START.indexOf(currentToken.args.get(0).getBytes(StandardCharsets.UTF_8)[0])
 > 0) {
+                        //in that case we want to concatenate all subsequent 
'args' as it is a JSON expression
+                        currentToken.args = 
Collections.singletonList(String.join(EMPTY, currentToken.args));
+                    }
                 }
             }
             log.trace("generated following token {}", currentToken);
diff --git 
a/src/main/java/org/apache/sling/pipes/internal/inputstream/JsonPipe.java 
b/src/main/java/org/apache/sling/pipes/internal/inputstream/JsonPipe.java
index 8ff0a91..4165142 100644
--- a/src/main/java/org/apache/sling/pipes/internal/inputstream/JsonPipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/inputstream/JsonPipe.java
@@ -48,7 +48,8 @@ import java.util.stream.Collectors;
  */
 public class JsonPipe extends AbstractInputStreamPipe {
     private static Logger logger = LoggerFactory.getLogger(JsonPipe.class);
-    public static final String RESOURCE_TYPE = RT_PREFIX + "json";
+    public static final String JSON_KEY = "json";
+    public static final String RESOURCE_TYPE = RT_PREFIX + JSON_KEY;
 
     /**
      * property specifying the json path where to fetched the used value
diff --git 
a/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java 
b/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
index 0bab6fb..5b06d97 100644
--- a/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
@@ -87,6 +87,14 @@ public class CommandExecutorImplTest extends 
AbstractPipeTest {
     }
 
     @Test
+    public void testParseJson() {
+        String tokenString = "json [{\"title\":\"this is the first\", 
\"path\":\"/content/nested/two\"}, {\"title\":\"this is the second\", 
\"path\":\"/content/nested/three\"}]";
+        List<CommandExecutorImpl.Token> tokens = 
commands.parseTokens(tokenString);
+        assertEquals("there should be 1 token", 1, tokens.size());
+        assertEquals("there should be 1 arg", 1, tokens.get(0).args.size());
+    }
+
+    @Test
     public void testQuotedTokens() {
         List<CommandExecutorImpl.Token> tokens = commands.parseTokens("some 
isolated items \"with quotes\"");
         assertEquals("there should be 1 token", 1, tokens.size());
@@ -216,11 +224,12 @@ public class CommandExecutorImplTest extends 
AbstractPipeTest {
         request.setParameterMap(params);
         request.setMethod("POST");
         List<String> cmdList = commands.getCommandList(context.request());
-        assertEquals(4, cmdList.size());
+        assertEquals(5, cmdList.size());
         for (int i = 0; i < 3; i ++) {
             assertEquals("echo /content | $ /apps/pipes-it/fruit | children 
nt:unstructured", cmdList.get(i));
         }
         assertEquals ("echo /content | write one=foo nested/two=foo 
nested/three=foo", cmdList.get(3));
+        assertEquals("echo /content | json [{\"title\":\"this is the first\", 
\"path\":\"/content/nested/two\"}, {\"title\":\"this is the second\", 
\"path\":\"/content/nested/three\"}] @ name test | echo ${test.path}", 
cmdList.get(4));
     }
 
     String[] getItemsArray(JsonObject response) {
diff --git a/src/test/resources/commandsFormats.txt 
b/src/test/resources/commandsFormats.txt
index 505ce48..73c5f3e 100644
--- a/src/test/resources/commandsFormats.txt
+++ b/src/test/resources/commandsFormats.txt
@@ -27,4 +27,8 @@ children nt:unstructured
 echo /content |
 write one=foo
 nested/two=foo
-nested/three=foo
\ No newline at end of file
+nested/three=foo
+
+echo /content | json [{"title":"this is the first", 
"path":"/content/nested/two"},
+{"title":"this is the second", "path":"/content/nested/three"}] @ name test
+| echo ${test.path}
\ No newline at end of file

Reply via email to