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 2023/08/01 15:18:34 UTC
[groovy] branch GROOVY_4_0_X updated (703c7b68a5 -> 150503b9fa)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a change to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
discard 703c7b68a5 GROOVY-10893: `collectEntries`/`collectMany` null return inserts nothing
new 150503b9fa GROOVY-10893: `collectEntries`/`collectMany` null return inserts nothing
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (703c7b68a5)
\
N -- N -- N refs/heads/GROOVY_4_0_X (150503b9fa)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
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.
Summary of changes:
build.bat | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 build.bat
[groovy] 01/01: GROOVY-10893: `collectEntries`/`collectMany` null return inserts nothing
Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 150503b9fad5a3d5b69a3ee6613432232659d546
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Aug 1 10:18:24 2023 -0500
GROOVY-10893: `collectEntries`/`collectMany` null return inserts nothing
4_0_X backport
---
.../groovy/runtime/DefaultGroovyMethods.java | 19 +++++----
src/test/groovy/GroovyMethodsTest.groovy | 47 ++++++++++++++--------
2 files changed, 42 insertions(+), 24 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index fd79df8013..cf884c60fc 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -3768,7 +3768,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* <p>
* <pre class="groovyTestCase">
* def map = [bread:3, milk:5, butter:2]
- * def result = map.collectMany(['x']){ k, v {@code ->} k.startsWith('b') ? k.toList() : [] }
+ * def result = map.collectMany(['x']){ k, v {@code ->} if (k.startsWith('b')) k.toList() }
* assert result == ['x', 'b', 'r', 'e', 'a', 'd', 'b', 'u', 't', 't', 'e', 'r']
* </pre>
*
@@ -3780,7 +3780,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
*/
public static <T, K, V, C extends Collection<T>> C collectMany(Map<K, V> self, C collector, @ClosureParams(MapEntryOrKeyValue.class) Closure<? extends Collection<? extends T>> projection) {
for (Map.Entry<K, V> entry : self.entrySet()) {
- collector.addAll(callClosureForMapEntry(projection, entry));
+ Collection items = callClosureForMapEntry(projection, entry);
+ if (items != null && !items.isEmpty()) collector.addAll(items);
}
return collector;
}
@@ -3853,7 +3854,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* <p>
* <pre class="groovyTestCase">
* def numsIter = [1, 2, 3, 4, 5, 6].iterator()
- * def squaresAndCubesOfEvens = numsIter.collectMany{ it % 2 ? [] : [it**2, it**3] }
+ * def squaresAndCubesOfEvens = numsIter.collectMany{ if (it % 2 == 0) [it**2, it**3] }
* assert squaresAndCubesOfEvens == [4, 8, 16, 64, 36, 216]
* </pre>
*
@@ -3867,7 +3868,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
public static <T, E, C extends Collection<T>> C collectMany(Iterator<E> self, C collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends Collection<? extends T>> projection) {
while (self.hasNext()) {
E element = self.next();
- collector.addAll(projection.call(element));
+ Collection items= projection.call(element);
+ if (items != null) collector.addAll(items);
}
return collector;
}
@@ -4202,12 +4204,14 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
return collectEntries(self, Closure.IDENTITY);
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"rawtypes","unchecked"})
private static <K, V> void addEntry(Map<K, V> result, Object newEntry) {
- if (newEntry instanceof Map) {
+ if (newEntry == null) {
+ // GROOVY-10893: insert nothing
+ } else if (newEntry instanceof Map) {
leftShift(result, (Map)newEntry);
} else if (newEntry instanceof List) {
- List list = (List) newEntry;
+ List list = (List)newEntry;
// def (key, value) == list
Object key = list.isEmpty() ? null : list.get(0);
Object value = list.size() <= 1 ? null : list.get(1);
@@ -4219,7 +4223,6 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
Object value = array.length <= 1 ? null : array[1];
leftShift(result, new MapEntry(key, value));
} else {
- // TODO: enforce stricter behavior?
// given Map.Entry is an interface, we get a proxy which gives us lots
// of flexibility but sometimes the error messages might be unexpected
leftShift(result, asType(newEntry, Map.Entry.class));
diff --git a/src/test/groovy/GroovyMethodsTest.groovy b/src/test/groovy/GroovyMethodsTest.groovy
index 91803d7acc..418aabb2c8 100644
--- a/src/test/groovy/GroovyMethodsTest.groovy
+++ b/src/test/groovy/GroovyMethodsTest.groovy
@@ -1462,32 +1462,47 @@ class GroovyMethodsTest extends GroovyTestCase {
assert !iterable.containsAll([1, 6] as int[])
}
- void testCollectEntriesIterator() {
- def items = ['a', 'bb', 'ccc'].iterator()
- def map = items.collectEntries { [it, it.size()] }
- assert map == [a: 1, bb: 2, ccc: 3]
+ void testCollectEntriesArray() {
+ def arr = '1 San Francisco,2 Cupertino'.split(',')
+ def map = arr.collectEntries { it.split(' ', 2); }
+ assert map == ['1':'San Francisco', '2':'Cupertino']
}
void testCollectEntriesIterable() {
- def things = new Things()
- def map = things.collectEntries { [it.toLowerCase(), it.toUpperCase()] }
- assert map == [a: 'A', b: 'B', c: 'C']
+ def obj = new Things()
+ def map = obj.collectEntries { [it.toLowerCase(), it.toUpperCase()] }
+ assert map == [a:'A', b:'B', c:'C']
}
- void testCollectEntriesListFallbackCases() {
- assert [[[1,'a'], [2,'b'], [3]].collectEntries(),
- [[1,'a'], [2,'b'], []].collectEntries(),
- [[1,'a'], [2,'b'], [3, 'c', 42]].collectEntries()] == [[1:'a', 2:'b', 3:null], [1:'a', 2:'b', (null):null], [1:'a', 2:'b', 3:'c']]
+ void testCollectEntriesIterator() {
+ def itr = ['a','bb','ccc'].iterator()
+ def map = itr.collectEntries { [(it): it.size()] }
+ assert map == [a:1, bb:2, ccc:3]
+ }
+
+ void testCollectEntriesListReturn() {
+ def map = [[1,'a'], [2,'b'], [3]].collectEntries()
+ assert map == [1:'a', 2:'b', 3:null]
+
+ map = [[1,'a'], [2,'b'], []].collectEntries()
+ assert map == [1:'a', 2:'b', (null):null]
+
+ map = [[1,'a'], [2,'b'], [3, 'c', 42]].collectEntries()
+ assert map == [1:'a', 2:'b', 3:'c']
+
shouldFail(NullPointerException) {
[[1, 'a'], [3]].collectEntries(new Hashtable())
}
}
- void testCollectEntriesWithArray() {
- def cityList = '1 San Francisco,2 Cupertino'
- def cityMap = cityList.split(',').
- collectEntries{ it.split(' ', 2) }
- assert cityMap == ['1': 'San Francisco', '2': 'Cupertino']
+ // GROOVY-10893
+ void testCollectEntriesNullReturn() {
+ def map = ['a','b','c','d','e'].collectEntries {
+ if (it == 'a' || it == 'e') {
+ return [it, 'vowel']
+ }
+ }
+ assert map == [a:'vowel', e:'vowel']
}
void testListTakeWhile() {