Skip to content

Commit dbbc5c4

Browse files
committed
tpl/collections: Fix union when the first slice is empty
Fixes #3686
1 parent 7bcc1ce commit dbbc5c4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

‎tpl/collections/collections.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ type intersector struct {
494494
}
495495

496496
func (i *intersector) appendIfNotSeen(v reflect.Value) {
497+
497498
vi := v.Interface()
498499
if !i.seen[vi] {
499500
i.r = reflect.Append(i.r, v)
@@ -565,6 +566,14 @@ func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error) {
565566
}
566567
}
567568

569+
if !l1vv.IsValid() {
570+
// The first slice may be empty. Pick the first value of the second
571+
// to use as a prototype.
572+
if l2v.Len() > 0 {
573+
l1vv = l2v.Index(0)
574+
}
575+
}
576+
568577
for j := 0; j < l2v.Len(); j++ {
569578
l2vv := l2v.Index(j)
570579

‎tpl/collections/collections_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,13 @@ func TestUnion(t *testing.T) {
666666
{pagesVals{p1v}, pagesVals{p3v, p3v}, pagesVals{p1v, p3v}, false},
667667
{[]interface{}{p1, p4}, []interface{}{p4, p2, p2}, []interface{}{p1, p4, p2}, false},
668668
{[]interface{}{p1v}, []interface{}{p3v, p3v}, []interface{}{p1v, p3v}, false},
669+
// #3686
670+
{[]interface{}{p1v}, []interface{}{}, []interface{}{p1v}, false},
671+
{[]interface{}{}, []interface{}{p1v}, []interface{}{p1v}, false},
672+
{pagesPtr{p1}, pagesPtr{}, pagesPtr{p1}, false},
673+
{pagesVals{p1v}, pagesVals{}, pagesVals{p1v}, false},
674+
{pagesPtr{}, pagesPtr{p1}, pagesPtr{p1}, false},
675+
{pagesVals{}, pagesVals{p1v}, pagesVals{p1v}, false},
669676

670677
// errors
671678
{"not array or slice", []string{"a"}, false, true},

0 commit comments

Comments
 (0)