diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-03-17 22:03:27 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-03-17 22:03:27 +0100 |
commit | b80853de90b10171155b8f3fde47d64ec7bfa0dd (patch) | |
tree | 435d3dbf7a495a0c6ce64c9769e037179aa0d27b /tpl | |
parent | 423594e03a906ef4150f433666ff588b022c3c92 (diff) | |
download | hugo-b80853de90b10171155b8f3fde47d64ec7bfa0dd.tar.gz hugo-b80853de90b10171155b8f3fde47d64ec7bfa0dd.zip |
all: gofmt -w -r 'interface{} -> any' .
Updates #9687
Diffstat (limited to 'tpl')
105 files changed, 863 insertions, 863 deletions
diff --git a/tpl/cast/cast.go b/tpl/cast/cast.go index c864b5e32..000e315f1 100644 --- a/tpl/cast/cast.go +++ b/tpl/cast/cast.go @@ -30,23 +30,23 @@ type Namespace struct { } // ToInt converts the given value to an int. -func (ns *Namespace) ToInt(v interface{}) (int, error) { +func (ns *Namespace) ToInt(v any) (int, error) { v = convertTemplateToString(v) return _cast.ToIntE(v) } // ToString converts the given value to a string. -func (ns *Namespace) ToString(v interface{}) (string, error) { +func (ns *Namespace) ToString(v any) (string, error) { return _cast.ToStringE(v) } // ToFloat converts the given value to a float. -func (ns *Namespace) ToFloat(v interface{}) (float64, error) { +func (ns *Namespace) ToFloat(v any) (float64, error) { v = convertTemplateToString(v) return _cast.ToFloat64E(v) } -func convertTemplateToString(v interface{}) interface{} { +func convertTemplateToString(v any) any { switch vv := v.(type) { case template.HTML: v = string(vv) diff --git a/tpl/cast/cast_test.go b/tpl/cast/cast_test.go index c6219728b..5b4a36c3a 100644 --- a/tpl/cast/cast_test.go +++ b/tpl/cast/cast_test.go @@ -27,8 +27,8 @@ func TestToInt(t *testing.T) { ns := New() for i, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {"1", 1}, {template.HTML("2"), 2}, @@ -59,8 +59,8 @@ func TestToString(t *testing.T) { ns := New() for i, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {1, "1"}, {template.HTML("2"), "2"}, @@ -87,8 +87,8 @@ func TestToFloat(t *testing.T) { ns := New() for i, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {"1", 1.0}, {template.HTML("2"), 2.0}, diff --git a/tpl/cast/docshelper.go b/tpl/cast/docshelper.go index 9a5d55b3d..1c7b9c888 100644 --- a/tpl/cast/docshelper.go +++ b/tpl/cast/docshelper.go @@ -43,7 +43,7 @@ func init() { } - return docshelper.DocProvider{"tpl": map[string]interface{}{"funcs": namespaces}} + return docshelper.DocProvider{"tpl": map[string]any{"funcs": namespaces}} } docshelper.AddDocProviderFunc(docsProvider) diff --git a/tpl/cast/init.go b/tpl/cast/init.go index 079be4719..f1badf993 100644 --- a/tpl/cast/init.go +++ b/tpl/cast/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.ToInt, diff --git a/tpl/collections/append.go b/tpl/collections/append.go index 50fafe3d9..752ad2fa0 100644 --- a/tpl/collections/append.go +++ b/tpl/collections/append.go @@ -25,7 +25,7 @@ import ( // Note that with 2 arguments where both are slices of the same type, // the first slice will be appended to the second: // {{ $pages = $pages | append .Site.RegularPages }} -func (ns *Namespace) Append(args ...interface{}) (interface{}, error) { +func (ns *Namespace) Append(args ...any) (any, error) { if len(args) < 2 { return nil, errors.New("need at least 2 arguments to append") } diff --git a/tpl/collections/append_test.go b/tpl/collections/append_test.go index 9dcc7d2b9..02e46039f 100644 --- a/tpl/collections/append_test.go +++ b/tpl/collections/append_test.go @@ -29,20 +29,20 @@ func TestAppend(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - start interface{} - addend []interface{} - expected interface{} + start any + addend []any + expected any }{ - {[]string{"a", "b"}, []interface{}{"c"}, []string{"a", "b", "c"}}, - {[]string{"a", "b"}, []interface{}{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}}, - {[]string{"a", "b"}, []interface{}{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}}, + {[]string{"a", "b"}, []any{"c"}, []string{"a", "b", "c"}}, + {[]string{"a", "b"}, []any{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}}, + {[]string{"a", "b"}, []any{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}}, // Errors - {"", []interface{}{[]string{"a", "b"}}, false}, - {[]string{"a", "b"}, []interface{}{}, false}, + {"", []any{[]string{"a", "b"}}, false}, + {[]string{"a", "b"}, []any{}, false}, // No string concatenation. { "ab", - []interface{}{"c"}, + []any{"c"}, false, }, } { diff --git a/tpl/collections/apply.go b/tpl/collections/apply.go index 55bab5d1a..3b8de5f0e 100644 --- a/tpl/collections/apply.go +++ b/tpl/collections/apply.go @@ -25,9 +25,9 @@ import ( ) // Apply takes a map, array, or slice and returns a new slice with the function fname applied over it. -func (ns *Namespace) Apply(ctx context.Context, seq interface{}, fname string, args ...interface{}) (interface{}, error) { +func (ns *Namespace) Apply(ctx context.Context, seq any, fname string, args ...any) (any, error) { if seq == nil { - return make([]interface{}, 0), nil + return make([]any, 0), nil } if fname == "apply" { @@ -47,7 +47,7 @@ func (ns *Namespace) Apply(ctx context.Context, seq interface{}, fname string, a switch seqv.Kind() { case reflect.Array, reflect.Slice: - r := make([]interface{}, seqv.Len()) + r := make([]any, seqv.Len()) for i := 0; i < seqv.Len(); i++ { vv := seqv.Index(i) @@ -65,10 +65,10 @@ func (ns *Namespace) Apply(ctx context.Context, seq interface{}, fname string, a } } -func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...interface{}) (reflect.Value, error) { +func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (reflect.Value, error) { num := fn.Type().NumIn() if num > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) { - args = append([]interface{}{ctx}, args...) + args = append([]any{ctx}, args...) } n := make([]reflect.Value, len(args)) @@ -120,7 +120,7 @@ func (ns *Namespace) lookupFunc(fname string) (reflect.Value, bool) { return reflect.Value{}, false } - fn, ok := nv.Interface().(func(...interface{}) (interface{}, error)) + fn, ok := nv.Interface().(func(...any) (any, error)) if !ok { return reflect.Value{}, false } diff --git a/tpl/collections/apply_test.go b/tpl/collections/apply_test.go index 9ad0f405d..315f2bc85 100644 --- a/tpl/collections/apply_test.go +++ b/tpl/collections/apply_test.go @@ -48,11 +48,11 @@ func (templateFinder) LookupLayout(d output.LayoutDescriptor, f output.Format) ( return nil, false, nil } -func (templateFinder) Execute(t tpl.Template, wr io.Writer, data interface{}) error { +func (templateFinder) Execute(t tpl.Template, wr io.Writer, data any) error { return nil } -func (templateFinder) ExecuteWithContext(ctx context.Context, t tpl.Template, wr io.Writer, data interface{}) error { +func (templateFinder) ExecuteWithContext(ctx context.Context, t tpl.Template, wr io.Writer, data any) error { return nil } @@ -71,13 +71,13 @@ func TestApply(t *testing.T) { d.SetTmpl(new(templateFinder)) ns := New(d) - strings := []interface{}{"a\n", "b\n"} + strings := []any{"a\n", "b\n"} ctx := context.Background() result, err := ns.Apply(ctx, strings, "print", "a", "b", "c") c.Assert(err, qt.IsNil) - c.Assert(result, qt.DeepEquals, []interface{}{"abc", "abc"}) + c.Assert(result, qt.DeepEquals, []any{"abc", "abc"}) _, err = ns.Apply(ctx, strings, "apply", ".") c.Assert(err, qt.Not(qt.IsNil)) diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go index 0a9774aa4..24f43e62c 100644 --- a/tpl/collections/collections.go +++ b/tpl/collections/collections.go @@ -50,7 +50,7 @@ type Namespace struct { } // After returns all the items after the first N in a rangeable list. -func (ns *Namespace) After(index interface{}, seq interface{}) (interface{}, error) { +func (ns *Namespace) After(index any, seq any) (any, error) { if index == nil || seq == nil { return nil, errors.New("both limit and seq must be provided") } @@ -86,7 +86,7 @@ func (ns *Namespace) After(index interface{}, seq interface{}) (interface{}, err // Delimit takes a given sequence and returns a delimited HTML string. // If last is passed to the function, it will be used as the final delimiter. -func (ns *Namespace) Delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, error) { +func (ns *Namespace) Delimit(seq, delimiter any, last ...any) (template.HTML, error) { d, err := cast.ToStringE(delimiter) if err != nil { return "", err @@ -146,12 +146,12 @@ func (ns *Namespace) Delimit(seq, delimiter interface{}, last ...interface{}) (t // walking the parameters and treating them as key-value pairs. The number // of parameters must be even. // The keys can be string slices, which will create the needed nested structure. -func (ns *Namespace) Dictionary(values ...interface{}) (map[string]interface{}, error) { +func (ns *Namespace) Dictionary(values ...any) (map[string]any, error) { if len(values)%2 != 0 { return nil, errors.New("invalid dictionary call") } - root := make(map[string]interface{}) + root := make(map[string]any) for i := 0; i < len(values); i += 2 { dict := root @@ -162,12 +162,12 @@ func (ns *Namespace) Dictionary(values ...interface{}) (map[string]interface{}, case []string: for i := 0; i < len(v)-1; i++ { key = v[i] - var m map[string]interface{} + var m map[string]any v, found := dict[key] if found { - m = v.(map[string]interface{}) + m = v.(map[string]any) } else { - m = make(map[string]interface{}) + m = make(map[string]any) dict[key] = m } dict = m @@ -184,7 +184,7 @@ func (ns *Namespace) Dictionary(values ...interface{}) (map[string]interface{}, // EchoParam returns a given value if it is set; otherwise, it returns an // empty string. -func (ns *Namespace) EchoParam(a, key interface{}) interface{} { +func (ns *Namespace) EchoParam(a, key any) any { av, isNil := indirect(reflect.ValueOf(a)) if isNil { return "" @@ -227,7 +227,7 @@ func (ns *Namespace) EchoParam(a, key interface{}) interface{} { } // First returns the first N items in a rangeable list. -func (ns *Namespace) First(limit interface{}, seq interface{}) (interface{}, error) { +func (ns *Namespace) First(limit any, seq any) (any, error) { if limit == nil || seq == nil { return nil, errors.New("both limit and seq must be provided") } @@ -262,7 +262,7 @@ func (ns *Namespace) First(limit interface{}, seq interface{}) (interface{}, err } // In returns whether v is in the set l. l may be an array or slice. -func (ns *Namespace) In(l interface{}, v interface{}) (bool, error) { +func (ns *Namespace) In(l any, v any) (bool, error) { if l == nil || v == nil { return false, nil } @@ -301,9 +301,9 @@ func (ns *Namespace) In(l interface{}, v interface{}) (bool, error) { // Intersect returns the common elements in the given sets, l1 and l2. l1 and // l2 must be of the same type and may be either arrays or slices. -func (ns *Namespace) Intersect(l1, l2 interface{}) (interface{}, error) { +func (ns *Namespace) Intersect(l1, l2 any) (any, error) { if l1 == nil || l2 == nil { - return make([]interface{}, 0), nil + return make([]any, 0), nil } var ins *intersector @@ -313,19 +313,19 @@ func (ns *Namespace) Intersect(l1, l2 interface{}) (interface{}, error) { switch l1v.Kind() { case reflect.Array, reflect.Slice: - ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[interface{}]bool)} + ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[any]bool)} switch l2v.Kind() { case reflect.Array, reflect.Slice: for i := 0; i < l1v.Len(); i++ { l1vv := l1v.Index(i) if !l1vv.Type().Comparable() { - return make([]interface{}, 0), errors.New("intersect does not support slices or arrays of uncomparable types") + return make([]any, 0), errors.New("intersect does not support slices or arrays of uncomparable types") } for j := 0; j < l2v.Len(); j++ { l2vv := l2v.Index(j) if !l2vv.Type().Comparable() { - return make([]interface{}, 0), errors.New("intersect does not support slices or arrays of uncomparable types") + return make([]any, 0), errors.New("intersect does not support slices or arrays of uncomparable types") } ins.handleValuePair(l1vv, l2vv) @@ -342,7 +342,7 @@ func (ns *Namespace) Intersect(l1, l2 interface{}) (interface{}, error) { // Group groups a set of elements by the given key. // This is currently only supported for Pages. -func (ns *Namespace) Group(key interface{}, items interface{}) (interface{}, error) { +func (ns *Namespace) Group(key any, items any) (any, error) { if key == nil { return nil, errors.New("nil is not a valid key to group by") } @@ -362,7 +362,7 @@ func (ns *Namespace) Group(key interface{}, items interface{}) (interface{}, err // IsSet returns whether a given array, channel, slice, or map has a key // defined. -func (ns *Namespace) IsSet(a interface{}, key interface{}) (bool, error) { +func (ns *Namespace) IsSet(a any, key any) (bool, error) { av := reflect.ValueOf(a) kv := reflect.ValueOf(key) @@ -387,7 +387,7 @@ func (ns *Namespace) IsSet(a interface{}, key interface{}) (bool, error) { } // Last returns the last N items in a rangeable list. -func (ns *Namespace) Last(limit interface{}, seq interface{}) (interface{}, error) { +func (ns *Namespace) Last(limit any, seq any) (any, error) { if limit == nil || seq == nil { return nil, errors.New("both limit and seq must be provided") } @@ -422,7 +422,7 @@ func (ns *Namespace) Last(limit interface{}, seq interface{}) (interface{}, erro } // Querify encodes the given parameters in URL-encoded form ("bar=baz&foo=quux") sorted by key. -func (ns *Namespace) Querify(params ...interface{}) (string, error) { +func (ns *Namespace) Querify(params ...any) (string, error) { qs := url.Values{} if len(params) == 1 { @@ -438,7 +438,7 @@ func (ns *Namespace) Querify(params ...interface{}) (string, error) { return qs.Encode(), nil - case []interface{}: + case []any: params = v default: @@ -463,7 +463,7 @@ func (ns *Namespace) Querify(params ...interface{}) (string, error) { } // Reverse creates a copy of slice and reverses it. -func (ns *Namespace) Reverse(slice interface{}) (interface{}, error) { +func (ns *Namespace) Reverse(slice any) (any, error) { if slice == nil { return nil, nil } @@ -493,7 +493,7 @@ func (ns *Namespace) Reverse(slice interface{}) (interface{}, error) { // -3 => -1, -2, -3 // 1 4 => 1, 2, 3, 4 // 1 -2 => 1, 0, -1, -2 -func (ns *Namespace) Seq(args ...interface{}) ([]int, error) { +func (ns *Namespace) Seq(args ...any) ([]int, error) { if len(args) < 1 || len(args) > 3 { return nil, errors.New("invalid number of arguments to Seq") } @@ -561,7 +561,7 @@ func (ns *Namespace) Seq(args ...interface{}) ([]int, error) { } // Shuffle returns the given rangeable list in a randomised order. -func (ns *Namespace) Shuffle(seq interface{}) (interface{}, error) { +func (ns *Namespace) Shuffle(seq any) (any, error) { if seq == nil { return nil, errors.New("both count and seq must be provided") } @@ -591,7 +591,7 @@ func (ns *Namespace) Shuffle(seq interface{}) (interface{}, error) { } // Slice returns a slice of all passed arguments. -func (ns *Namespace) Slice(args ...interface{}) interface{} { +func (ns *Namespace) Slice(args ...any) any { if len(args) == 0 { return args } @@ -601,7 +601,7 @@ func (ns *Namespace) Slice(args ...interface{}) interface{} { type intersector struct { r reflect.Value - seen map[interface{}]bool + seen map[any]bool } func (i *intersector) appendIfNotSeen(v reflect.Value) { @@ -638,9 +638,9 @@ func (i *intersector) handleValuePair(l1vv, l2vv reflect.Value) { // l2 must be of the same type and may be either arrays or slices. // If l1 and l2 aren't of the same type then l1 will be returned. // If either l1 or l2 is nil then the non-nil list will be returned. -func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error) { +func (ns *Namespace) Union(l1, l2 any) (any, error) { if l1 == nil && l2 == nil { - return []interface{}{}, nil + return []any{}, nil } else if l1 == nil && l2 != nil { return l2, nil } else if l1 != nil && l2 == nil { @@ -656,7 +656,7 @@ func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error) { case reflect.Array, reflect.Slice: switch l2v.Kind() { case reflect.Array, reflect.Slice: - ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[interface{}]bool)} + ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[any]bool)} if l1v.Type() != l2v.Type() && l1v.Type().Elem().Kind() != reflect.Interface && @@ -673,7 +673,7 @@ func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error) { l1vv, isNil = indirectInterface(l1v.Index(i)) if !l1vv.Type().Comparable() { - return []interface{}{}, errors.New("union does not support slices or arrays of uncomparable types") + return []any{}, errors.New("union does not support slices or arrays of uncomparable types") } if !isNil { @@ -721,9 +721,9 @@ func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error) { // Uniq takes in a slice or array and returns a slice with subsequent // duplicate elements removed. -func (ns *Namespace) Uniq(seq interface{}) (interface{}, error) { +func (ns *Namespace) Uniq(seq any) (any, error) { if seq == nil { - return make([]interface{}, 0), nil + return make([]any, 0), nil } v := reflect.ValueOf(seq) @@ -739,7 +739,7 @@ func (ns *Namespace) Uniq(seq interface{}) (interface{}, error) { return nil, errors.Errorf("type %T not supported", seq) } - seen := make(map[interface{}]bool) + seen := make(map[any]bool) for i := 0; i < v.Len(); i++ { ev, _ := indirectInterface(v.Index(i)) @@ -756,7 +756,7 @@ func (ns *Namespace) Uniq(seq interface{}) (interface{}, error) { } // KeyVals creates a key and values wrapper. -func (ns *Namespace) KeyVals(key interface{}, vals ...interface{}) (types.KeyValues, error) { +func (ns *Namespace) KeyVals(key any, vals ...any) (types.KeyValues, error) { return types.KeyValues{Key: key, Values: vals}, nil } diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go index 8cced6fe5..3aadfae3b 100644 --- a/tpl/collections/collections_test.go +++ b/tpl/collections/collections_test.go @@ -43,9 +43,9 @@ func TestAfter(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - index interface{} - seq interface{} - expect interface{} + index any + seq any + expect any }{ {int(2), []string{"a", "b", "c", "d"}, []string{"c", "d"}}, {int32(3), []string{"a", "b"}, []string{}}, @@ -81,7 +81,7 @@ type tstGrouper struct { type tstGroupers []*tstGrouper -func (g tstGrouper) Group(key interface{}, items interface{}) (interface{}, error) { +func (g tstGrouper) Group(key any, items any) (any, error) { ilen := reflect.ValueOf(items).Len() return fmt.Sprintf("%v(%d)", key, ilen), nil } @@ -89,7 +89,7 @@ func (g tstGrouper) Group(key interface{}, items interface{}) (interface{}, erro type tstGrouper2 struct { } -func (g *tstGrouper2) Group(key interface{}, items interface{}) (interface{}, error) { +func (g *tstGrouper2) Group(key any, items any) (any, error) { ilen := reflect.ValueOf(items).Len() return fmt.Sprintf("%v(%d)", key, ilen), nil } @@ -100,9 +100,9 @@ func TestGroup(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - key interface{} - items interface{} - expect interface{} + key any + items any + expect any }{ {"a", []*tstGrouper{{}, {}}, "a(2)"}, {"b", tstGroupers{&tstGrouper{}, &tstGrouper{}}, "b(2)"}, @@ -136,9 +136,9 @@ func TestDelimit(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - seq interface{} - delimiter interface{} - last interface{} + seq any + delimiter any + last any expect template.HTML }{ {[]string{"class1", "class2", "class3"}, " ", nil, "class1 class2 class3"}, @@ -188,19 +188,19 @@ func TestDictionary(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - values []interface{} - expect interface{} + values []any + expect any }{ - {[]interface{}{"a", "b"}, map[string]interface{}{"a": "b"}}, - {[]interface{}{[]string{"a", "b"}, "c"}, map[string]interface{}{"a": map[string]interface{}{"b": "c"}}}, + {[]any{"a", "b"}, map[string]any{"a": "b"}}, + {[]any{[]string{"a", "b"}, "c"}, map[string]any{"a": map[string]any{"b": "c"}}}, { - []interface{}{[]string{"a", "b"}, "c", []string{"a", "b2"}, "c2", "b", "c"}, - map[string]interface{}{"a": map[string]interface{}{"b": "c", "b2": "c2"}, "b": "c"}, + []any{[]string{"a", "b"}, "c", []string{"a", "b2"}, "c2", "b", "c"}, + map[string]any{"a": map[string]any{"b": "c", "b2": "c2"}, "b": "c"}, }, - {[]interface{}{"a", 12, "b", []int{4}}, map[string]interface{}{"a": 12, "b": []int{4}}}, + {[]any{"a", 12, "b", []int{4}}, map[string]any{"a": 12, "b": []int{4}}}, // errors - {[]interface{}{5, "b"}, false}, - {[]interface{}{"a", "b", "c"}, false}, + {[]any{5, "b"}, false}, + {[]any{"a", "b", "c"}, false}, } { i := i test := test @@ -246,9 +246,9 @@ func TestEchoParam(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - a interface{} - key interface{} - expect interface{} + a any + key any + expect any }{ {[]int{1, 2, 3}, 1, int64(2)}, {[]uint{1, 2, 3}, 1, uint64(2)}, @@ -260,7 +260,7 @@ func TestEchoParam(t *testing.T) { {map[string]float64{"foo": 1.1, "bar": 2.2, "baz": 3.3}, "bar", float64(2.2)}, {map[string]string{"foo": "FOO", "bar": "BAR", "baz": "BAZ"}, "bar", "BAR"}, {map[string]TstX{"foo": {A: "a", B: "b"}, "bar": {A: "c", B: "d"}, "baz": {A: "e", B: "f"}}, "bar", ""}, - {map[string]interface{}{"foo": nil}, "foo", ""}, + {map[string]any{"foo": nil}, "foo", ""}, {(*[]string)(nil), "bar", ""}, } { errMsg := qt.Commentf("[%d] %v", i, test) @@ -278,9 +278,9 @@ func TestFirst(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - limit interface{} - seq interface{} - expect interface{} + limit any + seq any + expect any }{ {int(2), []string{"a", "b", "c"}, []string{"a", "b"}}, {int32(3), []string{"a", "b"}, []string{"a", "b"}}, @@ -316,20 +316,20 @@ func TestIn(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - l1 interface{} - l2 interface{} + l1 any + l2 any expect bool }{ {[]string{"a", "b", "c"}, "b", true}, - {[]interface{}{"a", "b", "c"}, "b", true}, - {[]interface{}{"a", "b", "c"}, "d", false}, + {[]any{"a", "b", "c"}, "b", true}, + {[]any{"a", "b", "c"}, "d", false}, {[]string{"a", "b", "c"}, "d", false}, {[]string{"a", "12", "c"}, 12, false}, {[]string{"a", "b", "c"}, nil, false}, {[]int{1, 2, 4}, 2, true}, - {[]interface{}{1, 2, 4}, 2, true}, - {[]interface{}{1, 2, 4}, nil, false}, - {[]interface{}{nil}, nil, false}, + {[]any{1, 2, 4}, 2, true}, + {[]any{1, 2, 4}, nil, false}, + {[]any{nil}, nil, false}, {[]int{1, 2, 4}, 3, false}, {[]float64{1.23, 2.45, 4.67}, 1.23, true}, {[]float64{1.234567, 2.45, 4.67}, 1.234568, false}, @@ -392,16 +392,16 @@ func TestIntersect(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - l1, l2 interface{} - expect interface{} + l1, l2 any + expect any }{ {[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b"}}, {[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b"}}, {[]string{"a", "b", "c"}, []string{"d", "e"}, []string{}}, {[]string{}, []string{}, []string{}}, - {[]string{"a", "b"}, nil, []interface{}{}}, - {nil, []string{"a", "b"}, []interface{}{}}, - {nil, nil, []interface{}{}}, + {[]string{"a", "b"}, nil, []any{}}, + {nil, []string{"a", "b"}, []any{}}, + {nil, nil, []any{}}, {[]string{"1", "2"}, []int{1, 2}, []string{}}, {[]int{1, 2}, []string{"1", "2"}, []int{}}, {[]int{1, 2, 4}, []int{2, 4}, []int{2, 4}}, @@ -410,45 +410,45 @@ func TestIntersect(t *testing.T) { {[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4}}, // []interface{} ∩ []interface{} - {[]interface{}{"a", "b", "c"}, []interface{}{"a", "b", "b"}, []interface{}{"a", "b"}}, - {[]interface{}{1, 2, 3}, []interface{}{1, 2, 2}, []interface{}{1, 2}}, - {[]interface{}{int8(1), int8(2), int8(3)}, []interface{}{int8(1), int8(2), int8(2)}, []interface{}{int8(1), int8(2)}}, - {[]interface{}{int16(1), int16(2), int16(3)}, []interface{}{int16(1), int16(2), int16(2)}, []interface{}{int16(1), int16(2)}}, - {[]interface{}{int32(1), int32(2), int32(3)}, []interface{}{int32(1), int32(2), int32(2)}, []interface{}{int32(1), int32(2)}}, - {[]interface{}{int64(1), int64(2), int64(3)}, []interface{}{int64(1), int64(2), int64(2)}, []interface{}{int64(1), int64(2)}}, - {[]interface{}{float32(1), float32(2), float32(3)}, []interface{}{float32(1), float32(2), float32(2)}, []interface{}{float32(1), float32(2)}}, - {[]interface{}{float64(1), float64(2), float64(3)}, []interface{}{float64(1), float64(2), float64(2)}, []interface{}{float64(1), float64(2)}}, + {[]any{"a", "b", "c"}, []any{"a", "b", "b"}, []any{"a", "b"}}, + {[]any{1, 2, 3}, []any{1, 2, 2}, []any{1, 2}}, + {[]any{int8(1), int8(2), int8(3)}, []any{int8(1), int8(2), int8(2)}, []any{int8(1), int8(2)}}, + {[]any{int16(1), int16(2), int16(3)}, []any{int16(1), int16(2), int16(2)}, []any{int16(1), int16(2)}}, + {[]any{int32(1), int32(2), int32(3)}, []any{int32(1), int32(2), int32(2)}, []any{int32(1), int32(2)}}, + {[]any{int64(1), int64(2), int64(3)}, []any{int64(1), int64(2), int64(2)}, []any{int64(1), int64(2)}}, + {[]any{float32(1), float32(2), float32(3)}, []any{float32(1), float32(2), float32(2)}, []any{float32(1), float32(2)}}, + {[]any{float64(1), float64(2), float64(3)}, []any{float64(1), float64(2), float64(2)}, []any{float64(1), float64(2)}}, // []interface{} ∩ []T - {[]interface{}{"a", "b", "c"}, []string{"a", "b", "b"}, []interface{}{"a", "b"}}, - {[]interface{}{1, 2, 3}, []int{1, 2, 2}, []interface{}{1, 2}}, - {[]interface{}{int8(1), int8(2), int8(3)}, []int8{1, 2, 2}, []interface{}{int8(1), int8(2)}}, - {[]interface{}{int16(1), int16(2), int16(3)}, []int16{1, 2, 2}, []interface{}{int16(1), int16(2)}}, - {[]interface{}{int32(1), int32(2), int32(3)}, []int32{1, 2, 2}, []interface{}{int32(1), int32(2)}}, - {[]interface{}{int64(1), int64(2), int64(3)}, []int64{1, 2, 2}, []interface{}{int64(1), int64(2)}}, - {[]interface{}{uint(1), uint(2), uint(3)}, []uint{1, 2, 2}, []interface{}{uint(1), uint(2)}}, - {[]interface{}{float32(1), float32(2), float32(3)}, []float32{1, 2, 2}, []interface{}{float32(1), float32(2)}}, - {[]interface{}{float64(1), float64(2), float64(3)}, []float64{1, 2, 2}, []interface{}{float64(1), float64(2)}}, + {[]any{"a", "b", "c"}, []string{"a", "b", "b"}, []any{"a", "b"}}, + {[]any{1, 2, 3}, []int{1, 2, 2}, []any{1, 2}}, + {[]any{int8(1), int8(2), int8(3)}, []int8{1, 2, 2}, []any{int8(1), int8(2)}}, + {[]any{int16(1), int16(2), int16(3)}, []int16{1, 2, 2}, []any{int16(1), int16(2)}}, + {[]any{int32(1), int32(2), int32(3)}, []int32{1, 2, 2}, []any{int32(1), int32(2)}}, + {[]any{int64(1), int64(2), int64(3)}, []int64{1, 2, 2}, []any{int64(1), int64(2)}}, + {[]any{uint(1), uint(2), uint(3)}, []uint{1, 2, 2}, []any{uint(1), uint(2)}}, + {[]any{float32(1), float32(2), float32(3)}, []float32{1, 2, 2}, []any{float32(1), float32(2)}}, + {[]any{float64(1), float64(2), float64(3)}, []float64{1, 2, 2}, []any{float64(1), float64(2)}}, // []T ∩ []interface{} - {[]string{"a", "b", "c"}, []interface{}{"a", "b", "b"}, []string{"a", "b"}}, - {[]int{1, 2, 3}, []interface{}{1, 2, 2}, []int{1, 2}}, - {[]int8{1, 2, 3}, []interface{}{int8(1), int8(2), int8(2)}, []int8{1, 2}}, - {[]int16{1, 2, 3}, []interface{}{int16(1), int16(2), int16(2)}, []int16{1, 2}}, - {[]int32{1, 2, 3}, []interface{}{int32(1), int32(2), int32(2)}, []int32{1, 2}}, - {[]int64{1, 2, 3}, []interface{}{int64(1), int64(2), int64(2)}, []int64{1, 2}}, - {[]float32{1, 2, 3}, []interface{}{float32(1), float32(2), float32(2)}, []float32{1, 2}}, - {[]float64{1, 2, 3}, []interface{}{float64(1), float64(2), float64(2)}, []float64{1, 2}}, + {[]string{"a", "b", "c"}, []any{"a", "b", "b"}, []string{"a", "b"}}, + {[]int{1, 2, 3}, []any{1, 2, 2}, []int{1, 2}}, + {[]int8{1, 2, 3}, []any{int8(1), int8(2), int8(2)}, []int8{1, 2}}, + {[]int16{1, 2, 3}, []any{int16(1), int16(2), int16(2)}, []int16{1, 2}}, + {[]int32{1, 2, 3}, []any{int32(1), int32(2), int32(2)}, []int32{1, 2}}, + {[]int64{1, 2, 3}, []any{int64(1), int64(2), int64(2)}, []int64{1, 2}}, + {[]float32{1, 2, 3}, []any{float32(1), float32(2), float32(2)}, []float32{1, 2}}, + {[]float64{1, 2, 3}, []any{float64(1), float64(2), float64(2)}, []float64{1, 2}}, // Structs {pagesPtr{p1, p4, p2, p3}, pagesPtr{p4, p2, p2}, pagesPtr{p4, p2}}, {pagesVals{p1v, p4v, p2v, p3v}, pagesVals{p1v, p3v, p3v}, pagesVals{p1v, p3v}}, - {[]interface{}{p1, p4, p2, p3}, []interface{}{p4, p2, p2}, []interface{}{p4, p2}}, - {[]interface{}{p1v, p4v, p2v, p3v}, []interface{}{p1v, p3v, p3v}, []interface{}{p1v, p3v}}, + {[]any{p1, p4, p2, p3}, []any{p4, p2, p2}, []any{p4, p2}}, + {[]any{p1v, p4v, p2v, p3v}, []any{p1v, p3v, p3v}, []any{p1v, p3v}}, {pagesPtr{p1, p4, p2, p3}, pagesPtr{}, pagesPtr{}}, {pagesVals{}, pagesVals{p1v, p3v, p3v}, pagesVals{}}, - {[]interface{}{p1, p4, p2, p3}, []interface{}{}, []interface{}{}}, - {[]interface{}{}, []interface{}{p1v, p3v, p3v}, []interface{}{}}, + {[]any{p1, p4, p2, p3}, []any{}, []any{}}, + {[]any{}, []any{p1v, p3v, p3v}, []any{}}, // errors {"not array or slice", []string{"a"}, false}, @@ -482,23 +482,23 @@ func TestIsSet(t *testing.T) { ns := newTestNs() for i, test := range []struct { - a interface{} - key interface{} + a any + key any expect bool isErr bool }{ - {[]interface{}{1, 2, 3, 5}, 2, true, false}, - {[]interface{}{1, 2, 3, 5}, "2", true, false}, - {[]interface{}{1, 2, 3, 5}, 2.0, true, false}, + {[]any{1, 2, 3, 5}, 2, true, false}, + {[]any{1, 2, 3, 5}, "2", true, false}, + {[]any{1, 2, 3, 5}, 2.0, true, false}, - {[]interface{}{1, 2, 3, 5}, 22, false, false}, + {[]any{1, 2, 3, 5}, 22, false, false}, - {map[string]interface{}{"a": 1, "b": 2}, "b", true, false}, - {map[string]interface{}{"a": 1, "b": 2}, "bc", false, false}, + {map[string]any{"a": 1, "b": 2}, "b", true, false}, + {map[string]any{"a": 1, "b": 2}, "bc", false, false}, {time.Now(), "Day", false, false}, {nil, "nil", false, false}, - {[]interface{}{1, 2, 3, 5}, TstX{}, false, true}, + {[]any{1, 2, 3, 5}, TstX{}, false, true}, } { errMsg := qt.Commentf("[%d] %v", i, test) @@ -519,9 +519,9 @@ func TestLast(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - limit interface{} - seq interface{} - expect interface{} + limit any + seq any + expect any }{ {int(2), []string{"a", "b", "c"}, []string{"b", "c"}}, {int32(3), []string{"a", "b"}, []string{"a", "b"}}, @@ -558,21 +558,21 @@ func TestQuerify(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - params []interface{} - expect interface{} + params []any + expect any }{ - {[]interface{}{"a", "b"}, "a=b"}, - {[]interface{}{"a", "b", "c", "d", "f", " &"}, `a=b&c=d&f=+%26`}, - {[]interface{}{[]string{"a", "b"}}, "a=b"}, - {[]interface{}{[]string{"a", "b", "c", "d", "f", " &"}}, `a=b&c=d&f=+%26`}, - {[]interface{}{[]interface{}{"x", "y"}}, `x=y`}, - {[]interface{}{[]interface{}{"x", 5}}, `x=5`}, + {[]any{"a", "b"}, "a=b"}, + {[]any{"a", "b", "c", "d", "f", " &"}, `a=b&c=d&f=+%26`}, + {[]any{[]string{"a", "b"}}, "a=b"}, + {[]any{[]string{"a", "b", "c", "d", "f", " &"}}, `a=b&c=d&f=+%26`}, + {[]any{[]any{"x", "y"}}, `x=y`}, + {[]any{[]any{"x", 5}}, `x=5`}, // errors - {[]interface{}{5, "b"}, false}, - {[]interface{}{"a", "b", "c"}, false}, - {[]interface{}{[]string{"a", "b", "c"}}, false}, - {[]interface{}{[]string{"a", "b"}, "c"}, false}, - {[]interface{}{[]interface{}{"c", "d", "e"}}, false}, + {[]any{5, "b"}, false}, + {[]any{"a", "b", "c"}, false}, + {[]any{[]string{"a", "b", "c"}}, false}, + {[]any{[]string{"a", "b"}, "c"}, false}, + {[]any{[]any{"c", "d", "e"}}, false}, } { errMsg := qt.Commentf("[%d] %v", i, test.params) @@ -590,7 +590,7 @@ func TestQuerify(t *testing.T) { func BenchmarkQuerify(b *testing.B) { ns := New(&deps.Deps{}) - params := []interface{}{"a", "b", "c", "d", "f", " &"} + params := []any{"a", "b", "c", "d", "f", " &"} b.ResetTimer() for i := 0; i < b.N; i++ { @@ -620,28 +620,28 @@ func TestSeq(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - args []interface{} - expect interface{} + args []any + expect any }{ - {[]interface{}{-2, 5}, []int{-2, -1, 0, 1, 2, 3, 4, 5}}, - {[]interface{}{1, 2, 4}, []int{1, 3}}, - {[]interface{}{1}, []int{1}}, - {[]interface{}{3}, []int{1, 2, 3}}, - {[]interface{}{3.2}, []int{1, 2, 3}}, - {[]interface{}{0}, []int{}}, - {[]interface{}{-1}, []int{-1}}, - {[]interface{}{-3}, []int{-1, -2, -3}}, - {[]interface{}{3, -2}, []int{3, 2, 1, 0, -1, -2}}, - {[]interface{}{6, -2, 2}, []int{6, 4, 2}}, + {[]any{-2, 5}, []int{-2, -1, 0, 1, 2, 3, 4, 5}}, + {[]any{1, 2, 4}, []int{1, 3}}, + {[]any{1}, []int{1}}, + {[]any{3}, []int{1, 2, 3}}, + {[]any{3.2}, []int{1, 2, 3}}, + {[]any{0}, []int{}}, + {[]any{-1}, []int{-1}}, + {[]any{-3}, []int{-1, -2, -3}}, + {[]any{3, -2}, []int{3, 2, 1, 0, -1, -2}}, + {[]any{6, -2, 2}, []int{6, 4, 2}}, // errors - {[]interface{}{1, 0, 2}, false}, - {[]interface{}{1, -1, 2}, false}, - {[]interface{}{2, 1, 1}, false}, - {[]interface{}{2, 1, 1, 1}, false}, - {[]interface{}{2001}, false}, - {[]interface{}{}, false}, - {[]interface{}{0, -1000000}, false}, - {[]interface{}{tstNoStringer{}}, false}, + {[]any{1, 0, 2}, false}, + {[]any{1, -1, 2}, false}, + {[]any{2, 1, 1}, false}, + {[]any{2, 1, 1, 1}, false}, + {[]any{2001}, false}, + {[]any{}, false}, + {[]any{0, -1000000}, false}, + {[]any{tstNoStringer{}}, false}, {nil, false}, } { errMsg := qt.Commentf("[%d] %v", i, test) @@ -664,7 +664,7 @@ func TestShuffle(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - seq interface{} + seq any success bool }{ {[]string{"a", "b", "c", "d"}, true}, @@ -735,14 +735,14 @@ func TestSlice(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - args []interface{} - expected interface{} + args []any + expected any }{ - {[]interface{}{"a", "b"}, []string{"a", "b"}}, - {[]interface{}{}, []interface{}{}}, - {[]interface{}{nil}, []interface{}{nil}}, - {[]interface{}{5, "b"}, []interface{}{5, "b"}}, - {[]interface{}{tstNoStringer{}}, []tstNoStringer{{}}}, + {[]any{"a", "b"}, []string{"a", "b"}}, + {[]any{}, []any{}}, + {[]any{nil}, []any{nil}}, + {[]any{5, "b"}, []any{5, "b"}}, + {[]any{tstNoStringer{}}, []tstNoStringer{{}}}, } { errMsg := qt.Commentf("[%d] %v", i, test.args) @@ -759,12 +759,12 @@ func TestUnion(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - l1 interface{} - l2 interface{} - expect interface{} + l1 any + l2 any + expect any isErr bool }{ - {nil, nil, []interface{}{}, false}, + {nil, nil, []any{}, false}, {nil, []string{"a", "b"}, []string{"a", "b"}, false}, {[]string{"a", "b"}, nil, []string{"a", "b"}, false}, @@ -783,36 +783,36 @@ func TestUnion(t *testing.T) { {[]int{2, 4}, []int{1, 2, 4}, []int{2, 4, 1}, false}, {[]int{1, 2, 4}, []int{3, 6}, []int{1, 2, 4, 3, 6}, false}, {[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false}, - {[]interface{}{"a", "b", "c", "c"}, []interface{}{"a", "b", "b"}, []interface{}{"a", "b", "c"}, false}, + {[]any{"a", "b", "c", "c"}, []any{"a", "b", "b"}, []any{"a", "b", "c"}, false}, // []T ∪ []interface{} - {[]string{"1", "2"}, []interface{}{"9"}, []string{"1", "2", "9"}, false}, - {[]int{2, 4}, []interface{}{1, 2, 4}, []int{2, 4, 1}, false}, - {[]int8{2, 4}, []interface{}{int8(1), int8(2), int8(4)}, []int8{2, 4, 1}, false}, - {[]int8{2, 4}, []interface{}{1, 2, 4}, []int8{2, 4, 1}, false}, - {[]int16{2, 4}, []interface{}{1, 2, 4}, []int16{2, 4, 1}, false}, - {[]int32{2, 4}, []interface{}{1, 2, 4}, []int32{2, 4, 1}, false}, - {[]int64{2, 4}, []interface{}{1, 2, 4}, []int64{2, 4, 1}, false}, + {[]string{"1", "2"}, []any{"9"}, []string{"1", "2", "9"}, false}, + {[]int{2, 4}, []any{1, 2, 4}, []int{2, 4, 1}, false}, + {[]int8{2, 4}, []any{int8(1), int8(2), int8(4)}, []int8{2, 4, 1}, false}, + {[]int8{2, 4}, []any{1, 2, 4}, []int8{2, 4, 1}, false}, + {[]int16{2, 4}, []any{1, 2, 4}, []int16{2, 4, 1}, false}, + {[]int32{2, 4}, []any{1, 2, 4}, []int32{2, 4, 1}, false}, + {[]int64{2, 4}, []any{1, 2, 4}, []int64{2, 4, 1}, false}, - {[]float64{2.2, 4.4}, []interface{}{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false}, - {[]float32{2.2, 4.4}, []interface{}{1.1, 2.2, 4.4}, []float32{2.2, 4.4, 1.1}, false}, + {[]float64{2.2, 4.4}, []any{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false}, + {[]float32{2.2, 4.4}, []any{1.1, 2.2, 4.4}, []float32{2.2, 4.4, 1.1}, false}, // []interface{} ∪ []T - {[]interface{}{"a", "b", "c", "c"}, []string{"a", "b", "d"}, []interface{}{"a", "b", "c", "d"}, false}, - {[]interface{}{}, []string{}, []interface{}{}, false}, - {[]interface{}{1, 2}, []int{2, 3}, []interface{}{1, 2, 3}, false}, - {[]interface{}{1, 2}, []int8{2, 3}, []interface{}{1, 2, 3}, false}, // 28 - {[]interface{}{uint(1), uint(2)}, []uint{2, 3}, []interface{}{uint(1), uint(2), uint(3)}, false}, - {[]interface{}{1.1, 2.2}, []float64{2.2, 3.3}, []interface{}{1.1, 2.2, 3.3}, false}, + {[]any{"a", "b", "c", "c"}, []string{"a", "b", "d"}, []any{"a", "b", "c", "d"}, false}, + {[]any{}, []string{}, []any{}, false}, + {[]any{1, 2}, []int{2, 3}, []any{1, 2, 3}, false}, + {[]any{1, 2}, []int8{2, 3}, []any{1, 2, 3}, false}, // 28 + {[]any{uint(1), uint(2)}, []uint{2, 3}, []any{uint(1), uint(2), uint(3)}, false}, + {[]any{1.1, 2.2}, []float64{2.2, 3.3}, []any{1.1, 2.2, 3.3}, false}, // Structs {pagesPtr{p1, p4}, pagesPtr{p4, p2, p2}, pagesPtr{p1, p4, p2}, false}, {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}, + {[]any{p1, p4}, []any{p4, p2, p2}, []any{p1, p4, p2}, false}, + {[]any{p1v}, []any{p3v, p3v}, []any{p1v, p3v}, false}, // #3686 - {[]interface{}{p1v}, []interface{}{}, []interface{}{p1v}, false}, - {[]interface{}{}, []interface{}{p1v}, []interface{}{p1v}, false}, + {[]any{p1v}, []any{}, []any{p1v}, false}, + {[]any{}, []any{p1v}, []any{p1v}, false}, {pagesPtr{p1}, pagesPtr{}, pagesPtr{p1}, false}, {pagesVals{p1v}, pagesVals{}, pagesVals{p1v}, false}, {pagesPtr{}, pagesPtr{p1}, pagesPtr{p1}, false}, @@ -847,8 +847,8 @@ func TestUniq(t *testing.T) { c := qt.New(t) ns := New(&deps.Deps{}) for i, test := range []struct { - l interface{} - expect interface{} + l any + expect any isErr bool }{ {[]string{"a", "b", "c"}, []string{"a", "b", "c"}, false}, @@ -860,7 +860,7 @@ func TestUniq(t *testing.T) { {[]int{1, 2, 2, 3}, []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}, - {nil, make([]interface{}, 0), false}, + {nil, make([]any, 0), false}, // Pointers {pagesPtr{p1, p2, p3, p2}, pagesPtr{p1, p2, p3}, false}, {pagesPtr{}, pagesPtr{}, false}, @@ -951,7 +951,7 @@ type TstXI interface { TstRv2() string } -func ToTstXIs(slice interface{}) []TstXI { +func ToTstXIs(slice any) []TstXI { s := reflect.ValueOf(slice) if s.Kind() != reflect.Slice { return nil diff --git a/tpl/collections/complement.go b/tpl/collections/complement.go index 4dc5e3bf2..723f73b0a 100644 --- a/tpl/collections/complement.go +++ b/tpl/collections/complement.go @@ -25,7 +25,7 @@ import ( // // The reasoning behind this rather clumsy API is so we can do this in the templates: // {{ $c := .Pages | complement $last4 }} -func (ns *Namespace) Complement(seqs ...interface{}) (interface{}, error) { +func (ns *Namespace) Complement(seqs ...any) (any, error) { if len(seqs) < 2 { return nil, errors.New("complement needs at least two arguments") } diff --git a/tpl/collections/complement_test.go b/tpl/collections/complement_test.go index 7e8d695b4..92565bf28 100644 --- a/tpl/collections/complement_test.go +++ b/tpl/collections/complement_test.go @@ -48,27 +48,27 @@ func TestComplement(t *testing.T) { sp2_2 := StructWithSlicePointers{xb, xe} for i, test := range []struct { - s interface{} - t []interface{} - expected interface{} + s any + t []any + expected any }{ - {[]string{"a", "b", "c"}, []interface{}{[]string{"c", "d"}}, []string{"a", "b"}}, - {[]string{"a", "b", "c"}, []interface{}{[]string{"c", "d"}, []string{"a", "b"}}, []string{}}, - {[]interface{}{"a", "b", nil}, []interface{}{[]string{"a", "d"}}, []interface{}{"b", nil}}, - {[]int{1, 2, 3, 4, 5}, []interface{}{[]int{1, 3}, []string{"a", "b"}, []int{1, 2}}, []int{4, 5}}, - {[]int{1, 2, 3, 4, 5}, []interface{}{[]int64{1, 3}}, []int{2, 4, 5}}, - {s1, []interface{}{s2}, []TstX{{A: "a"}, {A: "d"}}}, - {sp1, []interface{}{sp2}, []*StructWithSlice{xa, xd}}, - {sp1_2, []interface{}{sp2_2}, StructWithSlicePointers{xa, xd}}, + {[]string{"a", "b", "c"}, []any{[]string{"c", "d"}}, []string{"a", "b"}}, + {[]string{"a", "b", "c"}, []any{[]string{"c", "d"}, []string{"a", "b"}}, []string{}}, + {[]any{"a", "b", nil}, []any{[]string{"a", "d"}}, []any{"b", nil}}, + {[]int{1, 2, 3, 4, 5}, []any{[]int{1, 3}, []string{"a", "b"}, []int{1, 2}}, []int{4, 5}}, + {[]int{1, 2, 3, 4, 5}, []any{[]int64{1, 3}}, []int{2, 4, 5}}, + {s1, []any{s2}, []TstX{{A: "a"}, {A: "d"}}}, + {sp1, []any{sp2}, []*StructWithSlice{xa, xd}}, + {sp1_2, []any{sp2_2}, StructWithSlicePointers{xa, xd}}, // Errors - {[]string{"a", "b", "c"}, []interface{}{"error"}, false}, - {"error", []interface{}{[]string{"c", "d"}, []string{"a", "b"}}, false}, - {[]string{"a", "b", "c"}, []interface{}{[][]string{{"c", "d"}}}, false}, + {[]string{"a", "b", "c"}, []any{"error"}, false}, + {"error", []any{[]string{"c", "d"}, []string{"a", "b"}}, false}, + {[]string{"a", "b", "c"}, []any{[][]string{{"c", "d"}}}, false}, { - []interface{}{[][]string{{"c", "d"}}}, - []interface{}{[]string{"c", "d"}, []string{"a", "b"}}, - []interface{}{[][]string{{"c", "d"}}}, + []any{[][]string{{"c", "d"}}}, + []any{[]string{"c", "d"}, []string{"a", "b"}}, + []any{[][]string{{"c", "d"}}}, }, } { diff --git a/tpl/collections/index.go b/tpl/collections/index.go index cd1d1577b..43fccbc6b 100644 --- a/tpl/collections/index.go +++ b/tpl/collections/index.go @@ -32,7 +32,7 @@ import ( // We deviate from the stdlib due to https://github.com/golang/go/issues/14751. // // TODO(moorereason): merge upstream changes. -func (ns *Namespace) Index(item interface{}, args ...interface{}) (interface{}, error) { +func (ns *Namespace) Index(item any, args ...any) (any, error) { v := reflect.ValueOf(item) if !v.IsValid() { return nil, errors.New("index of untyped nil") @@ -43,7 +43,7 @@ func (ns *Namespace) Index(item interface{}, args ...interface{}) (interface{}, return lowerm.Get(cast.ToStringSlice(args)...), nil } - var indices []interface{} + var indices []any if len(args) == 1 { v := reflect.ValueOf(args[0]) diff --git a/tpl/collections/index_test.go b/tpl/collections/index_test.go index c690fe648..b9d5127ee 100644 --- a/tpl/collections/index_test.go +++ b/tpl/collections/index_test.go @@ -29,28 +29,28 @@ func TestIndex(t *testing.T) { ns := New(&deps.Deps{}) for i, test := range []struct { - item interface{} - indices []interface{} - expect interface{} + item any + indices []any + expect any isErr bool }{ - {[]int{0, 1}, []interface{}{0}, 0, false}, - {[]int{0, 1}, []interface{}{9}, nil, false}, // index out of range + {[]int{0, 1}, []any{0}, 0, false}, + {[]int{0, 1}, []any{9}, nil, false}, // index out of range {[]uint{0, 1}, nil, []uint{0, 1}, false}, - {[][]int{{1, 2}, {3, 4}}, []interface{}{0, 0}, 1, false}, - {map[int]int{1: 10, 2: 20}, []interface{}{1}, 10, false}, - {map[int]int{1: 10, 2: 20}, []interface{}{0}, 0, false}, - {map[string]map[string]string{"a": {"b": "c"}}, []interface{}{"a", "b"}, "c", false}, - {[]map[string]map[string]string{{"a": {"b": "c"}}}, []interface{}{0, "a", "b"}, "c", false}, - {map[string]map[string]interface{}{"a": {"b": []string{"c", "d"}}}, []interface{}{"a", "b", 1}, "d", false}, - {map[string]map[string]string{"a": {"b": "c"}}, []interface{}{[]string{"a", "b"}}, "c", false}, - {maps.Params{"a": "av"}, []interface{}{"A"}, "av", false}, - {maps.Params{"a": map[string]interface{}{"b": "bv"}}, []interface{}{"A", "B"}, "bv", false}, + {[][]int{{1, 2}, {3, 4}}, []any{0, 0}, 1, false}, + {map[int]int{1: 10, 2: 20}, []any{1}, 10, false}, + {map[int]int{1: 10, 2: 20}, []any{0}, 0, false}, + {map[string]map[string]string{"a": {"b": "c"}}, []any{"a", "b"}, "c", false}, + {[]map[string]map[string]string{{"a": {"b": "c"}}}, []any{0, "a", "b"}, "c", false}, + {map[string]map[string]any{"a": {"b": []string{"c", "d"}}}, []any{"a", "b", 1}, "d", false}, + {map[string]map[string]string{"a": {"b": "c"}}, []any{[]string{"a", "b"}}, "c", false}, + {maps.Params{"a": "av"}, []any{"A"}, "av", false}, + {maps.Params{"a": map[string]any{"b": "bv"}}, []any{"A", "B"}, "bv", false}, // errors {nil, nil, nil, true}, - {[]int{0, 1}, []interface{}{"1"}, nil, true}, - {[]int{0, 1}, []interface{}{nil}, nil, true}, - {tstNoStringer{}, []interface{}{0}, nil, true}, + {[]int{0, 1}, []any{"1"}, nil, true}, + {[]int{0, 1}, []any{nil}, nil, true}, + {tstNoStringer{}, []any{0}, nil, true}, } { c.Run(fmt.Sprint(i), func(c *qt.C) { errMsg := qt.Commentf("[%d] %v", i, test) diff --git a/tpl/collections/init.go b/tpl/collections/init.go index dc4e1ff31..3d22daab0 100644 --- a/tpl/collections/init.go +++ b/tpl/collections/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.After, diff --git a/tpl/collections/merge.go b/tpl/collections/merge.go index 43815560f..abfc2da9c 100644 --- a/tpl/collections/merge.go +++ b/tpl/collections/merge.go @@ -26,7 +26,7 @@ import ( // Merge creates a copy of the final parameter and merges the preceding // parameters into it in reverse order. // Currently only maps are supported. Key handling is case insensitive. -func (ns *Namespace) Merge(params ...interface{}) (interface{}, error) { +func (ns *Namespace) Merge(params ...any) (any, error) { if len(params) < 2 { return nil, errors.New("merge requires at least two parameters") } @@ -45,7 +45,7 @@ func (ns *Namespace) Merge(params ...interface{}) (interface{}, error) { } // merge creates a copy of dst and merges src into it. -func (ns *Namespace) merge(src, dst interface{}) (interface{}, error) { +func (ns *Namespace) merge(src, dst any) (any, error) { vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src) if vdst.Kind() != reflect.Map { diff --git a/tpl/collections/merge_test.go b/tpl/collections/merge_test.go index bde4d90b1..14cca1ddd 100644 --- a/tpl/collections/merge_test.go +++ b/tpl/collections/merge_test.go @@ -29,56 +29,56 @@ import ( func TestMerge(t *testing.T) { ns := New(&deps.Deps{}) - simpleMap := map[string]interface{}{"a": 1, "b": 2} + simpleMap := map[string]any{"a": 1, "b": 2} for i, test := range []struct { name string - params []interface{} - expect interface{} + params []any + expect any isErr bool }{ { "basic", - []interface{}{ - map[string]interface{}{"a": 42, "c": 3}, - map[string]interface{}{"a": 1, "b": 2}, + []any{ + map[string]any{"a": 42, "c": 3}, + map[string]any{"a": 1, "b": 2}, }, - map[string]interface{}{"a": 1, "b": 2, "c": 3}, + map[string]any{"a": 1, "b": 2, "c": 3}, false, }, { "multi", - []interface{}{ - map[string]interface{}{"a": 42, "c": 3, "e": 11}, - map[string]interface{}{"a": 1, "b": 2}, - map[string]interface{}{"a": 9, "c": 4, "d": 7}, + []any{ + map[string]any{"a": 42, "c": 3, "e": 11}, + map[string]any{"a": 1, "b": 2}, + map[string]any{"a": 9, "c": 4, "d": 7}, }, - map[string]interface{}{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11}, + map[string]any{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11}, false, }, { "basic case insensitive", - []interface{}{ - map[string]interface{}{"A": 42, "c": 3}, - map[string]interface{}{"a": 1, "b": 2}, + []any{ + map[string]any{"A": 42, "c": 3}, + map[string]any{"a": 1, "b": 2}, }, - map[string]interface{}{"a": 1, "b": 2, "c": 3}, + map[string]any{"a": 1, "b": 2, "c": 3}, false, }, { "nested", - []interface{}{ - map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}}, - map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2}}, + []any{ + map[string]any{"a": 42, "c": 3, "b": map[string]any{"d": 55, "e": 66, "f": 3}}, + map[string]any{"a": 1, "b": map[string]any{"d": 1, "e": 2}}, }, - map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2, "f": 3}, "c": 3}, + map[string]any{"a": 1, "b": map[string]any{"d": 1, "e": 2, "f": 3}, "c": 3}, false, }, { // https://github.com/gohugoio/hugo/issues/6633 "params dst", - []interface{}{ - map[string]interface{}{"a": 42, "c": 3}, + []any{ + map[string]any{"a": 42, "c": 3}, maps.Params{"a": 1, "b": 2}, }, maps.Params{"a": int(1), "b": int(2), "c": int(3)}, @@ -86,8 +86,8 @@ func TestMerge(t *testing.T) { }, { "params dst, upper case src", - []interface{}{ - map[string]interface{}{"a": 42, "C": 3}, + []any{ + map[string]any{"a": 42, "C": 3}, maps.Params{"a": 1, "b": 2}, }, maps.Params{"a": int(1), "b": int(2), "c": int(3)}, @@ -95,26 +95,26 @@ func TestMerge(t *testing.T) { }, { "params src", - []interface{}{ + []any{ maps.Params{"a": 42, "c": 3}, - map[string]interface{}{"a": 1, "c": 2}, + map[string]any{"a": 1, "c": 2}, }, - map[string]interface{}{"a": int(1), "c": int(2)}, + map[string]any{"a": int(1), "c": int(2)}, false, }, { "params src, upper case dst", - []interface{}{ + []any{ maps.Params{"a": 42, "c": 3}, - map[string]interface{}{"a": 1, "C": 2}, + map[string]any{"a": 1, "C": 2}, }, - map[string]interface{}{"a": int(1), "C": int(2)}, + map[string]any{"a": int(1), "C": int(2)}, false, }, { "nested, params dst", - []interface{}{ - map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}}, + []any{ + map[string]any{"a": 42, "c": 3, "b": map[string]any{"d": 55, "e": 66, "f": 3}}, maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2}}, }, maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, @@ -123,19 +123,19 @@ func TestMerge(t *testing.T) { { // https://github.com/gohugoio/hugo/issues/7899 "matching keys with non-map src value", - []interface{}{ - map[string]interface{}{"k": "v"}, - map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}}, + []any{ + map[string]any{"k": "v"}, + map[string]any{"k": map[string]any{"k2": "v2"}}, }, - map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}}, + map[string]any{"k": map[string]any{"k2": "v2"}}, false, }, - {"src nil", []interface{}{nil, simpleMap}, simpleMap, false}, + {"src nil", []any{nil, simpleMap}, simpleMap, false}, // Error cases. - {"dst not a map", []interface{}{nil, "not a map"}, nil, true}, - {"src not a map", []interface{}{"not a map", simpleMap}, nil, true}, - {"different map types", []interface{}{map[int]interface{}{32: "a"}, simpleMap}, nil, true}, - {"all nil", []interface{}{nil, nil}, nil, true}, + {"dst not a map", []any{nil, "not a map"}, nil, true}, + {"src not a map", []any{"not a map", simpleMap}, nil, true}, + {"different map types", []any{map[int]any{32: "a"}, simpleMap}, nil, true}, + {"all nil", []any{nil, nil}, nil, true}, } { test := test @@ -205,9 +205,9 @@ V22 = "v22_2" c.Assert( merged, qt.DeepEquals, - map[string]interface{}{ + map[string]any{ "V1": "v1_1", "V2": "v2_2", - "V2s": map[string]interface{}{"V21": "v21_1", "V22": "v22_2"}, + "V2s": map[string]any{"V21": "v21_1", "V22": "v22_2"}, }) } } @@ -215,12 +215,12 @@ V22 = "v22_2" func TestCaseInsensitiveMapLookup(t *testing.T) { c := qt.New(t) - m1 := reflect.ValueOf(map[string]interface{}{ + m1 := reflect.ValueOf(map[string]any{ "a": 1, "B": 2, }) - m2 := reflect.ValueOf(map[int]interface{}{ + m2 := reflect.ValueOf(map[int]any{ 1: 1, 2: 2, }) diff --git a/tpl/collections/reflect_helpers.go b/tpl/collections/reflect_helpers.go index 453d2f356..4aa137e88 100644 --- a/tpl/collections/reflect_helpers.go +++ b/tpl/collections/reflect_helpers.go @@ -46,7 +46,7 @@ func numberToFloat(v reflect.Value) (float64, error) { // normalizes different numeric types if isNumber // or get the hash values if not Comparable (such as map or struct) // to make them comparable -func normalize(v reflect.Value) interface{} { +func normalize(v reflect.Value) any { k := v.Kind() switch { @@ -67,8 +67,8 @@ func normalize(v reflect.Value) interface{} { // collects identities from the slices in seqs into a set. Numeric values are normalized, // pointers unwrapped. -func collectIdentities(seqs ...interface{}) (map[interface{}]bool, error) { - seen := make(map[interface{}]bool) +func collectIdentities(seqs ...any) (map[any]bool, error) { + seen := make(map[any]bool) for _, seq := range seqs { v := reflect.ValueOf(seq) switch v.Kind() { @@ -167,7 +167,7 @@ func convertNumber(v reflect.Value, to reflect.Kind) (reflect.Value, error) { return n, nil } -func newSliceElement(items interface{}) interface{} { +func newSliceElement(items any) any { tp := reflect.TypeOf(items) if tp == nil { return nil diff --git a/tpl/collections/sort.go b/tpl/collections/sort.go index 7ca764e9b..305399eb8 100644 --- a/tpl/collections/sort.go +++ b/tpl/collections/sort.go @@ -27,7 +27,7 @@ import ( var sortComp = compare.New(true) // Sort returns a sorted sequence. -func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, error) { +func (ns *Namespace) Sort(seq any, args ...any) (any, error) { if seq == nil { return nil, errors.New("sequence must be provided") } @@ -167,7 +167,7 @@ func (p pairList) Less(i, j int) bool { } // sorts a pairList and returns a slice of sorted values -func (p pairList) sort() interface{} { +func (p pairList) sort() any { if p.SortAsc { sort.Sort(p) } else { diff --git a/tpl/collections/sort_test.go b/tpl/collections/sort_test.go index aa05b1874..698118864 100644 --- a/tpl/collections/sort_test.go +++ b/tpl/collections/sort_test.go @@ -40,10 +40,10 @@ func TestSort(t *testing.T) { } for i, test := range []struct { - seq interface{} - sortByField interface{} + seq any + sortByField any sortAsc string - expect interface{} + expect any }{ {[]string{"class1", "class2", "class3"}, nil, "asc", []string{"class1", "class2", "class3"}}, {[]string{"class3", "class1", "class2"}, nil, "asc", []string{"class1", "class2", "class3"}}, @@ -205,17 +205,17 @@ func TestSort(t *testing.T) { }, // interface slice with missing elements { - []interface{}{ - map[interface{}]interface{}{"Title": "Foo", "Weight": 10}, - map[interface{}]interface{}{"Title": "Bar"}, - map[interface{}]interface{}{"Title": "Zap", "Weight": 5}, + []any{ + map[any]any{"Title": "Foo", "Weight": 10}, + map[any]any{"Title": "Bar"}, + map[any]any{"Title": "Zap", "Weight": 5}, }, "Weight", "asc", - []interface{}{ - map[interface{}]interface{}{"Title": "Bar"}, - map[interface{}]interface{}{"Title": "Zap", "Weight": 5}, - map[interface{}]interface{}{"Title": "Foo", "Weight": 10}, + []any{ + map[any]any{"Title": "Bar"}, + map[any]any{"Title": "Zap", "Weight": 5}, + map[any]any{"Title": "Foo", "Weight": 10}, }, }, // test boolean values @@ -239,7 +239,7 @@ func TestSort(t *testing.T) { {nil, nil, "asc", false}, } { t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) { - var result interface{} + var result any var err error if test.sortByField == nil { result, err = ns.Sort(test.seq) diff --git a/tpl/collections/symdiff.go b/tpl/collections/symdiff.go index e61814e26..1c411bc35 100644 --- a/tpl/collections/symdiff.go +++ b/tpl/collections/symdiff.go @@ -22,7 +22,7 @@ import ( // SymDiff returns the symmetric difference of s1 and s2. // Arguments must be either a slice or an array of comparable types. -func (ns *Namespace) SymDiff(s2, s1 interface{}) (interface{}, error) { +func (ns *Namespace) SymDiff(s2, s1 any) (any, error) { ids1, err := collectIdentities(s1) if err != nil { return nil, err @@ -35,7 +35,7 @@ func (ns *Namespace) SymDiff(s2, s1 interface{}) (interface{}, error) { var slice reflect.Value var sliceElemType reflect.Type - for i, s := range []interface{}{s1, s2} { + for i, s := range []any{s1, s2} { v := reflect.ValueOf(s) switch v.Kind() { diff --git a/tpl/collections/symdiff_test.go b/tpl/collections/symdiff_test.go index 120dfaac6..c781ca040 100644 --- a/tpl/collections/symdiff_test.go +++ b/tpl/collections/symdiff_test.go @@ -38,13 +38,13 @@ func TestSymDiff(t *testing.T) { sp2 := []*StructWithSlice{xb, xe} for i, test := range []struct { - s1 interface{} - s2 interface{} - expected interface{} + s1 any + s2 any + expected any }{ {[]string{"a", "x", "b", "c"}, []string{"a", "b", "y", "c"}, []string{"x", "y"}}, {[]string{"a", "b", "c"}, []string{"a", "b", "c"}, []string{}}, - {[]interface{}{"a", "b", nil}, []interface{}{"a"}, []interface{}{"b", nil}}, + {[]any{"a", "b", nil}, []any{"a"}, []any{"b", nil}}, {[]int{1, 2, 3}, []int{3, 4}, []int{1, 2, 4}}, {[]int{1, 2, 3}, []int64{3, 4}, []int{1, 2, 4}}, {s1, s2, []TstX{{A: "b"}, {A: "e"}}}, diff --git a/tpl/collections/where.go b/tpl/collections/where.go index ac604760d..8a9df445d 100644 --- a/tpl/collections/where.go +++ b/tpl/collections/where.go @@ -24,7 +24,7 @@ import ( ) // Where returns a filtered subset of a given data type. -func (ns *Namespace) Where(seq, key interface{}, args ...interface{}) (interface{}, error) { +func (ns *Namespace) Where(seq, key any, args ...any) (any, error) { seqv, isNil := indirect(reflect.ValueOf(seq)) if isNil { return nil, errors.New("can't iterate over a nil value of type " + reflect.ValueOf(seq).Type().String()) @@ -84,7 +84,7 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error var ivp, imvp *int64 var fvp, fmvp *float64 var svp, smvp *string - var slv, slmv interface{} + var slv, slmv any var ima []int64 var fma []float64 var sma []string @@ -353,7 +353,7 @@ func evaluateSubElem(obj reflect.Value, elemName string) (reflect.Value, error) // parseWhereArgs parses the end arguments to the where function. Return a // match value and an operator, if one is defined. -func parseWhereArgs(args ...interface{}) (mv reflect.Value, op string, err error) { +func parseWhereArgs(args ...any) (mv reflect.Value, op string, err error) { switch len(args) { case 1: mv = reflect.ValueOf(args[0]) @@ -373,7 +373,7 @@ func parseWhereArgs(args ...interface{}) (mv reflect.Value, op string, err error // checkWhereArray handles the where-matching logic when the seqv value is an // Array or Slice. -func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, op string) (interface{}, error) { +func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, op string) (any, error) { rv := reflect.MakeSlice(seqv.Type(), 0, 0) for i := 0; i < seqv.Len(); i++ { @@ -419,7 +419,7 @@ func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, } // checkWhereMap handles the where-matching logic when the seqv value is a Map. -func (ns *Namespace) checkWhereMap(seqv, kv, mv reflect.Value, path []string, op string) (interface{}, error) { +func (ns *Namespace) checkWhereMap(seqv, kv, mv reflect.Value, path []string, op string) (any, error) { rv := reflect.MakeMap(seqv.Type()) keys := seqv.MapKeys() for _, k := range keys { diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go index 2a97c8cd0..395a90cb1 100644 --- a/tpl/collections/where_test.go +++ b/tpl/collections/where_test.go @@ -43,11 +43,11 @@ func TestWhere(t *testing.T) { d6 := d5.Add(1 * time.Hour) type testt struct { - seq interface{} - key interface{} + seq any + key any op string - match interface{} - expect interface{} + match any + expect any } createTestVariants := func(test testt) []testt { @@ -150,11 +150,11 @@ func TestWhere(t *testing.T) { // Issue #8353 // String type mismatch. { - seq: []map[string]interface{}{ + seq: []map[string]any{ {"a": "1", "b": "2"}, {"a": "3", "b": template.HTML("4")}, {"a": "5", "x": "4"}, }, key: "b", match: "4", - expect: []map[string]interface{}{ + expect: []map[string]any{ {"a": "3", "b": template.HTML("4")}, }, }, @@ -184,9 +184,9 @@ func TestWhere(t *testing.T) { expect: []TstParams{{params: maps.Params{"i": 1, "color": "blue"}}, {params: maps.Params{"i": 3, "color": "blue"}}}, }, { - seq: []TstParams{{params: maps.Params{"nested": map[string]interface{}{"color": "indigo"}}}, {params: maps.Params{"nested": map[string]interface{}{"color": "blue"}}}}, + seq: []TstParams{{params: maps.Params{"nested": map[string]any{"color": "indigo"}}}, {params: maps.Params{"nested": map[string]any{"color": "blue"}}}}, key: ".Params.NEsTED.COLOR", match: "blue", - expect: []TstParams{{params: maps.Params{"nested": map[string]interface{}{"color": "blue"}}}}, + expect: []TstParams{{params: maps.Params{"nested": map[string]any{"color": "blue"}}}}, }, { seq: []TstParams{{params: maps.Params{"i": 0, "color": "indigo"}}, {params: maps.Params{"i": 1, "color": "blue"}}, {params: maps.Params{"i": 2, "color": "green"}}, {params: maps.Params{"i": 3, "color": "blue"}}}, @@ -206,12 +206,12 @@ func TestWhere(t *testing.T) { { seq: []maps.Params{ { - "a": map[string]interface{}{ + "a": map[string]any{ "b": "b1", }, }, { - "a": map[string]interface{}{ + "a": map[string]any{ "b": "b2", }, }, @@ -219,7 +219,7 @@ func TestWhere(t *testing.T) { key: "A.B", match: "b2", expect: []maps.Params{ { - "a": map[string]interface{}{ + "a": map[string]any{ "b": "b2", }, }, @@ -598,38 +598,38 @@ func TestWhere(t *testing.T) { expect: false, }, { - seq: map[string]interface{}{ - "foo": []interface{}{map[interface{}]interface{}{"a": 1, "b": 2}}, - "bar": []interface{}{map[interface{}]interface{}{"a": 3, "b": 4}}, - "zap": []interface{}{map[interface{}]interface{}{"a": 5, "b": 6}}, + seq: map[string]any{ + "foo": []any{map[any]any{"a": 1, "b": 2}}, + "bar": []any{map[any]any{"a": 3, "b": 4}}, + "zap": []any{map[any]any{"a": 5, "b": 6}}, }, key: "b", op: "in", match: ns.Slice(3, 4, 5), - expect: map[string]interface{}{ - "bar": []interface{}{map[interface{}]interface{}{"a": 3, "b": 4}}, + expect: map[string]any{ + "bar": []any{map[any]any{"a": 3, "b": 4}}, }, }, { - seq: map[string]interface{}{ - "foo": []interface{}{map[interface{}]interface{}{"a": 1, "b": 2}}, - "bar": []interface{}{map[interface{}]interface{}{"a": 3, "b": 4}}, - "zap": []interface{}{map[interface{}]interface{}{"a": 5, "b": 6}}, + seq: map[string]any{ + "foo": []any{map[any]any{"a": 1, "b": 2}}, + "bar": []any{map[any]any{"a": 3, "b": 4}}, + "zap": []any{map[any]any{"a": 5, "b": 6}}, }, key: "b", op: ">", match: 3, - expect: map[string]interface{}{ - "bar": []interface{}{map[interface{}]interface{}{"a": 3, "b": 4}}, - "zap": []interface{}{map[interface{}]interface{}{"a": 5, "b": 6}}, + expect: map[string]any{ + "bar": []any{map[any]any{"a": 3, "b": 4}}, + "zap": []any{map[any]any{"a": 5, "b": 6}}, }, }, { - seq: map[string]interface{}{ - "foo": []interface{}{maps.Params{"a": 1, "b": 2}}, - "bar": []interface{}{maps.Params{"a": 3, "b": 4}}, - "zap": []interface{}{maps.Params{"a": 5, "b": 6}}, + seq: map[string]any{ + "foo": []any{maps.Params{"a": 1, "b": 2}}, + "bar": []any{maps.Params{"a": 3, "b": 4}}, + "zap": []any{maps.Params{"a": 5, "b": 6}}, }, key: "B", op: ">", match: 3, - expect: map[string]interface{}{ - "bar": []interface{}{maps.Params{"a": 3, "b": 4}}, - "zap": []interface{}{maps.Params{"a": 5, "b": 6}}, + expect: map[string]any{ + "bar": []any{maps.Params{"a": 3, "b": 4}}, + "zap": []any{maps.Params{"a": 5, "b": 6}}, }, }, } { @@ -639,7 +639,7 @@ func TestWhere(t *testing.T) { name := fmt.Sprintf("%d/%d %T %s %s", i, j, test.seq, test.op, test.key) name = strings.ReplaceAll(name, "[]", "slice-of-") t.Run(name, func(t *testing.T) { - var results interface{} + var results any var err error if len(test.op) > 0 { @@ -788,10 +788,10 @@ func TestCheckCondition(t *testing.T) { {reflect.ValueOf(123), reflect.ValueOf(123), "op", expect{false, true}}, // Issue #3718 - {reflect.ValueOf([]interface{}{"a"}), reflect.ValueOf([]string{"a", "b"}), "intersect", expect{true, false}}, - {reflect.ValueOf([]string{"a"}), reflect.ValueOf([]interface{}{"a", "b"}), "intersect", expect{true, false}}, - {reflect.ValueOf([]interface{}{1, 2}), reflect.ValueOf([]int{1}), "intersect", expect{true, false}}, - {reflect.ValueOf([]int{1}), reflect.ValueOf([]interface{}{1, 2}), "intersect", expect{true, false}}, + {reflect.ValueOf([]any{"a"}), reflect.ValueOf([]string{"a", "b"}), "intersect", expect{true, false}}, + {reflect.ValueOf([]string{"a"}), reflect.ValueOf([]any{"a", "b"}), "intersect", expect{true, false}}, + {reflect.ValueOf([]any{1, 2}), reflect.ValueOf([]int{1}), "intersect", expect{true, false}}, + {reflect.ValueOf([]int{1}), reflect.ValueOf([]any{1, 2}), "intersect", expect{true, false}}, } { result, err := ns.checkCondition(test.value, test.match, test.op) if test.expect.isError { @@ -822,7 +822,7 @@ func TestEvaluateSubElem(t *testing.T) { for i, test := range []struct { value reflect.Value key string - expect interface{} + expect any }{ {reflect.ValueOf(tstx), "A", "foo"}, {reflect.ValueOf(&tstx), "TstRp", "rfoo"}, diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go index 8f3f536b5..21b9c5bcd 100644 --- a/tpl/compare/compare.go +++ b/tpl/compare/compare.go @@ -40,7 +40,7 @@ type Namespace struct { // is not. "Set" in this context means non-zero for numeric types and times; // non-zero length for strings, arrays, slices, and maps; // any boolean or struct value; or non-nil for any other types. -func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{}, error) { +func (*Namespace) Default(dflt any, given ...any) (any, error) { // given is variadic because the following construct will not pass a piped // argument when the key is missing: {{ index . "key" | default "foo" }} // The Go template will complain that we got 1 argument when we expected 2. @@ -91,12 +91,12 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{}, } // Eq returns the boolean truth of arg1 == arg2 || arg1 == arg3 || arg1 == arg4. -func (n *Namespace) Eq(first interface{}, others ...interface{}) bool { +func (n *Namespace) Eq(first any, others ...any) bool { if n.caseInsensitive { panic("caseInsensitive not implemented for Eq") } n.checkComparisonArgCount(1, others...) - normalize := func(v interface{}) interface{} { + normalize := func(v any) any { if types.IsNil(v) { return nil } @@ -141,7 +141,7 @@ func (n *Namespace) Eq(first interface{}, others ...interface{}) bool { } // Ne returns the boolean truth of arg1 != arg2 && arg1 != arg3 && arg1 != arg4. -func (n *Namespace) Ne(first interface{}, others ...interface{}) bool { +func (n *Namespace) Ne(first any, others ...any) bool { n.checkComparisonArgCount(1, others...) for _, other := range others { if n.Eq(first, other) { @@ -152,7 +152,7 @@ func (n *Namespace) Ne(first interface{}, others ...interface{}) bool { } // Ge returns the boolean truth of arg1 >= arg2 && arg1 >= arg3 && arg1 >= arg4. -func (n *Namespace) Ge(first interface{}, others ...interface{}) bool { +func (n *Namespace) Ge(first any, others ...any) bool { n.checkComparisonArgCount(1, others...) for _, other := range others { left, right := n.compareGet(first, other) @@ -164,7 +164,7 @@ func (n *Namespace) Ge(first interface{}, others ...interface{}) bool { } // Gt returns the boolean truth of arg1 > arg2 && arg1 > arg3 && arg1 > arg4. -func (n *Namespace) Gt(first interface{}, others ...interface{}) bool { +func (n *Namespace) Gt(first any, others ...any) bool { n.checkComparisonArgCount(1, others...) for _, other := range others { left, right := n.compareGet(first, other) @@ -176,7 +176,7 @@ func (n *Namespace) Gt(first interface{}, others ...interface{}) bool { } // Le returns the boolean truth of arg1 <= arg2 && arg1 <= arg3 && arg1 <= arg4. -func (n *Namespace) Le(first interface{}, others ...interface{}) bool { +func (n *Namespace) Le(first any, others ...any) bool { n.checkComparisonArgCount(1, others...) for _, other := range others { left, right := n.compareGet(first, other) @@ -188,7 +188,7 @@ func (n *Namespace) Le(first interface{}, others ...interface{}) bool { } // Lt returns the boolean truth of arg1 < arg2 && arg1 < arg3 && arg1 < arg4. -func (n *Namespace) Lt(first interface{}, others ...interface{}) bool { +func (n *Namespace) Lt(first any, others ...any) bool { n.checkComparisonArgCount(1, others...) for _, other := range others { left, right := n.compareGet(first, other) @@ -199,7 +199,7 @@ func (n *Namespace) Lt(first interface{}, others ...interface{}) bool { return true } -func (n *Namespace) checkComparisonArgCount(min int, others ...interface{}) bool { +func (n *Namespace) checkComparisonArgCount(min int, others ...any) bool { if len(others) < min { panic("missing arguments for comparison") } @@ -208,14 +208,14 @@ func (n *Namespace) checkComparisonArgCount(min int, others ...interface{}) bool // Conditional can be used as a ternary operator. // It returns a if condition, else b. -func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} { +func (n *Namespace) Conditional(condition bool, a, b any) any { if condition { return a } return b } -func (ns *Namespace) compareGet(a interface{}, b interface{}) (float64, float64) { +func (ns *Namespace) compareGet(a any, b any) (float64, float64) { if ac, ok := a.(compare.Comparer); ok { c := ac.Compare(b) if c < 0 { diff --git a/tpl/compare/compare_test.go b/tpl/compare/compare_test.go index 9ef32fd85..782941b15 100644 --- a/tpl/compare/compare_test.go +++ b/tpl/compare/compare_test.go @@ -49,7 +49,7 @@ type ( tstEqerType2 string ) -func (t tstEqerType2) Eq(other interface{}) bool { +func (t tstEqerType2) Eq(other any) bool { return cast.ToString(t) == cast.ToString(other) } @@ -57,7 +57,7 @@ func (t tstEqerType2) String() string { return string(t) } -func (t tstEqerType1) Eq(other interface{}) bool { +func (t tstEqerType1) Eq(other any) bool { return cast.ToString(t) == cast.ToString(other) } @@ -91,9 +91,9 @@ func TestDefaultFunc(t *testing.T) { ns := New(false) for i, test := range []struct { - dflt interface{} - given interface{} - expect interface{} + dflt any + given any + expect any }{ {true, false, false}, {"5", 0, "5"}, @@ -149,33 +149,33 @@ func TestCompare(t *testing.T) { n := New(false) - twoEq := func(a, b interface{}) bool { + twoEq := func(a, b any) bool { return n.Eq(a, b) } - twoGt := func(a, b interface{}) bool { + twoGt := func(a, b any) bool { return n.Gt(a, b) } - twoLt := func(a, b interface{}) bool { + twoLt := func(a, b any) bool { return n.Lt(a, b) } - twoGe := func(a, b interface{}) bool { + twoGe := func(a, b any) bool { return n.Ge(a, b) } - twoLe := func(a, b interface{}) bool { + twoLe := func(a, b any) bool { return n.Le(a, b) } - twoNe := func(a, b interface{}) bool { + twoNe := func(a, b any) bool { return n.Ne(a, b) } for _, test := range []struct { tstCompareType - funcUnderTest func(a, b interface{}) bool + funcUnderTest func(a, b any) bool }{ {tstGt, twoGt}, {tstLt, twoLt}, @@ -188,10 +188,10 @@ func TestCompare(t *testing.T) { } } -func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b interface{}) bool) { +func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b any) bool) { for i, test := range []struct { - left interface{} - right interface{} + left any + right any expectIndicator int }{ {5, 8, -1}, @@ -272,16 +272,16 @@ func TestEqualExtend(t *testing.T) { ns := New(false) for _, test := range []struct { - first interface{} - others []interface{} + first any + others []any expect bool }{ - {1, []interface{}{1, 2}, true}, - {1, []interface{}{2, 1}, true}, - {1, []interface{}{2, 3}, false}, - {tstEqerType1("a"), []interface{}{tstEqerType1("a"), tstEqerType1("b")}, true}, - {tstEqerType1("a"), []interface{}{tstEqerType1("b"), tstEqerType1("a")}, true}, - {tstEqerType1("a"), []interface{}{tstEqerType1("b"), tstEqerType1("c")}, false}, + {1, []any{1, 2}, true}, + {1, []any{2, 1}, true}, + {1, []any{2, 3}, false}, + {tstEqerType1("a"), []any{tstEqerType1("a"), tstEqerType1("b")}, true}, + {tstEqerType1("a"), []any{tstEqerType1("b"), tstEqerType1("a")}, true}, + {tstEqerType1("a"), []any{tstEqerType1("b"), tstEqerType1("c")}, false}, } { result := ns.Eq(test.first, test.others...) @@ -297,13 +297,13 @@ func TestNotEqualExtend(t *testing.T) { ns := New(false) for _, test := range []struct { - first interface{} - others []interface{} + first any + others []any expect bool }{ - {1, []interface{}{2, 3}, true}, - {1, []interface{}{2, 1}, false}, - {1, []interface{}{1, 2}, false}, + {1, []any{2, 3}, true}, + {1, []any{2, 1}, false}, + {1, []any{1, 2}, false}, } { result := ns.Ne(test.first, test.others...) c.Assert(result, qt.Equals, test.expect) @@ -317,14 +317,14 @@ func TestGreaterEqualExtend(t *testing.T) { ns := New(false) for _, test := range []struct { - first interface{} - others []interface{} + first any + others []any expect bool }{ - {5, []interface{}{2, 3}, true}, - {5, []interface{}{5, 5}, true}, - {3, []interface{}{4, 2}, false}, - {3, []interface{}{2, 4}, false}, + {5, []any{2, 3}, true}, + {5, []any{5, 5}, true}, + {3, []any{4, 2}, false}, + {3, []any{2, 4}, false}, } { result := ns.Ge(test.first, test.others...) c.Assert(result, qt.Equals, test.expect) @@ -338,13 +338,13 @@ func TestGreaterThanExtend(t *testing.T) { ns := New(false) for _, test := range []struct { - first interface{} - others []interface{} + first any + others []any expect bool }{ - {5, []interface{}{2, 3}, true}, - {5, []interface{}{5, 4}, false}, - {3, []interface{}{4, 2}, false}, + {5, []any{2, 3}, true}, + {5, []any{5, 4}, false}, + {3, []any{4, 2}, false}, } { result := ns.Gt(test.first, test.others...) c.Assert(result, qt.Equals, test.expect) @@ -358,14 +358,14 @@ func TestLessEqualExtend(t *testing.T) { ns := New(false) for _, test := range []struct { - first interface{} - others []interface{} + first any + others []any expect bool }{ - {1, []interface{}{2, 3}, true}, - {1, []interface{}{1, 2}, true}, - {2, []interface{}{1, 2}, false}, - {3, []interface{}{2, 4}, false}, + {1, []any{2, 3}, true}, + {1, []any{1, 2}, true}, + {2, []any{1, 2}, false}, + {3, []any{2, 4}, false}, } { result := ns.Le(test.first, test.others...) c.Assert(result, qt.Equals, test.expect) @@ -379,14 +379,14 @@ func TestLessThanExtend(t *testing.T) { ns := New(false) for _, test := range []struct { - first interface{} - others []interface{} + first any + others []any expect bool }{ - {1, []interface{}{2, 3}, true}, - {1, []interface{}{1, 2}, false}, - {2, []interface{}{1, 2}, false}, - {3, []interface{}{2, 4}, false}, + {1, []any{2, 3}, true}, + {1, []any{1, 2}, false}, + {2, []any{1, 2}, false}, + {3, []any{2, 4}, false}, } { result := ns.Lt(test.first, test.others...) c.Assert(result, qt.Equals, test.expect) diff --git a/tpl/compare/init.go b/tpl/compare/init.go index f423f615e..2308b235e 100644 --- a/tpl/compare/init.go +++ b/tpl/compare/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Default, diff --git a/tpl/crypto/crypto.go b/tpl/crypto/crypto.go index 3a825bf15..785b13502 100644 --- a/tpl/crypto/crypto.go +++ b/tpl/crypto/crypto.go @@ -36,7 +36,7 @@ func New() *Namespace { type Namespace struct{} // MD5 hashes the given input and returns its MD5 checksum. -func (ns *Namespace) MD5(in interface{}) (string, error) { +func (ns *Namespace) MD5(in any) (string, error) { conv, err := cast.ToStringE(in) if err != nil { return "", err @@ -47,7 +47,7 @@ func (ns *Namespace) MD5(in interface{}) (string, error) { } // SHA1 hashes the given input and returns its SHA1 checksum. -func (ns *Namespace) SHA1(in interface{}) (string, error) { +func (ns *Namespace) SHA1(in any) (string, error) { conv, err := cast.ToStringE(in) if err != nil { return "", err @@ -58,7 +58,7 @@ func (ns *Namespace) SHA1(in interface{}) (string, error) { } // SHA256 hashes the given input and returns its SHA256 checksum. -func (ns *Namespace) SHA256(in interface{}) (string, error) { +func (ns *Namespace) SHA256(in any) (string, error) { conv, err := cast.ToStringE(in) if err != nil { return "", err @@ -69,7 +69,7 @@ func (ns *Namespace) SHA256(in interface{}) (string, error) { } // HMAC returns a cryptographic hash that uses a key to sign a message. -func (ns *Namespace) HMAC(h interface{}, k interface{}, m interface{}) (string, error) { +func (ns *Namespace) HMAC(h any, k any, m any) (string, error) { ha, err := cast.ToStringE(h) if err != nil { return "", err diff --git a/tpl/crypto/crypto_test.go b/tpl/crypto/crypto_test.go index fe82f2afd..78e387318 100644 --- a/tpl/crypto/crypto_test.go +++ b/tpl/crypto/crypto_test.go @@ -26,8 +26,8 @@ func TestMD5(t *testing.T) { ns := New() for i, test := range []struct { - in interface{} - expect interface{} + in any + expect any }{ {"Hello world, gophers!", "b3029f756f98f79e7f1b7f1d1f0dd53b"}, {"Lorem ipsum dolor", "06ce65ac476fc656bea3fca5d02cfd81"}, @@ -53,8 +53,8 @@ func TestSHA1(t *testing.T) { ns := New() for i, test := range []struct { - in interface{} - expect interface{} + in any + expect any }{ {"Hello world, gophers!", "c8b5b0e33d408246e30f53e32b8f7627a7a649d4"}, {"Lorem ipsum dolor", "45f75b844be4d17b3394c6701768daf39419c99b"}, @@ -80,8 +80,8 @@ func TestSHA256(t *testing.T) { ns := New() for i, test := range []struct { - in interface{} - expect interface{} + in any + expect any }{ {"Hello world, gophers!", "6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46"}, {"Lorem ipsum dolor", "9b3e1beb7053e0f900a674dd1c99aca3355e1275e1b03d3cb1bc977f5154e196"}, @@ -107,10 +107,10 @@ func TestHMAC(t *testing.T) { ns := New() for i, test := range []struct { - hash interface{} - key interface{} - msg interface{} - expect interface{} + hash any + key any + msg any + expect any }{ {"md5", "Secret key", "Hello world, gophers!", "36eb69b6bf2de96b6856fdee8bf89754"}, {"sha1", "Secret key", "Hello world, gophers!", "84a76647de6cd47ac6ae4258e3753f711172ce68"}, diff --git a/tpl/crypto/init.go b/tpl/crypto/init.go index 5ce2a4b5e..4b7cde2fa 100644 --- a/tpl/crypto/init.go +++ b/tpl/crypto/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.MD5, diff --git a/tpl/data/data.go b/tpl/data/data.go index cfd847474..5e03d52c3 100644 --- a/tpl/data/data.go +++ b/tpl/data/data.go @@ -63,7 +63,7 @@ type Namespace struct { // The data separator can be a comma, semi-colon, pipe, etc, but only one character. // If you provide multiple parts for the URL they will be joined together to the final URL. // GetCSV returns nil or a slice slice to use in a short code. -func (ns *Namespace) GetCSV(sep string, args ...interface{}) (d [][]string, err error) { +func (ns *Namespace) GetCSV(sep string, args ...any) (d [][]string, err error) { url, headers := toURLAndHeaders(args) cache := ns.cacheGetCSV @@ -102,8 +102,8 @@ func (ns *Namespace) GetCSV(sep string, args ...interface{}) (d [][]string, err // GetJSON expects one or n-parts of a URL to a resource which can either be a local or a remote one. // If you provide multiple parts they will be joined together to the final URL. // GetJSON returns nil or parsed JSON to use in a short code. -func (ns *Namespace) GetJSON(args ...interface{}) (interface{}, error) { - var v interface{} +func (ns *Namespace) GetJSON(args ...any) (any, error) { + var v any url, headers := toURLAndHeaders(args) cache := ns.cacheGetJSON @@ -146,7 +146,7 @@ func addDefaultHeaders(req *http.Request, accepts ...string) { } } -func addUserProvidedHeaders(headers map[string]interface{}, req *http.Request) { +func addUserProvidedHeaders(headers map[string]any, req *http.Request) { if headers == nil { return } @@ -179,7 +179,7 @@ func hasHeaderKey(m http.Header, key string) bool { return ok } -func toURLAndHeaders(urlParts []interface{}) (string, map[string]interface{}) { +func toURLAndHeaders(urlParts []any) (string, map[string]any) { if len(urlParts) == 0 { return "", nil } diff --git a/tpl/data/data_test.go b/tpl/data/data_test.go index 8a18a19e4..3d365e5fb 100644 --- a/tpl/data/data_test.go +++ b/tpl/data/data_test.go @@ -35,7 +35,7 @@ func TestGetCSV(t *testing.T) { sep string url string content string - expect interface{} + expect any }{ // Remotes { @@ -129,12 +129,12 @@ func TestGetJSON(t *testing.T) { for i, test := range []struct { url string content string - expect interface{} + expect any }{ { `http://success/`, `{"gomeetup":["Sydney","San Francisco","Stockholm"]}`, - map[string]interface{}{"gomeetup": []interface{}{"Sydney", "San Francisco", "Stockholm"}}, + map[string]any{"gomeetup": []any{"Sydney", "San Francisco", "Stockholm"}}, }, { `http://malformed/`, @@ -150,7 +150,7 @@ func TestGetJSON(t *testing.T) { { "pass/semi", `{"gomeetup":["Sydney","San Francisco","Stockholm"]}`, - map[string]interface{}{"gomeetup": []interface{}{"Sydney", "San Francisco", "Stockholm"}}, + map[string]any{"gomeetup": []any{"Sydney", "San Francisco", "Stockholm"}}, }, { "fail/no-file", @@ -160,7 +160,7 @@ func TestGetJSON(t *testing.T) { { `pass/üńīçøðê-url.json`, `{"gomeetup":["Sydney","San Francisco","Stockholm"]}`, - map[string]interface{}{"gomeetup": []interface{}{"Sydney", "San Francisco", "Stockholm"}}, + map[string]any{"gomeetup": []any{"Sydney", "San Francisco", "Stockholm"}}, }, } { @@ -218,12 +218,12 @@ func TestHeaders(t *testing.T) { for _, test := range []struct { name string - headers interface{} + headers any assert func(c *qt.C, headers string) }{ { `Misc header variants`, - map[string]interface{}{ + map[string]any{ "Accept-Charset": "utf-8", "Max-forwards": "10", "X-Int": 32, @@ -254,7 +254,7 @@ func TestHeaders(t *testing.T) { }, { `Override User-Agent`, - map[string]interface{}{ + map[string]any{ "User-Agent": "007", }, func(c *qt.C, headers string) { @@ -278,7 +278,7 @@ func TestHeaders(t *testing.T) { }) defer func() { srv.Close() }() - testFunc := func(fn func(args ...interface{}) error) { + testFunc := func(fn func(args ...any) error) { defer headers.Reset() err := fn("http://example.org/api", "?foo", test.headers) @@ -287,11 +287,11 @@ func TestHeaders(t *testing.T) { test.assert(c, headers.String()) } - testFunc(func(args ...interface{}) error { + testFunc(func(args ...any) error { _, err := ns.GetJSON(args...) return err }) - testFunc(func(args ...interface{}) error { + testFunc(func(args ...any) error { _, err := ns.GetCSV(",", args...) return err }) @@ -304,13 +304,13 @@ func TestHeaders(t *testing.T) { func TestToURLAndHeaders(t *testing.T) { t.Parallel() c := qt.New(t) - url, headers := toURLAndHeaders([]interface{}{"https://foo?id=", 32}) + url, headers := toURLAndHeaders([]any{"https://foo?id=", 32}) c.Assert(url, qt.Equals, "https://foo?id=32") c.Assert(headers, qt.IsNil) - url, headers = toURLAndHeaders([]interface{}{"https://foo?id=", 32, map[string]interface{}{"a": "b"}}) + url, headers = toURLAndHeaders([]any{"https://foo?id=", 32, map[string]any{"a": "b"}}) c.Assert(url, qt.Equals, "https://foo?id=32") - c.Assert(headers, qt.DeepEquals, map[string]interface{}{"a": "b"}) + c.Assert(headers, qt.DeepEquals, map[string]any{"a": "b"}) } func TestParseCSV(t *testing.T) { diff --git a/tpl/data/init.go b/tpl/data/init.go index 5ac24eaab..22e685fc8 100644 --- a/tpl/data/init.go +++ b/tpl/data/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.GetCSV, diff --git a/tpl/data/resources.go b/tpl/data/resources.go index b4b310bcc..dca25efb2 100644 --- a/tpl/data/resources.go +++ b/tpl/data/resources.go @@ -44,7 +44,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (b if err := ns.deps.ExecHelper.Sec().CheckAllowedHTTPMethod("GET"); err != nil { return err } - + var headers bytes.Buffer req.Header.Write(&headers) id := helpers.MD5String(url + headers.String()) diff --git a/tpl/debug/debug.go b/tpl/debug/debug.go index 693b97adc..04fb71565 100644 --- a/tpl/debug/debug.go +++ b/tpl/debug/debug.go @@ -35,6 +35,6 @@ type Namespace struct { // nicely. // Also note that the output from Dump may change from Hugo version to the next, // so don't depend on a specific output. -func (ns *Namespace) Dump(val interface{}) string { +func (ns *Namespace) Dump(val any) string { return litter.Sdump(val) } diff --git a/tpl/debug/init.go b/tpl/debug/init.go index 1f032ce69..bf59ae030 100644 --- a/tpl/debug/init.go +++ b/tpl/debug/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Dump, diff --git a/tpl/diagrams/diagrams.go b/tpl/diagrams/diagrams.go index 60ebfb902..7f0a171b2 100644 --- a/tpl/diagrams/diagrams.go +++ b/tpl/diagrams/diagrams.go @@ -63,7 +63,7 @@ type Diagrams struct { d *deps.Deps } -func (d *Diagrams) Goat(v interface{}) SVGDiagram { +func (d *Diagrams) Goat(v any) SVGDiagram { var r io.Reader switch vv := v.(type) { diff --git a/tpl/diagrams/init.go b/tpl/diagrams/init.go index 1a5578837..fed85e6a3 100644 --- a/tpl/diagrams/init.go +++ b/tpl/diagrams/init.go @@ -28,7 +28,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } return ns diff --git a/tpl/encoding/encoding.go b/tpl/encoding/encoding.go index ce25151a0..272503e0c 100644 --- a/tpl/encoding/encoding.go +++ b/tpl/encoding/encoding.go @@ -33,7 +33,7 @@ func New() *Namespace { type Namespace struct{} // Base64Decode returns the base64 decoding of the given content. -func (ns *Namespace) Base64Decode(content interface{}) (string, error) { +func (ns *Namespace) Base64Decode(content any) (string, error) { conv, err := cast.ToStringE(content) if err != nil { return "", err @@ -44,7 +44,7 @@ func (ns *Namespace) Base64Decode(content interface{}) (string, error) { } // Base64Encode returns the base64 encoding of the given content. -func (ns *Namespace) Base64Encode(content interface{}) (string, error) { +func (ns *Namespace) Base64Encode(content any) (string, error) { conv, err := cast.ToStringE(content) if err != nil { return "", err @@ -58,7 +58,7 @@ func (ns *Namespace) Base64Encode(content interface{}) (string, error) { // "prefix" and "indent". Each JSON element in the output will begin on a new // line beginning with prefix followed by one or more copies of indent according // to the indentation nesting. -func (ns *Namespace) Jsonify(args ...interface{}) (template.HTML, error) { +func (ns *Namespace) Jsonify(args ...any) (template.HTML, error) { var ( b []byte err error diff --git a/tpl/encoding/encoding_test.go b/tpl/encoding/encoding_test.go index 815aa2613..e7c82e3be 100644 --- a/tpl/encoding/encoding_test.go +++ b/tpl/encoding/encoding_test.go @@ -30,8 +30,8 @@ func TestBase64Decode(t *testing.T) { ns := New() for _, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {"YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"}, // errors @@ -57,8 +57,8 @@ func TestBase64Encode(t *testing.T) { ns := New() for _, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {"YWJjMTIzIT8kKiYoKSctPUB+", "WVdKak1USXpJVDhrS2lZb0tTY3RQVUIr"}, // errors @@ -83,9 +83,9 @@ func TestJsonify(t *testing.T) { ns := New() for _, test := range []struct { - opts interface{} - v interface{} - expect interface{} + opts any + v any + expect any }{ {nil, []string{"a", "b"}, template.HTML(`["a","b"]`)}, {map[string]string{"indent": "<i>"}, []string{"a", "b"}, template.HTML("[\n<i>\"a\",\n<i>\"b\"\n]")}, @@ -97,7 +97,7 @@ func TestJsonify(t *testing.T) { {nil, math.NaN(), false}, {tstNoStringer{}, []string{"a", "b"}, false}, } { - args := []interface{}{} + args := []any{} if test.opts != nil { args = append(args, test.opts) diff --git a/tpl/encoding/init.go b/tpl/encoding/init.go index 77c9c8c49..1d42b4e37 100644 --- a/tpl/encoding/init.go +++ b/tpl/encoding/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Base64Decode, diff --git a/tpl/fmt/fmt.go b/tpl/fmt/fmt.go index cb8aa3cf2..3882d4704 100644 --- a/tpl/fmt/fmt.go +++ b/tpl/fmt/fmt.go @@ -48,23 +48,23 @@ type Namespace struct { } // Print returns string representation of the passed arguments. -func (ns *Namespace) Print(a ...interface{}) string { +func (ns *Namespace) Print(a ...any) string { return _fmt.Sprint(a...) } // Printf returns a formatted string representation of the passed arguments. -func (ns *Namespace) Printf(format string, a ...interface{}) string { +func (ns *Namespace) Printf(format string, a ...any) string { return _fmt.Sprintf(format, a...) } // Println returns string representation of the passed arguments ending with a newline. -func (ns *Namespace) Println(a ...interface{}) string { +func (ns *Namespace) Println(a ...any) string { return _fmt.Sprintln(a...) } // Errorf formats according to a format specifier and logs an ERROR. // It returns an empty string. -func (ns *Namespace) Errorf(format string, a ...interface{}) string { +func (ns *Namespace) Errorf(format string, a ...any) string { ns.distinctLogger.Errorf(format, a...) return "" } @@ -72,14 +72,14 @@ func (ns *Namespace) Errorf(format string, a ...interface{}) string { // Erroridf formats according to a format specifier and logs an ERROR and // an information text that the error with the given ID can be suppressed in config. // It returns an empty string. -func (ns *Namespace) Erroridf(id, format string, a ...interface{}) string { +func (ns *Namespace) Erroridf(id, format string, a ...any) string { ns.distinctLogger.Errorsf(id, format, a...) return "" } // Warnf formats according to a format specifier and logs a WARNING. // It returns an empty string. -func (ns *Namespace) Warnf(format string, a ...interface{}) string { +func (ns *Namespace) Warnf(format string, a ...any) string { ns.distinctLogger.Warnf(format, a...) return "" } diff --git a/tpl/fmt/init.go b/tpl/fmt/init.go index c02f985be..b0683f061 100644 --- a/tpl/fmt/init.go +++ b/tpl/fmt/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Print, diff --git a/tpl/hugo/init.go b/tpl/hugo/init.go index f2c43893e..e2b4ae7af 100644 --- a/tpl/hugo/init.go +++ b/tpl/hugo/init.go @@ -27,7 +27,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return h, nil }, + Context: func(args ...any) (any, error) { return h, nil }, } // We just add the Hugo struct as the namespace here. No method mappings. diff --git a/tpl/images/images.go b/tpl/images/images.go index 1d12aea72..aae8b3c2b 100644 --- a/tpl/images/images.go +++ b/tpl/images/images.go @@ -55,7 +55,7 @@ type Namespace struct { // Config returns the image.Config for the specified path relative to the // working directory. -func (ns *Namespace) Config(path interface{}) (image.Config, error) { +func (ns *Namespace) Config(path any) (image.Config, error) { filename, err := cast.ToStringE(path) if err != nil { return image.Config{}, err @@ -92,7 +92,7 @@ func (ns *Namespace) Config(path interface{}) (image.Config, error) { return config, nil } -func (ns *Namespace) Filter(args ...interface{}) (resource.Image, error) { +func (ns *Namespace) Filter(args ...any) (resource.Image, error) { if len(args) < 2 { return nil, errors.New("must provide an image and one or more filters") } diff --git a/tpl/images/images_test.go b/tpl/images/images_test.go index 2a2b79478..c142801b7 100644 --- a/tpl/images/images_test.go +++ b/tpl/images/images_test.go @@ -32,9 +32,9 @@ import ( type tstNoStringer struct{} var configTests = []struct { - path interface{} + path any input []byte - expect interface{} + expect any }{ { path: "a.png", diff --git a/tpl/images/init.go b/tpl/images/init.go index f3233f6e9..d9b9af4e7 100644 --- a/tpl/images/init.go +++ b/tpl/images/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Config, diff --git a/tpl/inflect/inflect.go b/tpl/inflect/inflect.go index 787e2c53b..1f887cecd 100644 --- a/tpl/inflect/inflect.go +++ b/tpl/inflect/inflect.go @@ -38,7 +38,7 @@ type Namespace struct{} // Example: "my-first-post" -> "My first post" // Example: "103" -> "103rd" // Example: 52 -> "52nd" -func (ns *Namespace) Humanize(in interface{}) (string, error) { +func (ns *Namespace) Humanize(in any) (string, error) { word, err := cast.ToStringE(in) if err != nil { return "", err @@ -59,7 +59,7 @@ func (ns *Namespace) Humanize(in interface{}) (string, error) { } // Pluralize returns the plural form of a single word. -func (ns *Namespace) Pluralize(in interface{}) (string, error) { +func (ns *Namespace) Pluralize(in any) (string, error) { word, err := cast.ToStringE(in) if err != nil { return "", err @@ -69,7 +69,7 @@ func (ns *Namespace) Pluralize(in interface{}) (string, error) { } // Singularize returns the singular form of a single word. -func (ns *Namespace) Singularize(in interface{}) (string, error) { +func (ns *Namespace) Singularize(in any) (string, error) { word, err := cast.ToStringE(in) if err != nil { return "", err diff --git a/tpl/inflect/inflect_test.go b/tpl/inflect/inflect_test.go index 7e2839a8a..083e7da4e 100644 --- a/tpl/inflect/inflect_test.go +++ b/tpl/inflect/inflect_test.go @@ -13,9 +13,9 @@ func TestInflect(t *testing.T) { ns := New() for _, test := range []struct { - fn func(i interface{}) (string, error) - in interface{} - expect interface{} + fn func(i any) (string, error) + in any + expect any }{ {ns.Humanize, "MyCamel", "My camel"}, {ns.Humanize, "óbito", "Óbito"}, diff --git a/tpl/inflect/init.go b/tpl/inflect/init.go index 548827465..a2d28f6bf 100644 --- a/tpl/inflect/init.go +++ b/tpl/inflect/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Humanize, diff --git a/tpl/internal/go_templates/texttemplate/hugo_template.go b/tpl/internal/go_templates/texttemplate/hugo_template.go index 41b43887d..dab5a05a3 100644 --- a/tpl/internal/go_templates/texttemplate/hugo_template.go +++ b/tpl/internal/go_templates/texttemplate/hugo_template.go @@ -48,7 +48,7 @@ type ExecHelper interface { // Executer executes a given template. type Executer interface { - ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data interface{}) error + ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data any) error } type executer struct { @@ -72,7 +72,7 @@ const ( ) // Note: The context is currently not fully implemeted in Hugo. This is a work in progress. -func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data interface{}) error { +func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data any) error { tmpl, err := p.Prepare() if err != nil { return err @@ -101,7 +101,7 @@ func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Wri return tmpl.executeWithState(state, value) } -func (t *executer) Execute(p Preparer, wr io.Writer, data interface{}) error { +func (t *executer) Execute(p Preparer, wr io.Writer, data any) error { tmpl, err := p.Prepare() if err != nil { return err diff --git a/tpl/internal/resourcehelpers/helpers.go b/tpl/internal/resourcehelpers/helpers.go index 4f8b7539a..d1c9bbb75 100644 --- a/tpl/internal/resourcehelpers/helpers.go +++ b/tpl/internal/resourcehelpers/helpers.go @@ -26,7 +26,7 @@ import ( ) // We allow string or a map as the first argument in some cases. -func ResolveIfFirstArgIsString(args []interface{}) (resources.ResourceTransformer, string, bool) { +func ResolveIfFirstArgIsString(args []any) (resources.ResourceTransformer, string, bool) { if len(args) != 2 { return nil, "", false } @@ -41,7 +41,7 @@ func ResolveIfFirstArgIsString(args []interface{}) (resources.ResourceTransforme } // This roundabout way of doing it is needed to get both pipeline behaviour and options as arguments. -func ResolveArgs(args []interface{}) (resources.ResourceTransformer, map[string]interface{}, error) { +func ResolveArgs(args []any) (resources.ResourceTransformer, map[string]any, error) { if len(args) == 0 { return nil, nil, errors.New("no Resource provided in transformation") } @@ -56,7 +56,7 @@ func ResolveArgs(args []interface{}) (resources.ResourceTransformer, map[string] r, ok := args[1].(resources.ResourceTransformer) if !ok { - if _, ok := args[1].(map[string]interface{}); !ok { + if _, ok := args[1].(map[string]any); !ok { return nil, nil, fmt.Errorf("no Resource provided in transformation") } return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0]) diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go index fe5dfe7ca..d06b55b4d 100644 --- a/tpl/internal/templatefuncsRegistry.go +++ b/tpl/internal/templatefuncsRegistry.go @@ -49,7 +49,7 @@ type TemplateFuncsNamespace struct { Name string // This is the method receiver. - Context func(v ...interface{}) (interface{}, error) + Context func(v ...any) (any, error) // Additional info, aliases and examples, per method name. MethodMappings map[string]TemplateFuncMethodMapping @@ -59,7 +59,7 @@ type TemplateFuncsNamespace struct { type TemplateFuncsNamespaces []*TemplateFuncsNamespace // AddMethodMapping adds a method to a template function namespace. -func (t *TemplateFuncsNamespace) AddMethodMapping(m interface{}, aliases []string, examples [][2]string) { +func (t *TemplateFuncsNamespace) AddMethodMapping(m any, aliases []string, examples [][2]string) { if t.MethodMappings == nil { t.MethodMappings = make(map[string]TemplateFuncMethodMapping) } @@ -88,7 +88,7 @@ func (t *TemplateFuncsNamespace) AddMethodMapping(m interface{}, aliases []strin // TemplateFuncMethodMapping represents a mapping of functions to methods for a // given namespace. type TemplateFuncMethodMapping struct { - Method interface{} + Method any // Any template funcs aliases. This is mainly motivated by keeping // backwards compatibility, but some new template funcs may also make @@ -104,7 +104,7 @@ type TemplateFuncMethodMapping struct { Examples [][2]string } -func methodToName(m interface{}) string { +func methodToName(m any) string { name := runtime.FuncForPC(reflect.ValueOf(m).Pointer()).Name() name = filepath.Ext(name) name = strings.TrimPrefix(name, ".") diff --git a/tpl/js/init.go b/tpl/js/init.go index 4ab8671cc..d57e0fdcb 100644 --- a/tpl/js/init.go +++ b/tpl/js/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } return ns diff --git a/tpl/js/js.go b/tpl/js/js.go index d5d435be7..bb8d20966 100644 --- a/tpl/js/js.go +++ b/tpl/js/js.go @@ -39,10 +39,10 @@ type Namespace struct { } // Build processes the given Resource with ESBuild. -func (ns *Namespace) Build(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) Build(args ...any) (resource.Resource, error) { var ( r resources.ResourceTransformer - m map[string]interface{} + m map[string]any targetPath string err error ok bool @@ -58,7 +58,7 @@ func (ns *Namespace) Build(args ...interface{}) (resource.Resource, error) { } if targetPath != "" { - m = map[string]interface{}{"targetPath": targetPath} + m = map[string]any{"targetPath": targetPath} } return ns.client.Process(r, m) diff --git a/tpl/lang/init.go b/tpl/lang/init.go index f74b6fc35..3f7b57ffc 100644 --- a/tpl/lang/init.go +++ b/tpl/lang/init.go @@ -27,7 +27,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Translate, diff --git a/tpl/lang/lang.go b/tpl/lang/lang.go index b593ccfe6..33c4c093b 100644 --- a/tpl/lang/lang.go +++ b/tpl/lang/lang.go @@ -43,8 +43,8 @@ type Namespace struct { } // Translate returns a translated string for id. -func (ns *Namespace) Translate(id interface{}, args ...interface{}) (string, error) { - var templateData interface{} +func (ns *Namespace) Translate(id any, args ...any) (string, error) { + var templateData any if len(args) > 0 { if len(args) > 1 { @@ -62,7 +62,7 @@ func (ns *Namespace) Translate(id interface{}, args ...interface{}) (string, err } // FormatNumber formats number with the given precision for the current language. -func (ns *Namespace) FormatNumber(precision, number interface{}) (string, error) { +func (ns *Namespace) FormatNumber(precision, number any) (string, error) { p, n, err := ns.castPrecisionNumber(precision, number) if err != nil { return "", err @@ -72,7 +72,7 @@ func (ns *Namespace) FormatNumber(precision, number interface{}) (string, error) // FormatPercent formats number with the given precision for the current language. // Note that the number is assumed to be a percentage. -func (ns *Namespace) FormatPercent(precision, number interface{}) (string, error) { +func (ns *Namespace) FormatPercent(precision, number any) (string, error) { p, n, err := ns.castPrecisionNumber(precision, number) if err != nil { return "", err @@ -84,7 +84,7 @@ func (ns *Namespace) FormatPercent(precision, number interface{}) (string, error // for the current language. // // The return value is formatted with at least two decimal places. -func (ns *Namespace) FormatCurrency(precision, currency, number interface{}) (string, error) { +func (ns *Namespace) FormatCurrency(precision, currency, number any) (string, error) { p, n, err := ns.castPrecisionNumber(precision, number) if err != nil { return "", err @@ -100,7 +100,7 @@ func (ns *Namespace) FormatCurrency(precision, currency, number interface{}) (st // for the current language in accounting notation. // // The return value is formatted with at least two decimal places. -func (ns *Namespace) FormatAccounting(precision, currency, number interface{}) (string, error) { +func (ns *Namespace) FormatAccounting(precision, currency, number any) (string, error) { p, n, err := ns.castPrecisionNumber(precision, number) if err != nil { return "", err @@ -112,7 +112,7 @@ func (ns *Namespace) FormatAccounting(precision, currency, number interface{}) ( return ns.translator.FmtAccounting(n, p, c), nil } -func (ns *Namespace) castPrecisionNumber(precision, number interface{}) (uint64, float64, error) { +func (ns *Namespace) castPrecisionNumber(precision, number any) (uint64, float64, error) { p, err := cast.ToUint64E(precision) if err != nil { return 0, 0, err @@ -139,7 +139,7 @@ func (ns *Namespace) castPrecisionNumber(precision, number interface{}) (uint64, // So, with precision set to 0, 1.5 becomes `2`, and 1.4 becomes `1`. // // For a simpler function that adapts to the current language, see FormatNumber. -func (ns *Namespace) FormatNumberCustom(precision, number interface{}, options ...interface{}) (string, error) { +func (ns *Namespace) FormatNumberCustom(precision, number any, options ...any) (string, error) { prec, err := cast.ToIntE(precision) if err != nil { return "", err @@ -240,16 +240,16 @@ func (ns *Namespace) FormatNumberCustom(precision, number interface{}, options . // NumFmt is deprecated, use FormatNumberCustom. // We renamed this in Hugo 0.87. // Deprecated: Use FormatNumberCustom -func (ns *Namespace) NumFmt(precision, number interface{}, options ...interface{}) (string, error) { +func (ns *Namespace) NumFmt(precision, number any, options ...any) (string, error) { return ns.FormatNumberCustom(precision, number, options...) } type pagesLanguageMerger interface { - MergeByLanguageInterface(other interface{}) (interface{}, error) + MergeByLanguageInterface(other any) (any, error) } // Merge creates a union of pages from two languages. -func (ns *Namespace) Merge(p2, p1 interface{}) (interface{}, error) { +func (ns *Namespace) Merge(p2, p1 any) (any, error) { merger, ok := p1.(pagesLanguageMerger) if !ok { return nil, fmt.Errorf("language merge not supported for %T", p1) diff --git a/tpl/math/init.go b/tpl/math/init.go index 32315d362..b4b002a78 100644 --- a/tpl/math/init.go +++ b/tpl/math/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Add, diff --git a/tpl/math/math.go b/tpl/math/math.go index cd4a47eca..fce44dc4b 100644 --- a/tpl/math/math.go +++ b/tpl/math/math.go @@ -33,12 +33,12 @@ func New() *Namespace { type Namespace struct{} // Add adds two numbers. -func (ns *Namespace) Add(a, b interface{}) (interface{}, error) { +func (ns *Namespace) Add(a, b any) (any, error) { return _math.DoArithmetic(a, b, '+') } // Ceil returns the least integer value greater than or equal to x. -func (ns *Namespace) Ceil(x interface{}) (float64, error) { +func (ns *Namespace) Ceil(x any) (float64, error) { xf, err := cast.ToFloat64E(x) if err != nil { return 0, errors.New("Ceil operator can't be used with non-float value") @@ -48,12 +48,12 @@ func (ns *Namespace) Ceil(x interface{}) (float64, error) { } // Div divides two numbers. -func (ns *Namespace) Div(a, b interface{}) (interface{}, error) { +func (ns *Namespace) Div(a, b any) (any, error) { return _math.DoArithmetic(a, b, '/') } // Floor returns the greatest integer value less than or equal to x. -func (ns *Namespace) Floor(x interface{}) (float64, error) { +func (ns *Namespace) Floor(x any) (float64, error) { xf, err := cast.ToFloat64E(x) if err != nil { return 0, errors.New("Floor operator can't be used with non-float value") @@ -63,7 +63,7 @@ func (ns *Namespace) Floor(x interface{}) (float64, error) { } // Log returns the natural logarithm of a number. -func (ns *Namespace) Log(a interface{}) (float64, error) { +func (ns *Namespace) Log(a any) (float64, error) { af, err := cast.ToFloat64E(a) if err != nil { return 0, errors.New("Log operator can't be used with non integer or float value") @@ -73,7 +73,7 @@ func (ns *Namespace) Log(a interface{}) (float64, error) { } // Max returns the greater of two numbers. -func (ns *Namespace) Max(a, b interface{}) (float64, error) { +func (ns *Namespace) Max(a, b any) (float64, error) { af, erra := cast.ToFloat64E(a) bf, errb := cast.ToFloat64E(b) @@ -85,7 +85,7 @@ func (ns *Namespace) Max(a, b interface{}) (float64, error) { } // Min returns the smaller of two numbers. -func (ns *Namespace) Min(a, b interface{}) (float64, error) { +func (ns *Namespace) Min(a, b any) (float64, error) { af, erra := cast.ToFloat64E(a) bf, errb := cast.ToFloat64E(b) @@ -97,7 +97,7 @@ func (ns *Namespace) Min(a, b interface{}) (float64, error) { } // Mod returns a % b. -func (ns *Namespace) Mod(a, b interface{}) (int64, error) { +func (ns *Namespace) Mod(a, b any) (int64, error) { ai, erra := cast.ToInt64E(a) bi, errb := cast.ToInt64E(b) @@ -113,7 +113,7 @@ func (ns *Namespace) Mod(a, b interface{}) (int64, error) { } // ModBool returns the boolean of a % b. If a % b == 0, return true. -func (ns *Namespace) ModBool(a, b interface{}) (bool, error) { +func (ns *Namespace) ModBool(a, b any) (bool, error) { res, err := ns.Mod(a, b) if err != nil { return false, err @@ -123,12 +123,12 @@ func (ns *Namespace) ModBool(a, b interface{}) (bool, error) { } // Mul multiplies two numbers. -func (ns *Namespace) Mul(a, b interface{}) (interface{}, error) { +func (ns *Namespace) Mul(a, b any) (any, error) { return _math.DoArithmetic(a, b, '*') } // Pow returns a raised to the power of b. -func (ns *Namespace) Pow(a, b interface{}) (float64, error) { +func (ns *Namespace) Pow(a, b any) (float64, error) { af, erra := cast.ToFloat64E(a) bf, errb := cast.ToFloat64E(b) @@ -140,7 +140,7 @@ func (ns *Namespace) Pow(a, b interface{}) (float64, error) { } // Round returns the nearest integer, rounding half away from zero. -func (ns *Namespace) Round(x interface{}) (float64, error) { +func (ns *Namespace) Round(x any) (float64, error) { xf, err := cast.ToFloat64E(x) if err != nil { return 0, errors.New("Round operator can't be used with non-float value") @@ -150,7 +150,7 @@ func (ns *Namespace) Round(x interface{}) (float64, error) { } // Sqrt returns the square root of a number. -func (ns *Namespace) Sqrt(a interface{}) (float64, error) { +func (ns *Namespace) Sqrt(a any) (float64, error) { af, err := cast.ToFloat64E(a) if err != nil { return 0, errors.New("Sqrt operator can't be used with non integer or float value") @@ -160,7 +160,7 @@ func (ns *Namespace) Sqrt(a interface{}) (float64, error) { } // Sub subtracts two numbers. -func (ns *Namespace) Sub(a, b interface{}) (interface{}, error) { +func (ns *Namespace) Sub(a, b any) (any, error) { return _math.DoArithmetic(a, b, '-') } diff --git a/tpl/math/math_test.go b/tpl/math/math_test.go index 45f28e093..cdec43d5f 100644 --- a/tpl/math/math_test.go +++ b/tpl/math/math_test.go @@ -27,10 +27,10 @@ func TestBasicNSArithmetic(t *testing.T) { ns := New() for _, test := range []struct { - fn func(a, b interface{}) (interface{}, error) - a interface{} - b interface{} - expect interface{} + fn func(a, b any) (any, error) + a any + b any + expect any }{ {ns.Add, 4, 2, int64(6)}, {ns.Add, 1.0, "foo", false}, @@ -60,8 +60,8 @@ func TestCeil(t *testing.T) { ns := New() for _, test := range []struct { - x interface{} - expect interface{} + x any + expect any }{ {0.1, 1.0}, {0.5, 1.0}, @@ -93,8 +93,8 @@ func TestFloor(t *testing.T) { ns := New() for _, test := range []struct { - x interface{} - expect interface{} + x any + expect any }{ {0.1, 0.0}, {0.5, 0.0}, @@ -126,8 +126,8 @@ func TestLog(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {1, float64(0)}, {3, float64(1.0986)}, @@ -167,8 +167,8 @@ func TestSqrt(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {81, float64(9)}, {0.25, float64(0.5)}, @@ -206,9 +206,9 @@ func TestMod(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - b interface{} - expect interface{} + a any + b any + expect any }{ {3, 2, int64(1)}, {3, 1, int64(0)}, @@ -246,9 +246,9 @@ func TestModBool(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - b interface{} - expect interface{} + a any + b any + expect any }{ {3, 3, true}, {3, 2, false}, @@ -292,8 +292,8 @@ func TestRound(t *testing.T) { ns := New() for _, test := range []struct { - x interface{} - expect interface{} + x any + expect any }{ {0.1, 0.0}, {0.5, 1.0}, @@ -325,9 +325,9 @@ func TestPow(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - b interface{} - expect interface{} + a any + b any + expect any }{ {0, 0, float64(1)}, {2, 0, float64(1)}, @@ -365,9 +365,9 @@ func TestMax(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - b interface{} - expect interface{} + a any + b any + expect any }{ {-1, -1, float64(-1)}, {-1, 0, float64(0)}, @@ -404,9 +404,9 @@ func TestMin(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - b interface{} - expect interface{} + a any + b any + expect any }{ {-1, -1, float64(-1)}, {-1, 0, float64(-1)}, diff --git a/tpl/openapi/openapi3/init.go b/tpl/openapi/openapi3/init.go index a0084d503..8597e3294 100644 --- a/tpl/openapi/openapi3/init.go +++ b/tpl/openapi/openapi3/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Unmarshal, diff --git a/tpl/openapi/openapi3/openapi3.go b/tpl/openapi/openapi3/openapi3.go index b4c2e64fd..276673889 100644 --- a/tpl/openapi/openapi3/openapi3.go +++ b/tpl/openapi/openapi3/openapi3.go @@ -54,7 +54,7 @@ func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*kopenapi3.T, er return nil, errors.New("no Key set in Resource") } - v, err := ns.cache.GetOrCreate(key, func() (interface{}, error) { + v, err := ns.cache.GetOrCreate(key, func() (any, error) { f := metadecoders.FormatFromMediaType(r.MediaType()) if f == "" { return nil, errors.Errorf("MIME %q not supported", r.MediaType()) diff --git a/tpl/os/init.go b/tpl/os/init.go index c25d63d56..cd9e370cd 100644 --- a/tpl/os/init.go +++ b/tpl/os/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Getenv, diff --git a/tpl/os/os.go b/tpl/os/os.go index 5abc03c68..4fa470952 100644 --- a/tpl/os/os.go +++ b/tpl/os/os.go @@ -53,7 +53,7 @@ type Namespace struct { // Getenv retrieves the value of the environment variable named by the key. // It returns the value, which will be empty if the variable is not present. -func (ns *Namespace) Getenv(key interface{}) (string, error) { +func (ns *Namespace) Getenv(key any) (string, error) { skey, err := cast.ToStringE(key) if err != nil { return "", nil @@ -85,7 +85,7 @@ func readFile(fs afero.Fs, filename string) (string, error) { // ReadFile reads the file named by filename relative to the configured WorkingDir. // It returns the contents as a string. // There is an upper size limit set at 1 megabytes. -func (ns *Namespace) ReadFile(i interface{}) (string, error) { +func (ns *Namespace) ReadFile(i any) (string, error) { s, err := cast.ToStringE(i) if err != nil { return "", err @@ -99,7 +99,7 @@ func (ns *Namespace) ReadFile(i interface{}) (string, error) { } // ReadDir lists the directory contents relative to the configured WorkingDir. -func (ns *Namespace) ReadDir(i interface{}) ([]_os.FileInfo, error) { +func (ns *Namespace) ReadDir(i any) ([]_os.FileInfo, error) { path, err := cast.ToStringE(i) if err != nil { return nil, err @@ -114,7 +114,7 @@ func (ns *Namespace) ReadDir(i interface{}) ([]_os.FileInfo, error) { } // FileExists checks whether a file exists under the given path. -func (ns *Namespace) FileExists(i interface{}) (bool, error) { +func (ns *Namespace) FileExists(i any) (bool, error) { path, err := cast.ToStringE(i) if err != nil { return false, err @@ -133,7 +133,7 @@ func (ns *Namespace) FileExists(i interface{}) (bool, error) { } // Stat returns the os.FileInfo structure describing file. -func (ns *Namespace) Stat(i interface{}) (_os.FileInfo, error) { +func (ns *Namespace) Stat(i any) (_os.FileInfo, error) { path, err := cast.ToStringE(i) if err != nil { return nil, err diff --git a/tpl/os/os_test.go b/tpl/os/os_test.go index 59491e97c..98befa061 100644 --- a/tpl/os/os_test.go +++ b/tpl/os/os_test.go @@ -34,7 +34,7 @@ func TestReadFile(t *testing.T) { for _, test := range []struct { filename string - expect interface{} + expect any }{ {filepath.FromSlash("/f/f1.txt"), "f1-content"}, {filepath.FromSlash("f/f1.txt"), "f1-content"}, @@ -64,7 +64,7 @@ func TestFileExists(t *testing.T) { for _, test := range []struct { filename string - expect interface{} + expect any }{ {filepath.FromSlash("/f/f1.txt"), true}, {filepath.FromSlash("f/f1.txt"), true}, @@ -91,7 +91,7 @@ func TestStat(t *testing.T) { for _, test := range []struct { filename string - expect interface{} + expect any }{ {filepath.FromSlash("/f/f1.txt"), int64(10)}, {filepath.FromSlash("f/f1.txt"), int64(10)}, diff --git a/tpl/partials/init.go b/tpl/partials/init.go index 1857b167a..2662b8894 100644 --- a/tpl/partials/init.go +++ b/tpl/partials/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: namespaceName, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Include, diff --git a/tpl/partials/partials.go b/tpl/partials/partials.go index b18e280ca..eb4ebfe32 100644 --- a/tpl/partials/partials.go +++ b/tpl/partials/partials.go @@ -43,7 +43,7 @@ var TestTemplateProvider deps.ResourceProvider type partialCacheKey struct { name string - variant interface{} + variant any } func (k partialCacheKey) templateName() string { @@ -56,18 +56,18 @@ func (k partialCacheKey) templateName() string { // partialCache represents a cache of partials protected by a mutex. type partialCache struct { sync.RWMutex - p map[partialCacheKey]interface{} + p map[partialCacheKey]any } func (p *partialCache) clear() { p.Lock() defer p.Unlock() - p.p = make(map[partialCacheKey]interface{}) + p.p = make(map[partialCacheKey]any) } // New returns a new instance of the templates-namespaced template functions. func New(deps *deps.Deps) *Namespace { - cache := &partialCache{p: make(map[partialCacheKey]interface{})} + cache := &partialCache{p: make(map[partialCacheKey]any)} deps.BuildStartListeners.Add( func() { cache.clear() @@ -87,12 +87,12 @@ type Namespace struct { // contextWrapper makes room for a return value in a partial invocation. type contextWrapper struct { - Arg interface{} - Result interface{} + Arg any + Result any } // Set sets the return value and returns an empty string. -func (c *contextWrapper) Set(in interface{}) string { +func (c *contextWrapper) Set(in any) string { c.Result = in return "" } @@ -102,7 +102,7 @@ func (c *contextWrapper) Set(in interface{}) string { // Else, the rendered output will be returned: // A string if the partial is a text/template, or template.HTML when html/template. // Note that ctx is provided by Hugo, not the end user. -func (ns *Namespace) Include(ctx context.Context, name string, contextList ...interface{}) (interface{}, error) { +func (ns *Namespace) Include(ctx context.Context, name string, contextList ...any) (any, error) { name, result, err := ns.include(ctx, name, contextList...) if err != nil { return result, err @@ -117,8 +117,8 @@ func (ns *Namespace) Include(ctx context.Context, name string, contextList ...in // include is a helper function that lookups and executes the named partial. // Returns the final template name and the rendered output. -func (ns *Namespace) include(ctx context.Context, name string, dataList ...interface{}) (string, interface{}, error) { - var data interface{} +func (ns *Namespace) include(ctx context.Context, name string, dataList ...any) (string, any, error) { + var data any if len(dataList) > 0 { data = dataList[0] } @@ -167,7 +167,7 @@ func (ns *Namespace) include(ctx context.Context, name string, dataList ...inter return "", nil, err } - var result interface{} + var result any if ctx, ok := data.(*contextWrapper); ok { result = ctx.Result @@ -182,7 +182,7 @@ func (ns *Namespace) include(ctx context.Context, name string, dataList ...inter // IncludeCached executes and caches partial templates. The cache is created with name+variants as the key. // Note that ctx is provided by Hugo, not the end user. -func (ns *Namespace) IncludeCached(ctx context.Context, name string, context interface{}, variants ...interface{}) (interface{}, error) { +func (ns *Namespace) IncludeCached(ctx context.Context, name string, context any, variants ...any) (any, error) { key, err := createKey(name, variants...) if err != nil { return nil, err @@ -198,8 +198,8 @@ func (ns *Namespace) IncludeCached(ctx context.Context, name string, context int return result, err } -func createKey(name string, variants ...interface{}) (partialCacheKey, error) { - var variant interface{} +func createKey(name string, variants ...any) (partialCacheKey, error) { + var variant any if len(variants) > 1 { variant = helpers.HashString(variants...) @@ -221,7 +221,7 @@ func createKey(name string, variants ...interface{}) (partialCacheKey, error) { var errUnHashable = errors.New("unhashable") -func (ns *Namespace) getOrCreate(ctx context.Context, key partialCacheKey, context interface{}) (result interface{}, err error) { +func (ns *Namespace) getOrCreate(ctx context.Context, key partialCacheKey, context any) (result any, err error) { start := time.Now() defer func() { if r := recover(); r != nil { diff --git a/tpl/partials/partials_test.go b/tpl/partials/partials_test.go index 656ab2d59..490354499 100644 --- a/tpl/partials/partials_test.go +++ b/tpl/partials/partials_test.go @@ -21,9 +21,9 @@ import ( func TestCreateKey(t *testing.T) { c := qt.New(t) - m := make(map[interface{}]bool) + m := make(map[any]bool) - create := func(name string, variants ...interface{}) partialCacheKey { + create := func(name string, variants ...any) partialCacheKey { k, err := createKey(name, variants...) c.Assert(err, qt.IsNil) m[k] = true diff --git a/tpl/path/init.go b/tpl/path/init.go index 07f20d717..c67d94866 100644 --- a/tpl/path/init.go +++ b/tpl/path/init.go @@ -29,7 +29,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Split, diff --git a/tpl/path/path.go b/tpl/path/path.go index ec50cff79..d334dd906 100644 --- a/tpl/path/path.go +++ b/tpl/path/path.go @@ -52,7 +52,7 @@ func (df DirFile) String() string { // it is empty if there is no dot. // The input path is passed into filepath.ToSlash converting any Windows slashes // to forward slashes. -func (ns *Namespace) Ext(path interface{}) (string, error) { +func (ns *Namespace) Ext(path any) (string, error) { spath, err := cast.ToStringE(path) if err != nil { return "", err @@ -70,7 +70,7 @@ func (ns *Namespace) Ext(path interface{}) (string, error) { // slash. // The input path is passed into filepath.ToSlash converting any Windows slashes // to forward slashes. -func (ns *Namespace) Dir(path interface{}) (string, error) { +func (ns *Namespace) Dir(path any) (string, error) { spath, err := cast.ToStringE(path) if err != nil { return "", err @@ -85,7 +85,7 @@ func (ns *Namespace) Dir(path interface{}) (string, error) { // If the path consists entirely of slashes, Base returns "/". // The input path is passed into filepath.ToSlash converting any Windows slashes // to forward slashes. -func (ns *Namespace) Base(path interface{}) (string, error) { +func (ns *Namespace) Base(path any) (string, error) { spath, err := cast.ToStringE(path) if err != nil { return "", err @@ -101,7 +101,7 @@ func (ns *Namespace) Base(path interface{}) (string, error) { // The input path is passed into filepath.ToSlash converting any Windows slashes // to forward slashes. // The returned values have the property that path = dir+file. -func (ns *Namespace) Split(path interface{}) (DirFile, error) { +func (ns *Namespace) Split(path any) (DirFile, error) { spath, err := cast.ToStringE(path) if err != nil { return DirFile{}, err @@ -118,7 +118,7 @@ func (ns *Namespace) Split(path interface{}) (DirFile, error) { // to forward slashes. // The result is Cleaned; in particular, // all empty strings are ignored. -func (ns *Namespace) Join(elements ...interface{}) (string, error) { +func (ns *Namespace) Join(elements ...any) (string, error) { var pathElements []string for _, elem := range elements { switch v := elem.(type) { @@ -126,7 +126,7 @@ func (ns *Namespace) Join(elements ...interface{}) (string, error) { for _, e := range v { pathElements = append(pathElements, filepath.ToSlash(e)) } - case []interface{}: + case []any: for _, e := range v { elemStr, err := cast.ToStringE(e) if err != nil { @@ -147,7 +147,7 @@ func (ns *Namespace) Join(elements ...interface{}) (string, error) { // Clean replaces the separators used with standard slashes and then // extraneous slashes are removed. -func (ns *Namespace) Clean(path interface{}) (string, error) { +func (ns *Namespace) Clean(path any) (string, error) { spath, err := cast.ToStringE(path) if err != nil { diff --git a/tpl/path/path_test.go b/tpl/path/path_test.go index d4a438b5c..c9f8469e7 100644 --- a/tpl/path/path_test.go +++ b/tpl/path/path_test.go @@ -31,8 +31,8 @@ func TestBase(t *testing.T) { c := qt.New(t) for _, test := range []struct { - path interface{} - expect interface{} + path any + expect any }{ {filepath.FromSlash(`foo/bar.txt`), `bar.txt`}, {filepath.FromSlash(`foo/bar/txt `), `txt `}, @@ -61,8 +61,8 @@ func TestDir(t *testing.T) { c := qt.New(t) for _, test := range []struct { - path interface{} - expect interface{} + path any + expect any }{ {filepath.FromSlash(`foo/bar.txt`), `foo`}, {filepath.FromSlash(`foo/bar/txt `), `foo/bar`}, @@ -91,8 +91,8 @@ func TestExt(t *testing.T) { c := qt.New(t) for _, test := range []struct { - path interface{} - expect interface{} + path any + expect any }{ {filepath.FromSlash(`foo/bar.json`), `.json`}, {`foo.bar.txt `, `.txt `}, @@ -119,21 +119,21 @@ func TestJoin(t *testing.T) { c := qt.New(t) for _, test := range []struct { - elements interface{} - expect interface{} + elements any + expect any }{ { []string{"", "baz", filepath.FromSlash(`foo/bar.txt`)}, `baz/foo/bar.txt`, }, { - []interface{}{"", "baz", DirFile{"big", "john"}, filepath.FromSlash(`foo/bar.txt`)}, + []any{"", "baz", DirFile{"big", "john"}, filepath.FromSlash(`foo/bar.txt`)}, `baz/big|john/foo/bar.txt`, }, {nil, ""}, // errors {tstNoStringer{}, false}, - {[]interface{}{"", tstNoStringer{}}, false}, + {[]any{"", tstNoStringer{}}, false}, } { result, err := ns.Join(test.elements) @@ -153,8 +153,8 @@ func TestSplit(t *testing.T) { c := qt.New(t) for _, test := range []struct { - path interface{} - expect interface{} + path any + expect any }{ {filepath.FromSlash(`foo/bar.txt`), DirFile{`foo/`, `bar.txt`}}, {filepath.FromSlash(`foo/bar/txt `), DirFile{`foo/bar/`, `txt `}}, @@ -181,8 +181,8 @@ func TestClean(t *testing.T) { c := qt.New(t) for _, test := range []struct { - path interface{} - expect interface{} + path any + expect any }{ {filepath.FromSlash(`foo/bar.txt`), `foo/bar.txt`}, {filepath.FromSlash(`foo/bar/txt`), `foo/bar/txt`}, diff --git a/tpl/reflect/init.go b/tpl/reflect/init.go index 63500a6a4..3af6dfa11 100644 --- a/tpl/reflect/init.go +++ b/tpl/reflect/init.go @@ -27,7 +27,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.IsMap, diff --git a/tpl/reflect/reflect.go b/tpl/reflect/reflect.go index 17646e9a0..07834be1c 100644 --- a/tpl/reflect/reflect.go +++ b/tpl/reflect/reflect.go @@ -26,11 +26,11 @@ func New() *Namespace { type Namespace struct{} // IsMap reports whether v is a map. -func (ns *Namespace) IsMap(v interface{}) bool { +func (ns *Namespace) IsMap(v any) bool { return reflect.ValueOf(v).Kind() == reflect.Map } // IsSlice reports whether v is a slice. -func (ns *Namespace) IsSlice(v interface{}) bool { +func (ns *Namespace) IsSlice(v any) bool { return reflect.ValueOf(v).Kind() == reflect.Slice } diff --git a/tpl/reflect/reflect_test.go b/tpl/reflect/reflect_test.go index 745360ee7..f85af87dd 100644 --- a/tpl/reflect/reflect_test.go +++ b/tpl/reflect/reflect_test.go @@ -26,8 +26,8 @@ type tstNoStringer struct{} func TestIsMap(t *testing.T) { c := qt.New(t) for _, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {map[int]int{1: 1}, true}, {"foo", false}, @@ -41,8 +41,8 @@ func TestIsMap(t *testing.T) { func TestIsSlice(t *testing.T) { c := qt.New(t) for _, test := range []struct { - v interface{} - expect interface{} + v any + expect any }{ {[]int{1, 2}, true}, {"foo", false}, diff --git a/tpl/resources/init.go b/tpl/resources/init.go index 4c8dff7bc..73a7b8f42 100644 --- a/tpl/resources/init.go +++ b/tpl/resources/init.go @@ -30,7 +30,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Get, diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index 8cd670603..f4b5b0719 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -110,7 +110,7 @@ func (ns *Namespace) getscssClientDartSass() (*dartsass.Client, error) { // Get locates the filename given in Hugo's assets filesystem and // creates a Resource object that can be used for // further transformations. -func (ns *Namespace) Get(filename interface{}) (resource.Resource, error) { +func (ns *Namespace) Get(filename any) (resource.Resource, error) { filenamestr, err := cast.ToStringE(filename) if err != nil { return nil, err @@ -125,8 +125,8 @@ func (ns *Namespace) Get(filename interface{}) (resource.Resource, error) { // // Note: This method does not return any error as a second argument, // for any error situations the error can be checked in .Err. -func (ns *Namespace) GetRemote(args ...interface{}) resource.Resource { - get := func(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) GetRemote(args ...any) resource.Resource { + get := func(args ...any) (resource.Resource, error) { if len(args) < 1 { return nil, errors.New("must provide an URL") } @@ -136,7 +136,7 @@ func (ns *Namespace) GetRemote(args ...interface{}) resource.Resource { return nil, err } - var options map[string]interface{} + var options map[string]any if len(args) > 1 { options, err = maps.ToStringMapE(args[1]) @@ -163,7 +163,7 @@ func (ns *Namespace) GetRemote(args ...interface{}) resource.Resource { // It looks for files in the assets file system. // // See Match for a more complete explanation about the rules used. -func (ns *Namespace) GetMatch(pattern interface{}) (resource.Resource, error) { +func (ns *Namespace) GetMatch(pattern any) (resource.Resource, error) { patternStr, err := cast.ToStringE(pattern) if err != nil { return nil, err @@ -188,7 +188,7 @@ func (ns *Namespace) GetMatch(pattern interface{}) (resource.Resource, error) { // It looks for files in the assets file system. // // See Match for a more complete explanation about the rules used. -func (ns *Namespace) Match(pattern interface{}) (resource.Resources, error) { +func (ns *Namespace) Match(pattern any) (resource.Resources, error) { patternStr, err := cast.ToStringE(pattern) if err != nil { return nil, err @@ -199,7 +199,7 @@ func (ns *Namespace) Match(pattern interface{}) (resource.Resources, error) { // Concat concatenates a slice of Resource objects. These resources must // (currently) be of the same Media Type. -func (ns *Namespace) Concat(targetPathIn interface{}, r interface{}) (resource.Resource, error) { +func (ns *Namespace) Concat(targetPathIn any, r any) (resource.Resource, error) { targetPath, err := cast.ToStringE(targetPathIn) if err != nil { return nil, err @@ -224,7 +224,7 @@ func (ns *Namespace) Concat(targetPathIn interface{}, r interface{}) (resource.R } // FromString creates a Resource from a string published to the relative target path. -func (ns *Namespace) FromString(targetPathIn, contentIn interface{}) (resource.Resource, error) { +func (ns *Namespace) FromString(targetPathIn, contentIn any) (resource.Resource, error) { targetPath, err := cast.ToStringE(targetPathIn) if err != nil { return nil, err @@ -239,7 +239,7 @@ func (ns *Namespace) FromString(targetPathIn, contentIn interface{}) (resource.R // ExecuteAsTemplate creates a Resource from a Go template, parsed and executed with // the given data, and published to the relative target path. -func (ns *Namespace) ExecuteAsTemplate(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) ExecuteAsTemplate(args ...any) (resource.Resource, error) { if len(args) != 3 { return nil, fmt.Errorf("must provide targetPath, the template data context and a Resource object") } @@ -259,7 +259,7 @@ func (ns *Namespace) ExecuteAsTemplate(args ...interface{}) (resource.Resource, // Fingerprint transforms the given Resource with a MD5 hash of the content in // the RelPermalink and Permalink. -func (ns *Namespace) Fingerprint(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) Fingerprint(args ...any) (resource.Resource, error) { if len(args) < 1 || len(args) > 2 { return nil, errors.New("must provide a Resource and (optional) crypto algo") } @@ -292,7 +292,7 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, // ToCSS converts the given Resource to CSS. You can optional provide an Options // object or a target path (string) as first argument. -func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) ToCSS(args ...any) (resource.Resource, error) { const ( // Transpiler implementation can be controlled from the client by // setting the 'transpiler' option. @@ -303,7 +303,7 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) { var ( r resources.ResourceTransformer - m map[string]interface{} + m map[string]any targetPath string err error ok bool @@ -346,7 +346,7 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) { } if m == nil { - m = make(map[string]interface{}) + m = make(map[string]any) } if targetPath != "" { m["targetPath"] = targetPath @@ -362,7 +362,7 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) { } // PostCSS processes the given Resource with PostCSS -func (ns *Namespace) PostCSS(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) PostCSS(args ...any) (resource.Resource, error) { r, m, err := resourcehelpers.ResolveArgs(args) if err != nil { return nil, err @@ -383,7 +383,7 @@ func (ns *Namespace) PostProcess(r resource.Resource) (postpub.PostPublishedReso } // Babel processes the given Resource with Babel. -func (ns *Namespace) Babel(args ...interface{}) (resource.Resource, error) { +func (ns *Namespace) Babel(args ...any) (resource.Resource, error) { r, m, err := resourcehelpers.ResolveArgs(args) if err != nil { return nil, err diff --git a/tpl/safe/init.go b/tpl/safe/init.go index 9fbae4044..794c9d6f0 100644 --- a/tpl/safe/init.go +++ b/tpl/safe/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.CSS, diff --git a/tpl/safe/safe.go b/tpl/safe/safe.go index 4abd34e7f..6c836b25c 100644 --- a/tpl/safe/safe.go +++ b/tpl/safe/safe.go @@ -31,43 +31,43 @@ func New() *Namespace { type Namespace struct{} // CSS returns a given string as html/template CSS content. -func (ns *Namespace) CSS(a interface{}) (template.CSS, error) { +func (ns *Namespace) CSS(a any) (template.CSS, error) { s, err := cast.ToStringE(a) return template.CSS(s), err } // HTML returns a given string as html/template HTML content. -func (ns *Namespace) HTML(a interface{}) (template.HTML, error) { +func (ns *Namespace) HTML(a any) (template.HTML, error) { s, err := cast.ToStringE(a) return template.HTML(s), err } // HTMLAttr returns a given string as html/template HTMLAttr content. -func (ns *Namespace) HTMLAttr(a interface{}) (template.HTMLAttr, error) { +func (ns *Namespace) HTMLAttr(a any) (template.HTMLAttr, error) { s, err := cast.ToStringE(a) return template.HTMLAttr(s), err } // JS returns the given string as a html/template JS content. -func (ns *Namespace) JS(a interface{}) (template.JS, error) { +func (ns *Namespace) JS(a any) (template.JS, error) { s, err := cast.ToStringE(a) return template.JS(s), err } // JSStr returns the given string as a html/template JSStr content. -func (ns *Namespace) JSStr(a interface{}) (template.JSStr, error) { +func (ns *Namespace) JSStr(a any) (template.JSStr, error) { s, err := cast.ToStringE(a) return template.JSStr(s), err } // URL returns a given string as html/template URL content. -func (ns *Namespace) URL(a interface{}) (template.URL, error) { +func (ns *Namespace) URL(a any) (template.URL, error) { s, err := cast.ToStringE(a) return template.URL(s), err } // SanitizeURL returns a given string as html/template URL content. -func (ns *Namespace) SanitizeURL(a interface{}) (string, error) { +func (ns *Namespace) SanitizeURL(a any) (string, error) { s, err := cast.ToStringE(a) return helpers.SanitizeURL(s), err } diff --git a/tpl/safe/safe_test.go b/tpl/safe/safe_test.go index e91605762..81fa40fd8 100644 --- a/tpl/safe/safe_test.go +++ b/tpl/safe/safe_test.go @@ -29,8 +29,8 @@ func TestCSS(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {`a[href =~ "//example.com"]#foo`, template.CSS(`a[href =~ "//example.com"]#foo`)}, // errors @@ -56,8 +56,8 @@ func TestHTML(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {`Hello, <b>World</b> &tc!`, template.HTML(`Hello, <b>World</b> &tc!`)}, // errors @@ -83,8 +83,8 @@ func TestHTMLAttr(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {` dir="ltr"`, template.HTMLAttr(` dir="ltr"`)}, // errors @@ -109,8 +109,8 @@ func TestJS(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {`c && alert("Hello, World!");`, template.JS(`c && alert("Hello, World!");`)}, // errors @@ -136,8 +136,8 @@ func TestJSStr(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {`Hello, World & O'Reilly\x21`, template.JSStr(`Hello, World & O'Reilly\x21`)}, // errors @@ -163,8 +163,8 @@ func TestURL(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {`greeting=H%69&addressee=(World)`, template.URL(`greeting=H%69&addressee=(World)`)}, // errors @@ -190,8 +190,8 @@ func TestSanitizeURL(t *testing.T) { ns := New() for _, test := range []struct { - a interface{} - expect interface{} + a any + expect any }{ {"http://foo/../../bar", "http://foo/bar"}, // errors diff --git a/tpl/site/init.go b/tpl/site/init.go index a24d28ad6..34ea7309f 100644 --- a/tpl/site/init.go +++ b/tpl/site/init.go @@ -27,7 +27,7 @@ func init() { s := d.Site ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return s, nil }, + Context: func(args ...any) (any, error) { return s, nil }, } if s == nil { diff --git a/tpl/strings/init.go b/tpl/strings/init.go index 384a5cda7..a11246e1c 100644 --- a/tpl/strings/init.go +++ b/tpl/strings/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Chomp, diff --git a/tpl/strings/regexp.go b/tpl/strings/regexp.go index c6d731a0d..5b6a812d4 100644 --- a/tpl/strings/regexp.go +++ b/tpl/strings/regexp.go @@ -22,7 +22,7 @@ import ( // FindRE returns a list of strings that match the regular expression. By default all matches // will be included. The number of matches can be limited with an optional third parameter. -func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface{}) ([]string, error) { +func (ns *Namespace) FindRE(expr string, content any, limit ...any) ([]string, error) { re, err := reCache.Get(expr) if err != nil { return nil, err @@ -48,7 +48,7 @@ func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface // ReplaceRE returns a copy of s, replacing all matches of the regular // expression pattern with the replacement text repl. The number of replacements // can be limited with an optional fourth parameter. -func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}, n ...interface{}) (_ string, err error) { +func (ns *Namespace) ReplaceRE(pattern, repl, s any, n ...any) (_ string, err error) { sp, err := cast.ToStringE(pattern) if err != nil { return diff --git a/tpl/strings/regexp_test.go b/tpl/strings/regexp_test.go index 433181f67..9ac098c17 100644 --- a/tpl/strings/regexp_test.go +++ b/tpl/strings/regexp_test.go @@ -25,9 +25,9 @@ func TestFindRE(t *testing.T) { for _, test := range []struct { expr string - content interface{} - limit interface{} - expect interface{} + content any + limit any + expect any }{ {"[G|g]o", "Hugo is a static site generator written in Go.", 2, []string{"go", "Go"}}, {"[G|g]o", "Hugo is a static site generator written in Go.", -1, []string{"go", "Go"}}, @@ -55,16 +55,16 @@ func TestReplaceRE(t *testing.T) { c := qt.New(t) for _, test := range []struct { - pattern interface{} - repl interface{} - s interface{} - n []interface{} - expect interface{} + pattern any + repl any + s any + n []any + expect any }{ {"^https?://([^/]+).*", "$1", "http://gohugo.io/docs", nil, "gohugo.io"}, {"^https?://([^/]+).*", "$2", "http://gohugo.io/docs", nil, ""}, {"(ab)", "AB", "aabbaab", nil, "aABbaAB"}, - {"(ab)", "AB", "aabbaab", []interface{}{1}, "aABbaab"}, + {"(ab)", "AB", "aabbaab", []any{1}, "aABbaab"}, // errors {"(ab", "AB", "aabb", nil, false}, // invalid re {tstNoStringer{}, "$2", "http://gohugo.io/docs", nil, false}, diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go index 2575b2fee..af7c34a59 100644 --- a/tpl/strings/strings.go +++ b/tpl/strings/strings.go @@ -45,7 +45,7 @@ type Namespace struct { } // CountRunes returns the number of runes in s, excluding whitespace. -func (ns *Namespace) CountRunes(s interface{}) (int, error) { +func (ns *Namespace) CountRunes(s any) (int, error) { ss, err := cast.ToStringE(s) if err != nil { return 0, _errors.Wrap(err, "Failed to convert content to string") @@ -62,7 +62,7 @@ func (ns *Namespace) CountRunes(s interface{}) (int, error) { } // RuneCount returns the number of runes in s. -func (ns *Namespace) RuneCount(s interface{}) (int, error) { +func (ns *Namespace) RuneCount(s any) (int, error) { ss, err := cast.ToStringE(s) if err != nil { return 0, _errors.Wrap(err, "Failed to convert content to string") @@ -71,7 +71,7 @@ func (ns *Namespace) RuneCount(s interface{}) (int, error) { } // CountWords returns the approximate word count in s. -func (ns *Namespace) CountWords(s interface{}) (int, error) { +func (ns *Namespace) CountWords(s any) (int, error) { ss, err := cast.ToStringE(s) if err != nil { return 0, _errors.Wrap(err, "Failed to convert content to string") @@ -101,7 +101,7 @@ func (ns *Namespace) CountWords(s interface{}) (int, error) { // Count counts the number of non-overlapping instances of substr in s. // If substr is an empty string, Count returns 1 + the number of Unicode code points in s. -func (ns *Namespace) Count(substr, s interface{}) (int, error) { +func (ns *Namespace) Count(substr, s any) (int, error) { substrs, err := cast.ToStringE(substr) if err != nil { return 0, _errors.Wrap(err, "Failed to convert substr to string") @@ -114,7 +114,7 @@ func (ns *Namespace) Count(substr, s interface{}) (int, error) { } // Chomp returns a copy of s with all trailing newline characters removed. -func (ns *Namespace) Chomp(s interface{}) (interface{}, error) { +func (ns *Namespace) Chomp(s any) (any, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -130,7 +130,7 @@ func (ns *Namespace) Chomp(s interface{}) (interface{}, error) { } // Contains reports whether substr is in s. -func (ns *Namespace) Contains(s, substr interface{}) (bool, error) { +func (ns *Namespace) Contains(s, substr any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -145,7 +145,7 @@ func (ns *Namespace) Contains(s, substr interface{}) (bool, error) { } // ContainsAny reports whether any Unicode code points in chars are within s. -func (ns *Namespace) ContainsAny(s, chars interface{}) (bool, error) { +func (ns *Namespace) ContainsAny(s, chars any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -160,7 +160,7 @@ func (ns *Namespace) ContainsAny(s, chars interface{}) (bool, error) { } // HasPrefix tests whether the input s begins with prefix. -func (ns *Namespace) HasPrefix(s, prefix interface{}) (bool, error) { +func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -175,7 +175,7 @@ func (ns *Namespace) HasPrefix(s, prefix interface{}) (bool, error) { } // HasSuffix tests whether the input s begins with suffix. -func (ns *Namespace) HasSuffix(s, suffix interface{}) (bool, error) { +func (ns *Namespace) HasSuffix(s, suffix any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -192,7 +192,7 @@ func (ns *Namespace) HasSuffix(s, suffix interface{}) (bool, error) { // Replace returns a copy of the string s with all occurrences of old replaced // with new. The number of replacements can be limited with an optional fourth // parameter. -func (ns *Namespace) Replace(s, old, new interface{}, limit ...interface{}) (string, error) { +func (ns *Namespace) Replace(s, old, new any, limit ...any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -223,7 +223,7 @@ func (ns *Namespace) Replace(s, old, new interface{}, limit ...interface{}) (str // SliceString slices a string by specifying a half-open range with // two indices, start and end. 1 and 4 creates a slice including elements 1 through 3. // The end index can be omitted, it defaults to the string's length. -func (ns *Namespace) SliceString(a interface{}, startEnd ...interface{}) (string, error) { +func (ns *Namespace) SliceString(a any, startEnd ...any) (string, error) { aStr, err := cast.ToStringE(a) if err != nil { return "", err @@ -267,7 +267,7 @@ func (ns *Namespace) SliceString(a interface{}, startEnd ...interface{}) (string } // Split slices an input string into all substrings separated by delimiter. -func (ns *Namespace) Split(a interface{}, delimiter string) ([]string, error) { +func (ns *Namespace) Split(a any, delimiter string) ([]string, error) { aStr, err := cast.ToStringE(a) if err != nil { return []string{}, err @@ -288,7 +288,7 @@ func (ns *Namespace) Split(a interface{}, delimiter string) ([]string, error) { // In addition, borrowing from the extended behavior described at http://php.net/substr, // if length is given and is negative, then that many characters will be omitted from // the end of string. -func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error) { +func (ns *Namespace) Substr(a any, nums ...any) (string, error) { s, err := cast.ToStringE(a) if err != nil { return "", err @@ -363,7 +363,7 @@ func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error) // Title returns a copy of the input s with all Unicode letters that begin words // mapped to their title case. -func (ns *Namespace) Title(s interface{}) (string, error) { +func (ns *Namespace) Title(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -373,7 +373,7 @@ func (ns *Namespace) Title(s interface{}) (string, error) { } // FirstUpper returns a string with the first character as upper case. -func (ns *Namespace) FirstUpper(s interface{}) (string, error) { +func (ns *Namespace) FirstUpper(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -384,7 +384,7 @@ func (ns *Namespace) FirstUpper(s interface{}) (string, error) { // ToLower returns a copy of the input s with all Unicode letters mapped to their // lower case. -func (ns *Namespace) ToLower(s interface{}) (string, error) { +func (ns *Namespace) ToLower(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -395,7 +395,7 @@ func (ns *Namespace) ToLower(s interface{}) (string, error) { // ToUpper returns a copy of the input s with all Unicode letters mapped to their // upper case. -func (ns *Namespace) ToUpper(s interface{}) (string, error) { +func (ns *Namespace) ToUpper(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -406,7 +406,7 @@ func (ns *Namespace) ToUpper(s interface{}) (string, error) { // Trim returns a string with all leading and trailing characters defined // contained in cutset removed. -func (ns *Namespace) Trim(s, cutset interface{}) (string, error) { +func (ns *Namespace) Trim(s, cutset any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -422,7 +422,7 @@ func (ns *Namespace) Trim(s, cutset interface{}) (string, error) { // TrimLeft returns a slice of the string s with all leading characters // contained in cutset removed. -func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) { +func (ns *Namespace) TrimLeft(cutset, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -438,7 +438,7 @@ func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) { // TrimPrefix returns s without the provided leading prefix string. If s doesn't // start with prefix, s is returned unchanged. -func (ns *Namespace) TrimPrefix(prefix, s interface{}) (string, error) { +func (ns *Namespace) TrimPrefix(prefix, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -454,7 +454,7 @@ func (ns *Namespace) TrimPrefix(prefix, s interface{}) (string, error) { // TrimRight returns a slice of the string s with all trailing characters // contained in cutset removed. -func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) { +func (ns *Namespace) TrimRight(cutset, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -470,7 +470,7 @@ func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) { // TrimSuffix returns s without the provided trailing suffix string. If s // doesn't end with suffix, s is returned unchanged. -func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) { +func (ns *Namespace) TrimSuffix(suffix, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -485,7 +485,7 @@ func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) { } // Repeat returns a new string consisting of count copies of the string s. -func (ns *Namespace) Repeat(n, s interface{}) (string, error) { +func (ns *Namespace) Repeat(n, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go index 18c033793..7e3960934 100644 --- a/tpl/strings/strings_test.go +++ b/tpl/strings/strings_test.go @@ -33,8 +33,8 @@ func TestChomp(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"\n a\n", "\n a"}, {"\n a\n\n", "\n a"}, @@ -68,8 +68,8 @@ func TestContains(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - substr interface{} + s any + substr any expect bool isErr bool }{ @@ -106,8 +106,8 @@ func TestContainsAny(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - substr interface{} + s any + substr any expect bool isErr bool }{ @@ -150,8 +150,8 @@ func TestCountRunes(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"foo bar", 6}, {"旁边", 2}, @@ -177,8 +177,8 @@ func TestRuneCount(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"foo bar", 7}, {"旁边", 2}, @@ -204,8 +204,8 @@ func TestCountWords(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"Do Be Do Be Do", 5}, {"旁边", 2}, @@ -234,9 +234,9 @@ func TestHasPrefix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - prefix interface{} - expect interface{} + s any + prefix any + expect any isErr bool }{ {"abcd", "ab", true, false}, @@ -268,9 +268,9 @@ func TestHasSuffix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - suffix interface{} - expect interface{} + s any + suffix any + expect any isErr bool }{ {"abcd", "cd", true, false}, @@ -302,11 +302,11 @@ func TestReplace(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - old interface{} - new interface{} - limit interface{} - expect interface{} + s any + old any + new any + limit any + expect any }{ {"aab", "a", "b", nil, "bbb"}, {"11a11", 1, 2, nil, "22a22"}, @@ -346,10 +346,10 @@ func TestSliceString(t *testing.T) { var err error for _, test := range []struct { - v1 interface{} - v2 interface{} - v3 interface{} - expect interface{} + v1 any + v2 any + v3 any + expect any }{ {"abc", 1, 2, "b"}, {"abc", 1, 3, "bc"}, @@ -408,9 +408,9 @@ func TestSplit(t *testing.T) { c := qt.New(t) for _, test := range []struct { - v1 interface{} + v1 any v2 string - expect interface{} + expect any }{ {"a, b", ", ", []string{"a", "b"}}, {"a & b & c", " & ", []string{"a", "b", "c"}}, @@ -437,10 +437,10 @@ func TestSubstr(t *testing.T) { var err error for _, test := range []struct { - v1 interface{} - v2 interface{} - v3 interface{} - expect interface{} + v1 any + v2 any + v3 any + expect any }{ {"abc", 1, 2, "bc"}, {"abc", 0, 1, "a"}, @@ -511,8 +511,8 @@ func TestTitle(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"test", "Test"}, {template.HTML("hypertext"), "Hypertext"}, @@ -538,8 +538,8 @@ func TestToLower(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"TEST", "test"}, {template.HTML("LoWeR"), "lower"}, @@ -565,8 +565,8 @@ func TestToUpper(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"test", "TEST"}, {template.HTML("UpPeR"), "UPPER"}, @@ -592,9 +592,9 @@ func TestTrim(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - cutset interface{} - expect interface{} + s any + cutset any + expect any }{ {"abba", "a", "bb"}, {"abba", "ab", ""}, @@ -626,9 +626,9 @@ func TestTrimLeft(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - cutset interface{} - expect interface{} + s any + cutset any + expect any }{ {"abba", "a", "bba"}, {"abba", "ab", ""}, @@ -661,9 +661,9 @@ func TestTrimPrefix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - prefix interface{} - expect interface{} + s any + prefix any + expect any }{ {"aabbaa", "a", "abbaa"}, {"aabb", "b", "aabb"}, @@ -691,9 +691,9 @@ func TestTrimRight(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - cutset interface{} - expect interface{} + s any + cutset any + expect any }{ {"abba", "a", "abb"}, {"abba", "ab", ""}, @@ -726,9 +726,9 @@ func TestTrimSuffix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - suffix interface{} - expect interface{} + s any + suffix any + expect any }{ {"aabbaa", "a", "aabba"}, {"aabb", "b", "aab"}, @@ -756,9 +756,9 @@ func TestRepeat(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - n interface{} - expect interface{} + s any + n any + expect any }{ {"yo", "2", "yoyo"}, {"~", "16", "~~~~~~~~~~~~~~~~"}, diff --git a/tpl/strings/truncate.go b/tpl/strings/truncate.go index 6e3a50ed2..dd6267280 100644 --- a/tpl/strings/truncate.go +++ b/tpl/strings/truncate.go @@ -40,12 +40,12 @@ type htmlTag struct { } // Truncate truncates a given string to the specified length. -func (ns *Namespace) Truncate(a interface{}, options ...interface{}) (template.HTML, error) { +func (ns *Namespace) Truncate(a any, options ...any) (template.HTML, error) { length, err := cast.ToIntE(a) if err != nil { return "", err } - var textParam interface{} + var textParam any var ellipsis string switch len(options) { diff --git a/tpl/strings/truncate_test.go b/tpl/strings/truncate_test.go index 51743e935..f7d5d132d 100644 --- a/tpl/strings/truncate_test.go +++ b/tpl/strings/truncate_test.go @@ -25,10 +25,10 @@ func TestTruncate(t *testing.T) { var err error cases := []struct { - v1 interface{} - v2 interface{} - v3 interface{} - want interface{} + v1 any + v2 any + v3 any + want any isErr bool }{ {10, "I am a test sentence", nil, template.HTML("I am a …"), false}, diff --git a/tpl/template.go b/tpl/template.go index 1d8c98ded..299b7208d 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -53,8 +53,8 @@ type UnusedTemplatesProvider interface { // TemplateHandler finds and executes templates. type TemplateHandler interface { TemplateFinder - Execute(t Template, wr io.Writer, data interface{}) error - ExecuteWithContext(ctx context.Context, t Template, wr io.Writer, data interface{}) error + Execute(t Template, wr io.Writer, data any) error + ExecuteWithContext(ctx context.Context, t Template, wr io.Writer, data any) error LookupLayout(d output.LayoutDescriptor, f output.Format) (Template, bool, error) HasTemplate(name string) bool } @@ -149,7 +149,7 @@ type TemplateFuncGetter interface { // GetDataFromContext returns the template data context (usually .Page) from ctx if set. // NOte: This is not fully implemented yet. -func GetDataFromContext(ctx context.Context) interface{} { +func GetDataFromContext(ctx context.Context) any { return ctx.Value(texttemplate.DataContextKey) } diff --git a/tpl/templates/init.go b/tpl/templates/init.go index 8da8440ae..e068fca81 100644 --- a/tpl/templates/init.go +++ b/tpl/templates/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Exists, diff --git a/tpl/time/init.go b/tpl/time/init.go index 0ef4fcdfd..a76348b7a 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -32,7 +32,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { + Context: func(args ...any) (any, error) { // Handle overlapping "time" namespace and func. // // If no args are passed to `time`, assume namespace usage and diff --git a/tpl/time/time.go b/tpl/time/time.go index 21ebae4f8..66ea721fa 100644 --- a/tpl/time/time.go +++ b/tpl/time/time.go @@ -42,7 +42,7 @@ type Namespace struct { // AsTime converts the textual representation of the datetime string into // a time.Time interface. -func (ns *Namespace) AsTime(v interface{}, args ...interface{}) (interface{}, error) { +func (ns *Namespace) AsTime(v any, args ...any) (any, error) { loc := ns.location if len(args) > 0 { locStr, err := cast.ToStringE(args[0]) @@ -62,7 +62,7 @@ func (ns *Namespace) AsTime(v interface{}, args ...interface{}) (interface{}, er // Format converts the textual representation of the datetime string into // the other form or returns it of the time.Time value. These are formatted // with the layout string -func (ns *Namespace) Format(layout string, v interface{}) (string, error) { +func (ns *Namespace) Format(layout string, v any) (string, error) { t, err := htime.ToTimeInDefaultLocationE(v, ns.location) if err != nil { return "", err @@ -82,7 +82,7 @@ func (ns *Namespace) Now() _time.Time { // such as "300ms", "-1.5h" or "2h45m". // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". // See https://golang.org/pkg/time/#ParseDuration -func (ns *Namespace) ParseDuration(in interface{}) (_time.Duration, error) { +func (ns *Namespace) ParseDuration(in any) (_time.Duration, error) { s, err := cast.ToStringE(in) if err != nil { return 0, err @@ -109,7 +109,7 @@ var durationUnits = map[string]_time.Duration{ // Duration converts the given number to a time.Duration. // Unit is one of nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m or hour/h. -func (ns *Namespace) Duration(unit interface{}, number interface{}) (_time.Duration, error) { +func (ns *Namespace) Duration(unit any, number any) (_time.Duration, error) { unitStr, err := cast.ToStringE(unit) if err != nil { return 0, err diff --git a/tpl/time/time_test.go b/tpl/time/time_test.go index 0aeaed579..f368ea43f 100644 --- a/tpl/time/time_test.go +++ b/tpl/time/time_test.go @@ -32,8 +32,8 @@ func TestTimeLocation(t *testing.T) { for i, test := range []struct { name string value string - location interface{} - expect interface{} + location any + expect any }{ {"Empty location", "2020-10-20", "", "2020-10-20 00:00:00 +0000 UTC"}, {"New location", "2020-10-20", nil, "2020-10-20 00:00:00 -0400 AST"}, @@ -53,7 +53,7 @@ func TestTimeLocation(t *testing.T) { {"Invalid time value", "invalid-value", "", false}, } { t.Run(test.name, func(t *testing.T) { - var args []interface{} + var args []any if test.location != nil { args = append(args, test.location) } @@ -90,8 +90,8 @@ func TestFormat(t *testing.T) { for i, test := range []struct { layout string - value interface{} - expect interface{} + value any + expect any }{ {"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"}, {"Monday, Jan 2, 2006", time.Date(2015, time.January, 21, 0, 0, 0, 0, time.UTC), "Wednesday, Jan 21, 2015"}, @@ -146,9 +146,9 @@ func TestDuration(t *testing.T) { ns := New(translators.GetTranslator("en"), time.UTC) for i, test := range []struct { - unit interface{} - num interface{} - expect interface{} + unit any + num any + expect any }{ {"nanosecond", 10, 10 * time.Nanosecond}, {"ns", 10, 10 * time.Nanosecond}, diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 706719278..4cee19a20 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -118,7 +118,7 @@ func newIdentity(name string) identity.Manager { return identity.NewManager(identity.NewPathIdentity(files.ComponentFolderLayouts, name)) } -func newStandaloneTextTemplate(funcs map[string]interface{}) tpl.TemplateParseFinder { +func newStandaloneTextTemplate(funcs map[string]any) tpl.TemplateParseFinder { return &textTemplateWrapperWithLock{ RWMutex: &sync.RWMutex{}, Template: texttemplate.New("").Funcs(funcs), @@ -127,7 +127,7 @@ func newStandaloneTextTemplate(funcs map[string]interface{}) tpl.TemplateParseFi func newTemplateExec(d *deps.Deps) (*templateExec, error) { exec, funcs := newTemplateExecuter(d) - funcMap := make(map[string]interface{}) + funcMap := make(map[string]any) for k, v := range funcs { funcMap[k] = v.Interface() } @@ -184,7 +184,7 @@ func newTemplateExec(d *deps.Deps) (*templateExec, error) { return e, nil } -func newTemplateNamespace(funcs map[string]interface{}) *templateNamespace { +func newTemplateNamespace(funcs map[string]any) *templateNamespace { return &templateNamespace{ prototypeHTML: htmltemplate.New("").Funcs(funcs), prototypeText: texttemplate.New("").Funcs(funcs), @@ -225,11 +225,11 @@ func (t templateExec) Clone(d *deps.Deps) *templateExec { return &t } -func (t *templateExec) Execute(templ tpl.Template, wr io.Writer, data interface{}) error { +func (t *templateExec) Execute(templ tpl.Template, wr io.Writer, data any) error { return t.ExecuteWithContext(context.Background(), templ, wr, data) } -func (t *templateExec) ExecuteWithContext(ctx context.Context, templ tpl.Template, wr io.Writer, data interface{}) error { +func (t *templateExec) ExecuteWithContext(ctx context.Context, templ tpl.Template, wr io.Writer, data any) error { if rlocker, ok := templ.(types.RLocker); ok { rlocker.RLock() defer rlocker.RUnlock() diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go index 56d970b23..90ca325ab 100644 --- a/tpl/tplimpl/template_ast_transformers_test.go +++ b/tpl/tplimpl/template_ast_transformers_test.go @@ -88,7 +88,7 @@ func TestCollectInfo(t *testing.T) { {"Basic config map", "{{ $_hugo_config := `" + configStr + "` }}", tpl.ParseInfo{Config: tpl.ParseConfig{Version: 42}}}, } - echo := func(in interface{}) interface{} { + echo := func(in any) any { return in } @@ -129,7 +129,7 @@ func TestPartialReturn(t *testing.T) { `, true}, } - echo := func(in interface{}) interface{} { + echo := func(in any) any { return in } diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 0ecd5df46..e664bd6c5 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -174,7 +174,7 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec ), funcsv } -func createFuncMap(d *deps.Deps) map[string]interface{} { +func createFuncMap(d *deps.Deps) map[string]any { funcMap := template.FuncMap{} // Merge the namespace funcs diff --git a/tpl/transform/init.go b/tpl/transform/init.go index aa7297bc4..5c6d7da5c 100644 --- a/tpl/transform/init.go +++ b/tpl/transform/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Emojify, diff --git a/tpl/transform/remarshal.go b/tpl/transform/remarshal.go index d9b6829a0..b5d682144 100644 --- a/tpl/transform/remarshal.go +++ b/tpl/transform/remarshal.go @@ -17,8 +17,8 @@ import ( // It is not a general purpose YAML to TOML converter etc., and may // change without notice if it serves a purpose in the docs. // Format is one of json, yaml or toml. -func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) { - var meta map[string]interface{} +func (ns *Namespace) Remarshal(format string, data any) (string, error) { + var meta map[string]any format = strings.TrimSpace(strings.ToLower(format)) @@ -27,7 +27,7 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) return "", err } - if m, ok := data.(map[string]interface{}); ok { + if m, ok := data.(map[string]any); ok { meta = m } else { from, err := cast.ToStringE(data) @@ -65,10 +65,10 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) // The unmarshal/marshal dance is extremely type lossy, and we need // to make sure that integer types prints as "43" and not "43.0" in // all formats, hence this hack. -func applyMarshalTypes(m map[string]interface{}) { +func applyMarshalTypes(m map[string]any) { for k, v := range m { switch t := v.(type) { - case map[string]interface{}: + case map[string]any: applyMarshalTypes(t) case float64: i := int64(t) diff --git a/tpl/transform/remarshal_test.go b/tpl/transform/remarshal_test.go index 22548593b..5262db591 100644 --- a/tpl/transform/remarshal_test.go +++ b/tpl/transform/remarshal_test.go @@ -184,7 +184,7 @@ a = "b" }) c.Run("Map input", func(c *qt.C) { - input := map[string]interface{}{ + input := map[string]any{ "hello": "world", } diff --git a/tpl/transform/transform.go b/tpl/transform/transform.go index 498c0d674..2f62557de 100644 --- a/tpl/transform/transform.go +++ b/tpl/transform/transform.go @@ -51,7 +51,7 @@ type Namespace struct { // Emojify returns a copy of s with all emoji codes replaced with actual emojis. // // See http://www.emoji-cheat-sheet.com/ -func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) { +func (ns *Namespace) Emojify(s any) (template.HTML, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -62,13 +62,13 @@ func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) { // Highlight returns a copy of s as an HTML string with syntax // highlighting applied. -func (ns *Namespace) Highlight(s interface{}, lang string, opts ...interface{}) (template.HTML, error) { +func (ns *Namespace) Highlight(s any, lang string, opts ...any) (template.HTML, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err } - var optsv interface{} + var optsv any if len(opts) > 0 { optsv = opts[0] } @@ -79,8 +79,8 @@ func (ns *Namespace) Highlight(s interface{}, lang string, opts ...interface{}) } // HighlightCodeBlock highlights a code block on the form received in the codeblock render hooks. -func (ns *Namespace) HighlightCodeBlock(ctx hooks.CodeblockContext, opts ...interface{}) (highlight.HightlightResult, error) { - var optsv interface{} +func (ns *Namespace) HighlightCodeBlock(ctx hooks.CodeblockContext, opts ...any) (highlight.HightlightResult, error) { + var optsv any if len(opts) > 0 { optsv = opts[0] } @@ -96,7 +96,7 @@ func (ns *Namespace) CanHighlight(lang string) bool { } // HTMLEscape returns a copy of s with reserved HTML characters escaped. -func (ns *Namespace) HTMLEscape(s interface{}) (string, error) { +func (ns *Namespace) HTMLEscape(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -107,7 +107,7 @@ func (ns *Namespace) HTMLEscape(s interface{}) (string, error) { // HTMLUnescape returns a copy of with HTML escape requences converted to plain // text. -func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) { +func (ns *Namespace) HTMLUnescape(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -117,7 +117,7 @@ func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) { } // Markdownify renders a given input from Markdown to HTML. -func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) { +func (ns *Namespace) Markdownify(s any) (template.HTML, error) { home := ns.deps.Site.Home() if home == nil { @@ -135,7 +135,7 @@ func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) { } // Plainify returns a copy of s with all HTML tags removed. -func (ns *Namespace) Plainify(s interface{}) (string, error) { +func (ns *Namespace) Plainify(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go index e52e0046d..ab2fd3b9e 100644 --- a/tpl/transform/transform_test.go +++ b/tpl/transform/transform_test.go @@ -42,8 +42,8 @@ func TestEmojify(t *testing.T) { ns := transform.New(b.H.Deps) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {":notamoji:", template.HTML(":notamoji:")}, {"I :heart: Hugo", template.HTML("I ❤️ Hugo")}, @@ -72,10 +72,10 @@ func TestHighlight(t *testing.T) { ns := transform.New(b.H.Deps) for _, test := range []struct { - s interface{} + s any lang string - opts interface{} - expect interface{} + opts any + expect any }{ {"func boo() {}", "go", "", "boo"}, {"func boo() {}", "go", nil, "boo"}, @@ -117,8 +117,8 @@ func TestHTMLEscape(t *testing.T) { ns := transform.New(b.H.Deps) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, {"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, @@ -147,8 +147,8 @@ func TestHTMLUnescape(t *testing.T) { ns := transform.New(b.H.Deps) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, {"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, @@ -177,8 +177,8 @@ func TestMarkdownify(t *testing.T) { ns := transform.New(b.H.Deps) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"Hello **World!**", template.HTML("Hello <strong>World!</strong>")}, {[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")}, @@ -233,8 +233,8 @@ func TestPlainify(t *testing.T) { ns := transform.New(b.H.Deps) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"<em>Note:</em> blah <b>blah</b>", "Note: blah blah"}, // errors diff --git a/tpl/transform/unmarshal.go b/tpl/transform/unmarshal.go index c59269577..62682557c 100644 --- a/tpl/transform/unmarshal.go +++ b/tpl/transform/unmarshal.go @@ -33,18 +33,18 @@ import ( // Unmarshal unmarshals the data given, which can be either a string, json.RawMessage // or a Resource. Supported formats are JSON, TOML, YAML, and CSV. // You can optionally provide an options map as the first argument. -func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { +func (ns *Namespace) Unmarshal(args ...any) (any, error) { if len(args) < 1 || len(args) > 2 { return nil, errors.New("unmarshal takes 1 or 2 arguments") } - var data interface{} + var data any decoder := metadecoders.Default if len(args) == 1 { data = args[0] } else { - m, ok := args[0].(map[string]interface{}) + m, ok := args[0].(map[string]any) if !ok { return nil, errors.New("first argument must be a map") } @@ -69,7 +69,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { key += decoder.OptionsKey() } - return ns.cache.GetOrCreate(key, func() (interface{}, error) { + return ns.cache.GetOrCreate(key, func() (any, error) { f := metadecoders.FormatFromMediaType(r.MediaType()) if f == "" { return nil, errors.Errorf("MIME %q not supported", r.MediaType()) @@ -101,7 +101,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { key := helpers.MD5String(dataStr) - return ns.cache.GetOrCreate(key, func() (interface{}, error) { + return ns.cache.GetOrCreate(key, func() (any, error) { f := decoder.FormatFromContentString(dataStr) if f == "" { return nil, errors.New("unknown format") @@ -111,7 +111,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { }) } -func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) { +func decodeDecoder(m map[string]any) (metadecoders.Decoder, error) { opts := metadecoders.Default if m == nil { @@ -144,7 +144,7 @@ func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) { return opts, err } -func stringToRune(v interface{}) (rune, error) { +func stringToRune(v any) (rune, error) { s, err := cast.ToStringE(v) if err != nil { return 0, err diff --git a/tpl/transform/unmarshal_test.go b/tpl/transform/unmarshal_test.go index 2b14282ec..e63f96de2 100644 --- a/tpl/transform/unmarshal_test.go +++ b/tpl/transform/unmarshal_test.go @@ -87,34 +87,34 @@ func TestUnmarshal(t *testing.T) { ns := transform.New(b.H.Deps) - assertSlogan := func(m map[string]interface{}) { + assertSlogan := func(m map[string]any) { b.Assert(m["slogan"], qt.Equals, "Hugo Rocks!") } for _, test := range []struct { - data interface{} - options interface{} - expect interface{} + data any + options any + expect any }{ - {`{ "slogan": "Hugo Rocks!" }`, nil, func(m map[string]interface{}) { + {`{ "slogan": "Hugo Rocks!" }`, nil, func(m map[string]any) { assertSlogan(m) }}, - {`slogan: "Hugo Rocks!"`, nil, func(m map[string]interface{}) { + {`slogan: "Hugo Rocks!"`, nil, func(m map[string]any) { assertSlogan(m) }}, - {`slogan = "Hugo Rocks!"`, nil, func(m map[string]interface{}) { + {`slogan = "Hugo Rocks!"`, nil, func(m map[string]any) { assertSlogan(m) }}, - {testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `slogan: "Hugo Rocks!"`, mime: media.YAMLType}, nil, func(m map[string]any) { assertSlogan(m) }}, - {testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `{ "slogan": "Hugo Rocks!" }`, mime: media.JSONType}, nil, func(m map[string]any) { assertSlogan(m) }}, - {testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `slogan = "Hugo Rocks!"`, mime: media.TOMLType}, nil, func(m map[string]any) { assertSlogan(m) }}, - {testContentResource{key: "r1", content: `<root><slogan>Hugo Rocks!</slogan></root>"`, mime: media.XMLType}, nil, func(m map[string]interface{}) { + {testContentResource{key: "r1", content: `<root><slogan>Hugo Rocks!</slogan></root>"`, mime: media.XMLType}, nil, func(m map[string]any) { assertSlogan(m) }}, {testContentResource{key: "r1", content: `1997,Ford,E350,"ac, abs, moon",3000.00 @@ -124,18 +124,18 @@ func TestUnmarshal(t *testing.T) { b.Assert(len(first), qt.Equals, 5) b.Assert(first[1], qt.Equals, "Ford") }}, - {testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"delimiter": ";"}, func(r [][]string) { + {testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]any{"delimiter": ";"}, func(r [][]string) { b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) }}, {"a,b,c", nil, func(r [][]string) { b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) }}, - {"a;b;c", map[string]interface{}{"delimiter": ";"}, func(r [][]string) { + {"a;b;c", map[string]any{"delimiter": ";"}, func(r [][]string) { b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) }}, {testContentResource{key: "r1", content: ` % This is a comment -a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment": "%"}, func(r [][]string) { +a;b;c`, mime: media.CSVType}, map[string]any{"DElimiter": ";", "Comment": "%"}, func(r [][]string) { b.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) }}, // errors @@ -149,21 +149,21 @@ a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment" ns.Reset() - var args []interface{} + var args []any if test.options != nil { - args = []interface{}{test.options, test.data} + args = []any{test.options, test.data} } else { - args = []interface{}{test.data} + args = []any{test.data} } result, err := ns.Unmarshal(args...) if bb, ok := test.expect.(bool); ok && !bb { b.Assert(err, qt.Not(qt.IsNil)) - } else if fn, ok := test.expect.(func(m map[string]interface{})); ok { + } else if fn, ok := test.expect.(func(m map[string]any)); ok { b.Assert(err, qt.IsNil) - m, ok := result.(map[string]interface{}) + m, ok := result.(map[string]any) b.Assert(ok, qt.Equals, true) fn(m) } else if fn, ok := test.expect.(func(r [][]string)); ok { diff --git a/tpl/urls/init.go b/tpl/urls/init.go index 0a97045e2..3597e87c5 100644 --- a/tpl/urls/init.go +++ b/tpl/urls/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.AbsURL, diff --git a/tpl/urls/urls.go b/tpl/urls/urls.go index 1bba636ad..e07106261 100644 --- a/tpl/urls/urls.go +++ b/tpl/urls/urls.go @@ -41,7 +41,7 @@ type Namespace struct { } // AbsURL takes a given string and converts it to an absolute URL. -func (ns *Namespace) AbsURL(a interface{}) (template.HTML, error) { +func (ns *Namespace) AbsURL(a any) (template.HTML, error) { s, err := cast.ToStringE(a) if err != nil { return "", nil @@ -52,7 +52,7 @@ func (ns *Namespace) AbsURL(a interface{}) (template.HTML, error) { // Parse parses rawurl into a URL structure. The rawurl may be relative or // absolute. -func (ns *Namespace) Parse(rawurl interface{}) (*url.URL, error) { +func (ns *Namespace) Parse(rawurl any) (*url.URL, error) { s, err := cast.ToStringE(rawurl) if err != nil { return nil, _errors.Wrap(err, "Error in Parse") @@ -63,7 +63,7 @@ func (ns *Namespace) Parse(rawurl interface{}) (*url.URL, error) { // RelURL takes a given string and prepends the relative path according to a // page's position in the project directory structure. -func (ns *Namespace) RelURL(a interface{}) (template.HTML, error) { +func (ns *Namespace) RelURL(a any) (template.HTML, error) { s, err := cast.ToStringE(a) if err != nil { return "", nil @@ -73,7 +73,7 @@ func (ns *Namespace) RelURL(a interface{}) (template.HTML, error) { } // URLize returns the given argument formatted as URL. -func (ns *Namespace) URLize(a interface{}) (string, error) { +func (ns *Namespace) URLize(a any) (string, error) { s, err := cast.ToStringE(a) if err != nil { return "", nil @@ -82,7 +82,7 @@ func (ns *Namespace) URLize(a interface{}) (string, error) { } // Anchorize creates sanitized anchor names that are compatible with Blackfriday. -func (ns *Namespace) Anchorize(a interface{}) (string, error) { +func (ns *Namespace) Anchorize(a any) (string, error) { s, err := cast.ToStringE(a) if err != nil { return "", nil @@ -91,7 +91,7 @@ func (ns *Namespace) Anchorize(a interface{}) (string, error) { } // Ref returns the absolute URL path to a given content item. -func (ns *Namespace) Ref(in interface{}, args interface{}) (template.HTML, error) { +func (ns *Namespace) Ref(in any, args any) (template.HTML, error) { p, ok := in.(urls.RefLinker) if !ok { return "", errors.New("invalid Page received in Ref") @@ -105,7 +105,7 @@ func (ns *Namespace) Ref(in interface{}, args interface{}) (template.HTML, error } // RelRef returns the relative URL path to a given content item. -func (ns *Namespace) RelRef(in interface{}, args interface{}) (template.HTML, error) { +func (ns *Namespace) RelRef(in any, args any) (template.HTML, error) { p, ok := in.(urls.RefLinker) if !ok { return "", errors.New("invalid Page received in RelRef") @@ -119,22 +119,22 @@ func (ns *Namespace) RelRef(in interface{}, args interface{}) (template.HTML, er return template.HTML(s), err } -func (ns *Namespace) refArgsToMap(args interface{}) (map[string]interface{}, error) { +func (ns *Namespace) refArgsToMap(args any) (map[string]any, error) { var ( s string of string ) v := args - if _, ok := v.([]interface{}); ok { + if _, ok := v.([]any); ok { v = cast.ToStringSlice(v) } switch v := v.(type) { - case map[string]interface{}: + case map[string]any: return v, nil case map[string]string: - m := make(map[string]interface{}) + m := make(map[string]any) for k, v := range v { m[k] = v } @@ -157,7 +157,7 @@ func (ns *Namespace) refArgsToMap(args interface{}) (map[string]interface{}, err } - return map[string]interface{}{ + return map[string]any{ "path": s, "outputFormat": of, }, nil @@ -165,7 +165,7 @@ func (ns *Namespace) refArgsToMap(args interface{}) (map[string]interface{}, err // RelLangURL takes a given string and prepends the relative path according to a // page's position in the project directory structure and the current language. -func (ns *Namespace) RelLangURL(a interface{}) (template.HTML, error) { +func (ns *Namespace) RelLangURL(a any) (template.HTML, error) { s, err := cast.ToStringE(a) if err != nil { return "", err @@ -177,7 +177,7 @@ func (ns *Namespace) RelLangURL(a interface{}) (template.HTML, error) { // AbsLangURL takes a given string and converts it to an absolute URL according // to a page's position in the project directory structure and the current // language. -func (ns *Namespace) AbsLangURL(a interface{}) (template.HTML, error) { +func (ns *Namespace) AbsLangURL(a any) (template.HTML, error) { s, err := cast.ToStringE(a) if err != nil { return "", err diff --git a/tpl/urls/urls_test.go b/tpl/urls/urls_test.go index 23068c243..73b5cd141 100644 --- a/tpl/urls/urls_test.go +++ b/tpl/urls/urls_test.go @@ -34,8 +34,8 @@ func TestParse(t *testing.T) { c := qt.New(t) for _, test := range []struct { - rawurl interface{} - expect interface{} + rawurl any + expect any }{ { "http://www.google.com", |