You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/02/21 10:04:43 UTC

[groovy] branch GROOVY-10500 created (now 0663f0f)

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

emilles pushed a change to branch GROOVY-10500
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 0663f0f  GROOVY-10500: `@NamedVariant`: slice named args map for `@NamedDelegate`

This branch includes the following new commits:

     new 0663f0f  GROOVY-10500: `@NamedVariant`: slice named args map for `@NamedDelegate`

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.


[groovy] 01/01: GROOVY-10500: `@NamedVariant`: slice named args map for `@NamedDelegate`

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

emilles pushed a commit to branch GROOVY-10500
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 0663f0f94ff14976a6be5356c7b135765495a975
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Feb 21 03:58:46 2022 -0600

    GROOVY-10500: `@NamedVariant`: slice named args map for `@NamedDelegate`
---
 .../transform/NamedVariantASTTransformation.java   | 30 ++++++++--------------
 .../transform/NamedVariantTransformTest.groovy     |  7 ++---
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
index 9bc6d18..82924b7 100644
--- a/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
@@ -31,7 +31,6 @@ import org.codehaus.groovy.ast.Parameter;
 import org.codehaus.groovy.ast.PropertyNode;
 import org.codehaus.groovy.ast.expr.ArgumentListExpression;
 import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.ast.expr.MapEntryExpression;
 import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.stmt.AssertStatement;
 import org.codehaus.groovy.ast.stmt.BlockStatement;
@@ -43,7 +42,6 @@ import org.codehaus.groovy.control.SourceUnit;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Optional;
 import java.util.Set;
 
 import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedConstructor;
@@ -66,10 +64,8 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.defaultValueX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.elvisX;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.plusX;
@@ -118,8 +114,7 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
             }
         }
 
-        if (!annoFound && autoDelegate) {
-            // assume the first param is the delegate by default
+        if (!annoFound && autoDelegate) { // the first param is the delegate
             processDelegateParam(mNode, mapParam, args, propNames, fromParams[0], coerce);
         } else {
             for (Parameter fromParam : fromParams) {
@@ -176,7 +171,7 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
             type = fromParam.getType();
             namedParam.addMember("type", classX(type));
         } else {
-            // TODO: Check attribute type is is assignable to declared param type?
+            // TODO: Check attribute type is assignable to declared param type?
         }
 
         boolean required = memberHasValue(namedParam, "required", true);
@@ -202,22 +197,19 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
 
         Set<String> names = new HashSet<>();
         List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
-        for (String next : names) {
-            if (hasDuplicates(this, mNode, propNames, next)) return false;
+        for (String name : names) {
+            if (hasDuplicates(this, mNode, propNames, name)) return false;
         }
-        List<MapEntryExpression> entries = new ArrayList<>();
-        for (PropertyNode pNode : props) {
-            String name = pNode.getName();
-            // create entry [name: __namedArgs.getOrDefault('name', initialValue)]
-            Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> defaultValueX(pNode.getType()));
-            entries.add(entryX(constX(name), asType(callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue)), pNode.getType(), coerce)));
-            // create annotation @NamedParam(value='name', type=DelegateType)
+        for (PropertyNode prop : props) {
+            // create annotation @NamedParam(value='name', type=PropertyType)
             AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
-            namedParam.addMember("value", constX(name));
-            namedParam.addMember("type", classX(pNode.getType()));
+            namedParam.addMember("value", constX(prop.getName()));
+            namedParam.addMember("type", classX(prop.getType()));
             mapParam.addAnnotation(namedParam);
         }
-        Expression delegateMap = mapX(entries);
+
+        Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new);
+        Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs));
         args.addExpression(castX(fromParam.getType(), delegateMap));
         return true;
     }
diff --git a/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy b/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy
index 0c882fd..58e3428 100644
--- a/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy
@@ -310,7 +310,7 @@ final class NamedVariantTransformTest {
     }
 
     @Test // GROOVY-10261
-    void testNamedDelegateWithDefaultArguments() {
+    void testNamedVariantWithDefaultArguments() {
         assertScript '''
             import groovy.transform.*
 
@@ -332,7 +332,7 @@ final class NamedVariantTransformTest {
         '''
     }
 
-    @Test // GROOVY-9183
+    @Test // GROOVY-9183, GROOVY-10500
     void testNamedDelegateWithPropertyDefaults() {
         assertScript '''
             import groovy.transform.*
@@ -361,7 +361,8 @@ final class NamedVariantTransformTest {
                     String separator = ','
                     Boolean headers = true
                     Integer headersRow = 0
-                    Integer firstDataRow = 1
+                    Integer firstDataRow = FIRST_DATA_ROW
+                    private static final int FIRST_DATA_ROW = 1
                 }
             }