You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/01/04 16:45:29 UTC

[isis] branch 2033-Spring_Data_Integration updated: ISIS-2478: fixes CommandDtoUtils to handle non-scalar values (2)

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

ahuber pushed a commit to branch 2033-Spring_Data_Integration
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/2033-Spring_Data_Integration by this push:
     new 22155c4  ISIS-2478: fixes CommandDtoUtils to handle non-scalar values (2)
22155c4 is described below

commit 22155c4fbbc415a57b23574f7d3156fe6bb72d12
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Jan 4 17:45:15 2021 +0100

    ISIS-2478: fixes CommandDtoUtils to handle non-scalar values (2)
---
 .../isis/applib/util/schema/CommonDtoUtils.java    | 38 ++++++++++++++++++----
 .../applib/util/schema/InteractionDtoUtils.java    |  7 +---
 .../apache/isis/applib/util/schema/Roundtrip.java  |  9 ++---
 3 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/isis/applib/util/schema/CommonDtoUtils.java b/api/applib/src/main/java/org/apache/isis/applib/util/schema/CommonDtoUtils.java
index 86821c74..b43a7d1 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/util/schema/CommonDtoUtils.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/util/schema/CommonDtoUtils.java
@@ -47,6 +47,7 @@ import org.apache.isis.applib.value.Blob;
 import org.apache.isis.applib.value.Clob;
 import org.apache.isis.commons.internal.base._Casts;
 import org.apache.isis.commons.internal.base._NullSafe;
+import org.apache.isis.commons.internal.base._Refs;
 import org.apache.isis.commons.internal.base._Strings;
 import org.apache.isis.commons.internal.collections._Maps;
 import org.apache.isis.commons.internal.context._Context;
@@ -188,7 +189,7 @@ public final class CommonDtoUtils {
 
         switch (valueType) {
         case COLLECTION: {
-            final CollectionDto collectionDto = asCollectionDto(pojo, ValueType.REFERENCE, bookmarkService);
+            final CollectionDto collectionDto = asCollectionDto(pojo, ValueType.VOID, bookmarkService);
             valueDto.setCollection(collectionDto);
             return valueDto;
         }
@@ -364,23 +365,46 @@ public final class CommonDtoUtils {
         val collectionDto = new CollectionDto();
         collectionDto.setType(commonElementValueType);
         
+        val needsCommonElementValueTypeAutodetect = commonElementValueType==ValueType.VOID;
+        
+        val commonElementValueTypeRef = _Refs.<ValueType>objectRef(null);
+        
         _NullSafe.streamAutodetect(iterableOrArray)
         .forEach(element->{
             val valueDto = new ValueDto();
-            
-            val elementValueType = element!=null
-                    ? asValueType(element.getClass())
-                    : ValueType.REFERENCE;
-            
-            setValueOn(valueDto, elementValueType, element, bookmarkService);
+            if(element==null) {
+                setValueOn(valueDto, ValueType.VOID, element, bookmarkService);
+            } else {
+                val elementValueType = asValueType(element.getClass()); 
+                setValueOn(valueDto, elementValueType, element, bookmarkService);
+                
+                if(needsCommonElementValueTypeAutodetect) {
+                    commonElementValueTypeRef.update(acc->reduce(acc, elementValueType));
+                }
+                
+            }
             collectionDto.getValue().add(valueDto);
         });
         
+        if(needsCommonElementValueTypeAutodetect) {
+            collectionDto.setType(commonElementValueTypeRef.getValueElseDefault(ValueType.VOID));
+        }
+        
         return collectionDto;
     }
 
     // -- getValue (from valueDto)
 
+    private static ValueType reduce(ValueType acc, ValueType next) {
+        if(acc==null) {
+            return next;    
+        }
+        if(acc==next) {
+            return acc;    
+        }
+        throw _Exceptions.unsupportedOperation("mixing types within a collection is not supported yet");    
+    }
+
     public static <T> T getValue(
             final ValueDto valueDto,
             final ValueType valueType) {
diff --git a/api/applib/src/main/java/org/apache/isis/applib/util/schema/InteractionDtoUtils.java b/api/applib/src/main/java/org/apache/isis/applib/util/schema/InteractionDtoUtils.java
index dd8419a..3aafd63 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/util/schema/InteractionDtoUtils.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/util/schema/InteractionDtoUtils.java
@@ -429,17 +429,12 @@ public final class InteractionDtoUtils {
 
 
     // -- getParameterArgValue
-    public static <T> T getParameterArgValue(final ActionInvocationDto ai, int paramNum, Class<T> inferClass) {
-        final ParamDto paramDto = getParameter(ai, paramNum);
-        return CommonDtoUtils.getValue(paramDto);
-    }
+
     public static <T> T getParameterArgValue(final ActionInvocationDto ai, int paramNum) {
         final ParamDto paramDto = getParameter(ai, paramNum);
         return CommonDtoUtils.getValue(paramDto);
     }
 
-
-
     // -- debugging (dump)
     public static void dump(final InteractionDto ixnDto, final PrintStream out) throws JAXBException {
         out.println(toXml(ixnDto));
diff --git a/api/applib/src/test/java/org/apache/isis/applib/util/schema/Roundtrip.java b/api/applib/src/test/java/org/apache/isis/applib/util/schema/Roundtrip.java
index 4073059..98027d0 100644
--- a/api/applib/src/test/java/org/apache/isis/applib/util/schema/Roundtrip.java
+++ b/api/applib/src/test/java/org/apache/isis/applib/util/schema/Roundtrip.java
@@ -47,10 +47,8 @@ import org.apache.isis.commons.internal.base._NullSafe;
 import org.apache.isis.commons.internal.collections._Lists;
 import org.apache.isis.commons.internal.collections._Sets;
 import org.apache.isis.schema.cmd.v2.ParamDto;
-import org.apache.isis.schema.common.v2.CollectionDto;
 import org.apache.isis.schema.common.v2.InteractionType;
 import org.apache.isis.schema.common.v2.OidDto;
-import org.apache.isis.schema.common.v2.ValueDto;
 import org.apache.isis.schema.common.v2.ValueType;
 import org.apache.isis.schema.common.v2.ValueWithTypeDto;
 import org.apache.isis.schema.ixn.v2.ActionInvocationDto;
@@ -119,7 +117,7 @@ public class Roundtrip {
         assertThat(InteractionDtoUtils.getParameterType(invocationDto, param), Matchers.is(valueType));
         assertThat(InteractionDtoUtils.isNull(invocationDto, param), is(false));
         
-        val actualValue = InteractionDtoUtils.getParameterArgValue(invocationDto, param, type);
+        val actualValue = InteractionDtoUtils.getParameterArgValue(invocationDto, param);
         
         // equals test, some types need special checks ...
         if(expectedValue instanceof OidDto) {
@@ -134,10 +132,7 @@ public class Roundtrip {
         } else if(expectedValue instanceof Iterable
                 || expectedValue.getClass().isArray()) {
             
-            val actualAsCan = ((CollectionDto) actualValue).getValue().stream()
-            .map(ValueDto::getLong)
-            .collect(Can.toCan());
-            
+            val actualAsCan = Can.ofStream(_NullSafe.streamAutodetect(actualValue));
             val expectedAsCan = Can.ofStream(_NullSafe.streamAutodetect(expectedValue));
             
             assertThat(actualAsCan, is(expectedAsCan));