ref: dbbc5c4810a04ac06fad7500d88cf5c3bfe0c7fd
parent: 7bcc1ce659710f2220b400ce3b76e50d2e48b241
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Jul 8 06:31:09 EDT 2017
tpl/collections: Fix union when the first slice is empty Fixes #3686
--- a/tpl/collections/collections.go
+++ b/tpl/collections/collections.go
@@ -494,6 +494,7 @@
}
func (i *intersector) appendIfNotSeen(v reflect.Value) {
+
vi := v.Interface()
if !i.seen[vi] {
i.r = reflect.Append(i.r, v)
@@ -562,6 +563,14 @@
l1vv, isNil = indirectInterface(l1v.Index(i))
if !isNil {
ins.appendIfNotSeen(l1vv)
+ }
+ }
+
+ if !l1vv.IsValid() {
+ // The first slice may be empty. Pick the first value of the second
+ // to use as a prototype.
+ if l2v.Len() > 0 {
+ l1vv = l2v.Index(0)
}
}
--- a/tpl/collections/collections_test.go
+++ b/tpl/collections/collections_test.go
@@ -666,6 +666,13 @@
{pagesVals{p1v}, pagesVals{p3v, p3v}, pagesVals{p1v, p3v}, false},
{[]interface{}{p1, p4}, []interface{}{p4, p2, p2}, []interface{}{p1, p4, p2}, false},
{[]interface{}{p1v}, []interface{}{p3v, p3v}, []interface{}{p1v, p3v}, false},
+ // #3686
+ {[]interface{}{p1v}, []interface{}{}, []interface{}{p1v}, false},
+ {[]interface{}{}, []interface{}{p1v}, []interface{}{p1v}, false},
+ {pagesPtr{p1}, pagesPtr{}, pagesPtr{p1}, false},
+ {pagesVals{p1v}, pagesVals{}, pagesVals{p1v}, false},
+ {pagesPtr{}, pagesPtr{p1}, pagesPtr{p1}, false},
+ {pagesVals{}, pagesVals{p1v}, pagesVals{p1v}, false},
// errors
{"not array or slice", []string{"a"}, false, true},