You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/07/28 11:00:11 UTC

[sling-org-apache-sling-scripting-sightly-runtime] branch feature/SLING-10679-improve-to-collection created (now 7b64f09)

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

kwin pushed a change to branch feature/SLING-10679-improve-to-collection
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-runtime.git.


      at 7b64f09  SLING-10679 wrap every object in a single item list

This branch includes the following new commits:

     new 7b64f09  SLING-10679 wrap every object in a single item list

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-scripting-sightly-runtime] 01/01: SLING-10679 wrap every object in a single item list

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/SLING-10679-improve-to-collection
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-runtime.git

commit 7b64f09b35865d50a16a521a25de486f3d4036d6
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Jul 28 12:59:54 2021 +0200

    SLING-10679 wrap every object in a single item list
---
 .../apache/sling/scripting/sightly/render/ObjectModel.java    | 11 ++---------
 .../sling/scripting/sightly/render/ObjectModelTest.java       |  3 ++-
 2 files changed, 4 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/sightly/render/ObjectModel.java b/src/main/java/org/apache/sling/scripting/sightly/render/ObjectModel.java
index c9dfaf4..eb706d1 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/render/ObjectModel.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/render/ObjectModel.java
@@ -266,9 +266,7 @@ public final class ObjectModel {
      *     <li>if the {@code object} is an instance of an {@link Enumeration} a list transformation will be returned</li>
      *     <li>if the {@code object} is an instance of an {@link Iterator} or {@link Iterable} the result of {@link #fromIterator(Iterator)}
      *     will be returned</li>
-     *     <li>if the {@code object} is an instance of a {@link String} or {@link Number} a {@link Collection} containing only this
-     *     object will be returned</li>
-     *     <li>any other case not covered by the previous rules will result in an empty {@link Collection}</li>
+     *     <li>otherwise the {@code object} is wrapped in a single item list</li>
      * </ul>
      *
      * @param object the target object
@@ -308,12 +306,7 @@ public final class ObjectModel {
             Iterable<Object> iterable = (Iterable<Object>) object;
             return fromIterator(iterable.iterator());
         }
-        if (object instanceof String || object instanceof Number) {
-            Collection<Object> list = new ArrayList<>();
-            list.add(object);
-            return list;
-        }
-        return Collections.emptyList();
+        return Collections.singletonList(object);
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/scripting/sightly/render/ObjectModelTest.java b/src/test/java/org/apache/sling/scripting/sightly/render/ObjectModelTest.java
index 00cf89b..8d7f83a 100644
--- a/src/test/java/org/apache/sling/scripting/sightly/render/ObjectModelTest.java
+++ b/src/test/java/org/apache/sling/scripting/sightly/render/ObjectModelTest.java
@@ -138,7 +138,8 @@ public class ObjectModelTest {
     @Test
     public void testToCollection() {
         assertTrue(ObjectModel.toCollection(null).isEmpty());
-        assertTrue(ObjectModel.toCollection(new StringBuilder()).isEmpty());
+        StringBuilder sb = new StringBuilder();
+        assertEquals(Collections.singletonList(sb), ObjectModel.toCollection(sb));
         Integer[] testArray = new Integer[] {1, 2, 3};
         int[] testPrimitiveArray = new int[] {1, 2, 3};
         List<Integer> testList = Arrays.asList(testArray);