mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
3e421bd47c
commit
d7a67dcb51
2 changed files with 42 additions and 48 deletions
|
@ -16,7 +16,6 @@
|
||||||
package collections
|
package collections
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
@ -30,6 +29,7 @@ import (
|
||||||
"github.com/gohugoio/hugo/common/types"
|
"github.com/gohugoio/hugo/common/types"
|
||||||
"github.com/gohugoio/hugo/deps"
|
"github.com/gohugoio/hugo/deps"
|
||||||
"github.com/gohugoio/hugo/helpers"
|
"github.com/gohugoio/hugo/helpers"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -655,40 +655,38 @@ func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error) {
|
||||||
|
|
||||||
// Uniq takes in a slice or array and returns a slice with subsequent
|
// Uniq takes in a slice or array and returns a slice with subsequent
|
||||||
// duplicate elements removed.
|
// duplicate elements removed.
|
||||||
func (ns *Namespace) Uniq(l interface{}) (interface{}, error) {
|
func (ns *Namespace) Uniq(seq interface{}) (interface{}, error) {
|
||||||
if l == nil {
|
if seq == nil {
|
||||||
return make([]interface{}, 0), nil
|
return make([]interface{}, 0), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
lv := reflect.ValueOf(l)
|
v := reflect.ValueOf(seq)
|
||||||
lv, isNil := indirect(lv)
|
var slice reflect.Value
|
||||||
if isNil {
|
|
||||||
return nil, errors.New("invalid nil argument to Uniq")
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret reflect.Value
|
switch v.Kind() {
|
||||||
|
|
||||||
switch lv.Kind() {
|
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
ret = reflect.MakeSlice(lv.Type(), 0, 0)
|
slice = reflect.MakeSlice(v.Type(), 0, 0)
|
||||||
case reflect.Array:
|
case reflect.Array:
|
||||||
ret = reflect.MakeSlice(reflect.SliceOf(lv.Type().Elem()), 0, 0)
|
slice = reflect.MakeSlice(reflect.SliceOf(v.Type().Elem()), 0, 0)
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("Can't use Uniq on " + reflect.ValueOf(lv).Type().String())
|
return nil, errors.Errorf("type %T not supported", seq)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i != lv.Len(); i++ {
|
seen := make(map[interface{}]bool)
|
||||||
lvv := lv.Index(i)
|
for i := 0; i < v.Len(); i++ {
|
||||||
lvv, isNil := indirect(lvv)
|
ev, _ := indirectInterface(v.Index(i))
|
||||||
if isNil {
|
if !ev.Type().Comparable() {
|
||||||
continue
|
return nil, errors.New("elements must be comparable")
|
||||||
|
}
|
||||||
|
key := normalize(ev)
|
||||||
|
if _, found := seen[key]; !found {
|
||||||
|
slice = reflect.Append(slice, ev)
|
||||||
|
seen[key] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ns.In(ret.Interface(), lvv.Interface()) {
|
return slice.Interface(), nil
|
||||||
ret = reflect.Append(ret, lvv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret.Interface(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyVals creates a key and values wrapper.
|
// KeyVals creates a key and values wrapper.
|
||||||
|
|
|
@ -322,11 +322,6 @@ func (p testPage) String() string {
|
||||||
type pagesPtr []*testPage
|
type pagesPtr []*testPage
|
||||||
type pagesVals []testPage
|
type pagesVals []testPage
|
||||||
|
|
||||||
func TestIntersect(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
ns := New(&deps.Deps{})
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
p1 = &testPage{"A"}
|
p1 = &testPage{"A"}
|
||||||
p2 = &testPage{"B"}
|
p2 = &testPage{"B"}
|
||||||
|
@ -339,6 +334,11 @@ func TestIntersect(t *testing.T) {
|
||||||
p4v = testPage{"D"}
|
p4v = testPage{"D"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestIntersect(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ns := New(&deps.Deps{})
|
||||||
|
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
l1, l2 interface{}
|
l1, l2 interface{}
|
||||||
expect interface{}
|
expect interface{}
|
||||||
|
@ -671,18 +671,6 @@ func TestUnion(t *testing.T) {
|
||||||
|
|
||||||
ns := New(&deps.Deps{})
|
ns := New(&deps.Deps{})
|
||||||
|
|
||||||
var (
|
|
||||||
p1 = &testPage{"A"}
|
|
||||||
p2 = &testPage{"B"}
|
|
||||||
// p3 = &page{"C"}
|
|
||||||
p4 = &testPage{"D"}
|
|
||||||
|
|
||||||
p1v = testPage{"A"}
|
|
||||||
//p2v = page{"B"}
|
|
||||||
p3v = testPage{"C"}
|
|
||||||
//p4v = page{"D"}
|
|
||||||
)
|
|
||||||
|
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
l1 interface{}
|
l1 interface{}
|
||||||
l2 interface{}
|
l2 interface{}
|
||||||
|
@ -786,7 +774,15 @@ func TestUniq(t *testing.T) {
|
||||||
{[]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
|
{[]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
|
||||||
{[4]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
|
{[4]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
|
||||||
{nil, make([]interface{}, 0), false},
|
{nil, make([]interface{}, 0), false},
|
||||||
// should-errors
|
// Pointers
|
||||||
|
{pagesPtr{p1, p2, p3, p2}, pagesPtr{p1, p2, p3}, false},
|
||||||
|
{pagesPtr{}, pagesPtr{}, false},
|
||||||
|
// Structs
|
||||||
|
{pagesVals{p3v, p2v, p3v, p2v}, pagesVals{p3v, p2v}, false},
|
||||||
|
|
||||||
|
// should fail
|
||||||
|
// uncomparable types
|
||||||
|
{[]map[string]int{{"K1": 1}}, []map[string]int{{"K2": 2}, {"K2": 2}}, true},
|
||||||
{1, 1, true},
|
{1, 1, true},
|
||||||
{"foo", "fo", true},
|
{"foo", "fo", true},
|
||||||
} {
|
} {
|
||||||
|
|
Loading…
Reference in a new issue