aboutsummaryrefslogtreecommitdiffhomepage
path: root/common/maps
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2022-03-17 22:03:27 +0100
committerBjørn Erik Pedersen <[email protected]>2022-03-17 22:03:27 +0100
commitb80853de90b10171155b8f3fde47d64ec7bfa0dd (patch)
tree435d3dbf7a495a0c6ce64c9769e037179aa0d27b /common/maps
parent423594e03a906ef4150f433666ff588b022c3c92 (diff)
downloadhugo-b80853de90b10171155b8f3fde47d64ec7bfa0dd.tar.gz
hugo-b80853de90b10171155b8f3fde47d64ec7bfa0dd.zip
all: gofmt -w -r 'interface{} -> any' .
Updates #9687
Diffstat (limited to 'common/maps')
-rw-r--r--common/maps/maps.go38
-rw-r--r--common/maps/maps_test.go42
-rw-r--r--common/maps/params.go22
-rw-r--r--common/maps/params_test.go14
-rw-r--r--common/maps/scratch.go28
-rw-r--r--common/maps/scratch_test.go8
6 files changed, 76 insertions, 76 deletions
diff --git a/common/maps/maps.go b/common/maps/maps.go
index b5e9ba2f5..5552da55d 100644
--- a/common/maps/maps.go
+++ b/common/maps/maps.go
@@ -24,12 +24,12 @@ import (
)
// ToStringMapE converts in to map[string]interface{}.
-func ToStringMapE(in interface{}) (map[string]interface{}, error) {
+func ToStringMapE(in any) (map[string]any, error) {
switch vv := in.(type) {
case Params:
return vv, nil
case map[string]string:
- var m = map[string]interface{}{}
+ var m = map[string]any{}
for k, v := range vv {
m[k] = v
}
@@ -43,7 +43,7 @@ func ToStringMapE(in interface{}) (map[string]interface{}, error) {
// ToParamsAndPrepare converts in to Params and prepares it for use.
// If in is nil, an empty map is returned.
// See PrepareParams.
-func ToParamsAndPrepare(in interface{}) (Params, bool) {
+func ToParamsAndPrepare(in any) (Params, bool) {
if types.IsNil(in) {
return Params{}, true
}
@@ -56,7 +56,7 @@ func ToParamsAndPrepare(in interface{}) (Params, bool) {
}
// MustToParamsAndPrepare calls ToParamsAndPrepare and panics if it fails.
-func MustToParamsAndPrepare(in interface{}) Params {
+func MustToParamsAndPrepare(in any) Params {
if p, ok := ToParamsAndPrepare(in); ok {
return p
} else {
@@ -65,13 +65,13 @@ func MustToParamsAndPrepare(in interface{}) Params {
}
// ToStringMap converts in to map[string]interface{}.
-func ToStringMap(in interface{}) map[string]interface{} {
+func ToStringMap(in any) map[string]any {
m, _ := ToStringMapE(in)
return m
}
// ToStringMapStringE converts in to map[string]string.
-func ToStringMapStringE(in interface{}) (map[string]string, error) {
+func ToStringMapStringE(in any) (map[string]string, error) {
m, err := ToStringMapE(in)
if err != nil {
return nil, err
@@ -80,26 +80,26 @@ func ToStringMapStringE(in interface{}) (map[string]string, error) {
}
// ToStringMapString converts in to map[string]string.
-func ToStringMapString(in interface{}) map[string]string {
+func ToStringMapString(in any) map[string]string {
m, _ := ToStringMapStringE(in)
return m
}
// ToStringMapBool converts in to bool.
-func ToStringMapBool(in interface{}) map[string]bool {
+func ToStringMapBool(in any) map[string]bool {
m, _ := ToStringMapE(in)
return cast.ToStringMapBool(m)
}
// ToSliceStringMap converts in to []map[string]interface{}.
-func ToSliceStringMap(in interface{}) ([]map[string]interface{}, error) {
+func ToSliceStringMap(in any) ([]map[string]any, error) {
switch v := in.(type) {
- case []map[string]interface{}:
+ case []map[string]any:
return v, nil
- case []interface{}:
- var s []map[string]interface{}
+ case []any:
+ var s []map[string]any
for _, entry := range v {
- if vv, ok := entry.(map[string]interface{}); ok {
+ if vv, ok := entry.(map[string]any); ok {
s = append(s, vv)
}
}
@@ -146,7 +146,7 @@ func (r KeyRenamer) getNewKey(keyPath string) string {
// Rename renames the keys in the given map according
// to the patterns in the current KeyRenamer.
-func (r KeyRenamer) Rename(m map[string]interface{}) {
+func (r KeyRenamer) Rename(m map[string]any) {
r.renamePath("", m)
}
@@ -158,15 +158,15 @@ func (KeyRenamer) keyPath(k1, k2 string) string {
return k1 + "/" + k2
}
-func (r KeyRenamer) renamePath(parentKeyPath string, m map[string]interface{}) {
+func (r KeyRenamer) renamePath(parentKeyPath string, m map[string]any) {
for key, val := range m {
keyPath := r.keyPath(parentKeyPath, key)
switch val.(type) {
- case map[interface{}]interface{}:
+ case map[any]any:
val = cast.ToStringMap(val)
- r.renamePath(keyPath, val.(map[string]interface{}))
- case map[string]interface{}:
- r.renamePath(keyPath, val.(map[string]interface{}))
+ r.renamePath(keyPath, val.(map[string]any))
+ case map[string]any:
+ r.renamePath(keyPath, val.(map[string]any))
}
newKey := r.getNewKey(keyPath)
diff --git a/common/maps/maps_test.go b/common/maps/maps_test.go
index f0c32b9fe..53120dce7 100644
--- a/common/maps/maps_test.go
+++ b/common/maps/maps_test.go
@@ -27,7 +27,7 @@ func TestPrepareParams(t *testing.T) {
expected Params
}{
{
- map[string]interface{}{
+ map[string]any{
"abC": 32,
},
Params{
@@ -35,16 +35,16 @@ func TestPrepareParams(t *testing.T) {
},
},
{
- map[string]interface{}{
+ map[string]any{
"abC": 32,
- "deF": map[interface{}]interface{}{
+ "deF": map[any]any{
23: "A value",
- 24: map[string]interface{}{
+ 24: map[string]any{
"AbCDe": "A value",
"eFgHi": "Another value",
},
},
- "gHi": map[string]interface{}{
+ "gHi": map[string]any{
"J": 25,
},
"jKl": map[string]string{
@@ -85,23 +85,23 @@ func TestToSliceStringMap(t *testing.T) {
c := qt.New(t)
tests := []struct {
- input interface{}
- expected []map[string]interface{}
+ input any
+ expected []map[string]any
}{
{
- input: []map[string]interface{}{
+ input: []map[string]any{
{"abc": 123},
},
- expected: []map[string]interface{}{
+ expected: []map[string]any{
{"abc": 123},
},
}, {
- input: []interface{}{
- map[string]interface{}{
+ input: []any{
+ map[string]any{
"def": 456,
},
},
- expected: []map[string]interface{}{
+ expected: []map[string]any{
{"def": 456},
},
},
@@ -116,7 +116,7 @@ func TestToSliceStringMap(t *testing.T) {
func TestToParamsAndPrepare(t *testing.T) {
c := qt.New(t)
- _, ok := ToParamsAndPrepare(map[string]interface{}{"A": "av"})
+ _, ok := ToParamsAndPrepare(map[string]any{"A": "av"})
c.Assert(ok, qt.IsTrue)
params, ok := ToParamsAndPrepare(nil)
@@ -127,33 +127,33 @@ func TestToParamsAndPrepare(t *testing.T) {
func TestRenameKeys(t *testing.T) {
c := qt.New(t)
- m := map[string]interface{}{
+ m := map[string]any{
"a": 32,
"ren1": "m1",
"ren2": "m1_2",
- "sub": map[string]interface{}{
- "subsub": map[string]interface{}{
+ "sub": map[string]any{
+ "subsub": map[string]any{
"REN1": "m2",
"ren2": "m2_2",
},
},
- "no": map[string]interface{}{
+ "no": map[string]any{
"ren1": "m2",
"ren2": "m2_2",
},
}
- expected := map[string]interface{}{
+ expected := map[string]any{
"a": 32,
"new1": "m1",
"new2": "m1_2",
- "sub": map[string]interface{}{
- "subsub": map[string]interface{}{
+ "sub": map[string]any{
+ "subsub": map[string]any{
"new1": "m2",
"ren2": "m2_2",
},
},
- "no": map[string]interface{}{
+ "no": map[string]any{
"ren1": "m2",
"ren2": "m2_2",
},
diff --git a/common/maps/params.go b/common/maps/params.go
index 15511f56f..60fe56668 100644
--- a/common/maps/params.go
+++ b/common/maps/params.go
@@ -21,11 +21,11 @@ import (
)
// Params is a map where all keys are lower case.
-type Params map[string]interface{}
+type Params map[string]any
// Get does a lower case and nested search in this map.
// It will return nil if none found.
-func (p Params) Get(indices ...string) interface{} {
+func (p Params) Get(indices ...string) any {
v, _, _ := getNested(p, indices)
return v
}
@@ -142,7 +142,7 @@ func (p Params) SetDefaultMergeStrategy(s ParamsMergeStrategy) {
p[mergeStrategyKey] = s
}
-func getNested(m map[string]interface{}, indices []string) (interface{}, string, map[string]interface{}) {
+func getNested(m map[string]any, indices []string) (any, string, map[string]any) {
if len(indices) == 0 {
return nil, "", nil
}
@@ -164,7 +164,7 @@ func getNested(m map[string]interface{}, indices []string) (interface{}, string,
switch m2 := v.(type) {
case Params:
return getNested(m2, indices[1:])
- case map[string]interface{}:
+ case map[string]any:
return getNested(m2, indices[1:])
default:
return nil, "", nil
@@ -175,7 +175,7 @@ func getNested(m map[string]interface{}, indices []string) (interface{}, string,
// It will first try the exact match and then try to find it as a nested map value,
// using the given separator, e.g. "mymap.name".
// It assumes that all the maps given have lower cased keys.
-func GetNestedParam(keyStr, separator string, candidates ...Params) (interface{}, error) {
+func GetNestedParam(keyStr, separator string, candidates ...Params) (any, error) {
keyStr = strings.ToLower(keyStr)
// Try exact match first
@@ -195,7 +195,7 @@ func GetNestedParam(keyStr, separator string, candidates ...Params) (interface{}
return nil, nil
}
-func GetNestedParamFn(keyStr, separator string, lookupFn func(key string) interface{}) (interface{}, string, map[string]interface{}, error) {
+func GetNestedParamFn(keyStr, separator string, lookupFn func(key string) any) (any, string, map[string]any, error) {
keySegments := strings.Split(keyStr, separator)
if len(keySegments) == 0 {
return nil, "", nil, nil
@@ -211,7 +211,7 @@ func GetNestedParamFn(keyStr, separator string, lookupFn func(key string) interf
}
switch m := first.(type) {
- case map[string]interface{}:
+ case map[string]any:
v, key, owner := getNested(m, keySegments[1:])
return v, key, owner, nil
case Params:
@@ -236,7 +236,7 @@ const (
mergeStrategyKey = "_merge"
)
-func toMergeStrategy(v interface{}) ParamsMergeStrategy {
+func toMergeStrategy(v any) ParamsMergeStrategy {
s := ParamsMergeStrategy(cast.ToString(v))
switch s {
case ParamsMergeStrategyDeep, ParamsMergeStrategyNone, ParamsMergeStrategyShallow:
@@ -260,13 +260,13 @@ func PrepareParams(m Params) {
retyped = true
} else {
switch vv := v.(type) {
- case map[interface{}]interface{}:
+ case map[any]any:
var p Params = cast.ToStringMap(v)
v = p
PrepareParams(p)
retyped = true
- case map[string]interface{}:
- var p Params = v.(map[string]interface{})
+ case map[string]any:
+ var p Params = v.(map[string]any)
v = p
PrepareParams(p)
retyped = true
diff --git a/common/maps/params_test.go b/common/maps/params_test.go
index 5c799aae1..a070e6f60 100644
--- a/common/maps/params_test.go
+++ b/common/maps/params_test.go
@@ -20,13 +20,13 @@ import (
)
func TestGetNestedParam(t *testing.T) {
- m := map[string]interface{}{
+ m := map[string]any{
"string": "value",
"first": 1,
"with_underscore": 2,
- "nested": map[string]interface{}{
+ "nested": map[string]any{
"color": "blue",
- "nestednested": map[string]interface{}{
+ "nestednested": map[string]any{
"color": "green",
},
},
@@ -34,7 +34,7 @@ func TestGetNestedParam(t *testing.T) {
c := qt.New(t)
- must := func(keyStr, separator string, candidates ...Params) interface{} {
+ must := func(keyStr, separator string, candidates ...Params) any {
v, err := GetNestedParam(keyStr, separator, candidates...)
c.Assert(err, qt.IsNil)
return v
@@ -53,14 +53,14 @@ func TestGetNestedParam(t *testing.T) {
func TestGetNestedParamFnNestedNewKey(t *testing.T) {
c := qt.New(t)
- nested := map[string]interface{}{
+ nested := map[string]any{
"color": "blue",
}
- m := map[string]interface{}{
+ m := map[string]any{
"nested": nested,
}
- existing, nestedKey, owner, err := GetNestedParamFn("nested.new", ".", func(key string) interface{} {
+ existing, nestedKey, owner, err := GetNestedParamFn("nested.new", ".", func(key string) any {
return m[key]
})
diff --git a/common/maps/scratch.go b/common/maps/scratch.go
index 26ffef7d2..d4745d27c 100644
--- a/common/maps/scratch.go
+++ b/common/maps/scratch.go
@@ -24,7 +24,7 @@ import (
// Scratch is a writable context used for stateful operations in Page/Node rendering.
type Scratch struct {
- values map[string]interface{}
+ values map[string]any
mu sync.RWMutex
}
@@ -50,8 +50,8 @@ func NewScratcher() Scratcher {
// Supports numeric values and strings.
//
// If the first add for a key is an array or slice, then the next value(s) will be appended.
-func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
- var newVal interface{}
+func (c *Scratch) Add(key string, newAddend any) (string, error) {
+ var newVal any
c.mu.RLock()
existingAddend, found := c.values[key]
c.mu.RUnlock()
@@ -82,7 +82,7 @@ func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
// Set stores a value with the given key in the Node context.
// This value can later be retrieved with Get.
-func (c *Scratch) Set(key string, value interface{}) string {
+func (c *Scratch) Set(key string, value any) string {
c.mu.Lock()
c.values[key] = value
c.mu.Unlock()
@@ -98,7 +98,7 @@ func (c *Scratch) Delete(key string) string {
}
// Get returns a value previously set by Add or Set.
-func (c *Scratch) Get(key string) interface{} {
+func (c *Scratch) Get(key string) any {
c.mu.RLock()
val := c.values[key]
c.mu.RUnlock()
@@ -109,7 +109,7 @@ func (c *Scratch) Get(key string) interface{} {
// Values returns the raw backing map. Note that you should just use
// this method on the locally scoped Scratch instances you obtain via newScratch, not
// .Page.Scratch etc., as that will lead to concurrency issues.
-func (c *Scratch) Values() map[string]interface{} {
+func (c *Scratch) Values() map[string]any {
c.mu.RLock()
defer c.mu.RUnlock()
return c.values
@@ -117,14 +117,14 @@ func (c *Scratch) Values() map[string]interface{} {
// SetInMap stores a value to a map with the given key in the Node context.
// This map can later be retrieved with GetSortedMapValues.
-func (c *Scratch) SetInMap(key string, mapKey string, value interface{}) string {
+func (c *Scratch) SetInMap(key string, mapKey string, value any) string {
c.mu.Lock()
_, found := c.values[key]
if !found {
- c.values[key] = make(map[string]interface{})
+ c.values[key] = make(map[string]any)
}
- c.values[key].(map[string]interface{})[mapKey] = value
+ c.values[key].(map[string]any)[mapKey] = value
c.mu.Unlock()
return ""
}
@@ -134,14 +134,14 @@ func (c *Scratch) DeleteInMap(key string, mapKey string) string {
c.mu.Lock()
_, found := c.values[key]
if found {
- delete(c.values[key].(map[string]interface{}), mapKey)
+ delete(c.values[key].(map[string]any), mapKey)
}
c.mu.Unlock()
return ""
}
// GetSortedMapValues returns a sorted map previously filled with SetInMap.
-func (c *Scratch) GetSortedMapValues(key string) interface{} {
+func (c *Scratch) GetSortedMapValues(key string) any {
c.mu.RLock()
if c.values[key] == nil {
@@ -149,7 +149,7 @@ func (c *Scratch) GetSortedMapValues(key string) interface{} {
return nil
}
- unsortedMap := c.values[key].(map[string]interface{})
+ unsortedMap := c.values[key].(map[string]any)
c.mu.RUnlock()
var keys []string
for mapKey := range unsortedMap {
@@ -158,7 +158,7 @@ func (c *Scratch) GetSortedMapValues(key string) interface{} {
sort.Strings(keys)
- sortedArray := make([]interface{}, len(unsortedMap))
+ sortedArray := make([]any, len(unsortedMap))
for i, mapKey := range keys {
sortedArray[i] = unsortedMap[mapKey]
}
@@ -168,5 +168,5 @@ func (c *Scratch) GetSortedMapValues(key string) interface{} {
// NewScratch returns a new instance of Scratch.
func NewScratch() *Scratch {
- return &Scratch{values: make(map[string]interface{})}
+ return &Scratch{values: make(map[string]any)}
}
diff --git a/common/maps/scratch_test.go b/common/maps/scratch_test.go
index 96b352572..b515adb1d 100644
--- a/common/maps/scratch_test.go
+++ b/common/maps/scratch_test.go
@@ -90,7 +90,7 @@ func TestScratchAddTypedSliceToInterfaceSlice(t *testing.T) {
c := qt.New(t)
scratch := NewScratch()
- scratch.Set("slice", []interface{}{})
+ scratch.Set("slice", []any{})
_, err := scratch.Add("slice", []int{1, 2})
c.Assert(err, qt.IsNil)
@@ -107,7 +107,7 @@ func TestScratchAddDifferentTypedSliceToInterfaceSlice(t *testing.T) {
_, err := scratch.Add("slice", []int{1, 2})
c.Assert(err, qt.IsNil)
- c.Assert(scratch.Get("slice"), qt.DeepEquals, []interface{}{"foo", 1, 2})
+ c.Assert(scratch.Get("slice"), qt.DeepEquals, []any{"foo", 1, 2})
}
func TestScratchSet(t *testing.T) {
@@ -185,7 +185,7 @@ func TestScratchSetInMap(t *testing.T) {
scratch.SetInMap("key", "zyx", "Zyx")
scratch.SetInMap("key", "abc", "Abc (updated)")
scratch.SetInMap("key", "def", "Def")
- c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []interface{}{0: "Abc (updated)", 1: "Def", 2: "Lux", 3: "Zyx"})
+ c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []any{0: "Abc (updated)", 1: "Def", 2: "Lux", 3: "Zyx"})
}
func TestScratchDeleteInMap(t *testing.T) {
@@ -199,7 +199,7 @@ func TestScratchDeleteInMap(t *testing.T) {
scratch.DeleteInMap("key", "abc")
scratch.SetInMap("key", "def", "Def")
scratch.DeleteInMap("key", "lmn") // Do nothing
- c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []interface{}{0: "Def", 1: "Lux", 2: "Zyx"})
+ c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []any{0: "Def", 1: "Lux", 2: "Zyx"})
}
func TestScratchGetSortedMapValues(t *testing.T) {