You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/07/25 02:47:19 UTC
[groovy] branch master updated: Support macro method `GQL` with
options
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 4312685 Support macro method `GQL` with options
4312685 is described below
commit 43126858df073f818536b49dd63f88177048f0f3
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jul 25 10:46:55 2021 +0800
Support macro method `GQL` with options
---
.../apache/groovy/ginq/GinqGroovyMethods.groovy | 21 +++++++++++++----
.../groovy-ginq/src/spec/doc/ginq-userguide.adoc | 2 +-
.../test/org/apache/groovy/ginq/GinqTest.groovy | 27 ++++++++++++++++++++++
3 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy
index b522c19..5a564ac 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy
@@ -27,7 +27,6 @@ import org.apache.groovy.ginq.dsl.expression.GinqExpression
import org.apache.groovy.ginq.provider.collection.GinqAstWalker
import org.apache.groovy.lang.annotation.Incubating
import org.codehaus.groovy.ast.ASTNode
-import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.Expression
import org.codehaus.groovy.ast.expr.MapEntryExpression
@@ -39,7 +38,7 @@ import org.codehaus.groovy.macro.runtime.Macro
import org.codehaus.groovy.macro.runtime.MacroContext
import org.codehaus.groovy.syntax.SyntaxException
-import static org.codehaus.groovy.ast.tools.GeneralUtils.asX
+import static org.codehaus.groovy.ast.tools.GeneralUtils.callX
/**
* Declare GINQ macro methods
*
@@ -62,7 +61,7 @@ class GinqGroovyMethods {
}
/**
- * Represents the abbreviation of {@code GQ {...} as List}, which is very useful when used as list comprehension
+ * Represents the abbreviation of {@code GQ {...}.toList()}, which is very useful when used as list comprehension
*
* @param ctx the macro context
* @param ginqClosureExpression hold the GINQ code
@@ -71,7 +70,7 @@ class GinqGroovyMethods {
*/
@Macro
static Expression GQL(final MacroContext ctx, final ClosureExpression ginqClosureExpression) {
- asX(ClassHelper.LIST_TYPE, GQ(ctx, ginqClosureExpression))
+ callX(GQ(ctx, ginqClosureExpression), 'toList')
}
/**
@@ -88,6 +87,20 @@ class GinqGroovyMethods {
return transformGinqCode(ctx.sourceUnit, ginqConfigurationMapExpression, ginqClosureExpression.code)
}
+ /**
+ * Represents the abbreviation of {@code GQ {...}.toList()}, which is very useful when used as list comprehension
+ *
+ * @param ctx the macro context
+ * @param ginqConfigurationMapExpression specify the configuration for GINQ, e.g. {@code astWalker}, {@code optimize}, {@code parallel}
+ * @param ginqClosureExpression hold the GINQ code
+ * @return target method invocation
+ * @since 4.0.0
+ */
+ @Macro
+ static Expression GQL(final MacroContext ctx, final MapExpression ginqConfigurationMapExpression, final ClosureExpression ginqClosureExpression) {
+ callX(GQ(ctx, ginqConfigurationMapExpression, ginqClosureExpression), 'toList')
+ }
+
static Expression transformGinqCode(SourceUnit sourceUnit, MapExpression ginqConfigurationMapExpression, Statement code) {
GinqAstBuilder ginqAstBuilder = new GinqAstBuilder(sourceUnit)
diff --git a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
index dc386b8..9ed15c5 100644
--- a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
+++ b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
@@ -911,7 +911,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_tips_02,indent
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_tips_06,indent=0]
----
[NOTE]
-`GQL {...}` is the abbreviation of `GQ {...} as List`
+`GQL {...}` is the abbreviation of `GQ {...}.toList()`
GINQ could be used as list comprehension in the loops directly:
[source, groovy]
diff --git a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 8b11cdc..1577535 100644
--- a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++ b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -4150,6 +4150,33 @@ class GinqTest {
}
@Test
+ void "testGinq - GQL - 3"() {
+ assertGinqScript '''
+ def result = GQL(optimize: false) {from n in [1] select n}
+ assert result instanceof List
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - GQL - 4"() {
+ assertGinqScript '''
+ def result = GQL(parallel: true) {from n in [1] select n}
+ assert result instanceof List
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - GQL - 5"() {
+ assertGinqScript '''
+ def result = GQL(optimize: false, parallel: true) {from n in [1] select n}
+ assert result instanceof List
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
void "testGinq - asType - 1"() {
assertGinqScript '''
def result = GQ {from n in [1] select n} as Collection