chore: change interface{} to any (#2818)

* chore: change interface{} to any

* chore: update goctl version to 1.5.0

* chore: update goctl deps
This commit is contained in:
Kevin Wan
2023-01-24 16:32:02 +08:00
committed by GitHub
parent 7e0ac77139
commit ae87114282
221 changed files with 1910 additions and 2207 deletions

View File

@@ -11,17 +11,17 @@ const jsonTagKey = "json"
var jsonUnmarshaler = NewUnmarshaler(jsonTagKey)
// UnmarshalJsonBytes unmarshals content into v.
func UnmarshalJsonBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalJsonBytes(content []byte, v any, opts ...UnmarshalOption) error {
return unmarshalJsonBytes(content, v, getJsonUnmarshaler(opts...))
}
// UnmarshalJsonMap unmarshals content from m into v.
func UnmarshalJsonMap(m map[string]interface{}, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalJsonMap(m map[string]any, v any, opts ...UnmarshalOption) error {
return getJsonUnmarshaler(opts...).Unmarshal(m, v)
}
// UnmarshalJsonReader unmarshals content from reader into v.
func UnmarshalJsonReader(reader io.Reader, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalJsonReader(reader io.Reader, v any, opts ...UnmarshalOption) error {
return unmarshalJsonReader(reader, v, getJsonUnmarshaler(opts...))
}
@@ -33,8 +33,8 @@ func getJsonUnmarshaler(opts ...UnmarshalOption) *Unmarshaler {
return jsonUnmarshaler
}
func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
var m interface{}
func unmarshalJsonBytes(content []byte, v any, unmarshaler *Unmarshaler) error {
var m any
if err := jsonx.Unmarshal(content, &m); err != nil {
return err
}
@@ -42,8 +42,8 @@ func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler)
return unmarshaler.Unmarshal(m, v)
}
func unmarshalJsonReader(reader io.Reader, v interface{}, unmarshaler *Unmarshaler) error {
var m interface{}
func unmarshalJsonReader(reader io.Reader, v any, unmarshaler *Unmarshaler) error {
var m any
if err := jsonx.UnmarshalFromReader(reader, &m); err != nil {
return err
}

View File

@@ -871,7 +871,7 @@ func TestUnmarshalReaderError(t *testing.T) {
func TestUnmarshalMap(t *testing.T) {
t.Run("nil map and valid", func(t *testing.T) {
var m map[string]interface{}
var m map[string]any
var v struct {
Any string `json:",optional"`
}
@@ -882,7 +882,7 @@ func TestUnmarshalMap(t *testing.T) {
})
t.Run("empty map but not valid", func(t *testing.T) {
m := map[string]interface{}{}
m := map[string]any{}
var v struct {
Any string
}
@@ -892,7 +892,7 @@ func TestUnmarshalMap(t *testing.T) {
})
t.Run("empty map and valid", func(t *testing.T) {
m := map[string]interface{}{}
m := map[string]any{}
var v struct {
Any string `json:",optional"`
}
@@ -905,7 +905,7 @@ func TestUnmarshalMap(t *testing.T) {
})
t.Run("valid map", func(t *testing.T) {
m := map[string]interface{}{
m := map[string]any{
"Any": "foo",
}
var v struct {

View File

@@ -13,8 +13,8 @@ const (
// Marshal marshals the given val and returns the map that contains the fields.
// optional=another is not implemented, and it's hard to implement and not common used.
func Marshal(val interface{}) (map[string]map[string]interface{}, error) {
ret := make(map[string]map[string]interface{})
func Marshal(val any) (map[string]map[string]any, error) {
ret := make(map[string]map[string]any)
tp := reflect.TypeOf(val)
if tp.Kind() == reflect.Ptr {
tp = tp.Elem()
@@ -45,7 +45,7 @@ func getTag(field reflect.StructField) (string, bool) {
}
func processMember(field reflect.StructField, value reflect.Value,
collector map[string]map[string]interface{}) error {
collector map[string]map[string]any) error {
var key string
var opt *fieldOptions
var err error
@@ -73,7 +73,7 @@ func processMember(field reflect.StructField, value reflect.Value,
if ok {
m[key] = val
} else {
m = map[string]interface{}{
m = map[string]any{
key: val,
}
}

View File

@@ -227,7 +227,7 @@ func TestMarshal_Range(t *testing.T) {
}
func TestMarshal_RangeOut(t *testing.T) {
tests := []interface{}{
tests := []any{
struct {
Int int `json:"int,range=[1:3]"`
}{
@@ -262,7 +262,7 @@ func TestMarshal_RangeOut(t *testing.T) {
}
func TestMarshal_RangeIllegal(t *testing.T) {
tests := []interface{}{
tests := []any{
struct {
Int int `json:"int,range=[3:1]"`
}{
@@ -284,7 +284,7 @@ func TestMarshal_RangeIllegal(t *testing.T) {
func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
tests := []struct {
name string
value interface{}
value any
err error
}{
{

View File

@@ -7,7 +7,7 @@ import (
)
// UnmarshalTomlBytes unmarshals TOML bytes into the given v.
func UnmarshalTomlBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalTomlBytes(content []byte, v any, opts ...UnmarshalOption) error {
b, err := encoding.TomlToJson(content)
if err != nil {
return err
@@ -17,7 +17,7 @@ func UnmarshalTomlBytes(content []byte, v interface{}, opts ...UnmarshalOption)
}
// UnmarshalTomlReader unmarshals TOML from the given io.Reader into the given v.
func UnmarshalTomlReader(r io.Reader, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalTomlReader(r io.Reader, v any, opts ...UnmarshalOption) error {
b, err := io.ReadAll(r)
if err != nil {
return err

View File

@@ -30,9 +30,9 @@ var (
durationType = reflect.TypeOf(time.Duration(0))
cacheKeys = make(map[string][]string)
cacheKeysLock sync.Mutex
defaultCache = make(map[string]interface{})
defaultCache = make(map[string]any)
defaultCacheLock sync.Mutex
emptyMap = map[string]interface{}{}
emptyMap = map[string]any{}
emptyValue = reflect.ValueOf(lang.Placeholder)
)
@@ -66,12 +66,12 @@ func NewUnmarshaler(key string, opts ...UnmarshalOption) *Unmarshaler {
}
// UnmarshalKey unmarshals m into v with tag key.
func UnmarshalKey(m map[string]interface{}, v interface{}) error {
func UnmarshalKey(m map[string]any, v any) error {
return keyUnmarshaler.Unmarshal(m, v)
}
// Unmarshal unmarshals m into v.
func (u *Unmarshaler) Unmarshal(i interface{}, v interface{}) error {
func (u *Unmarshaler) Unmarshal(i any, v any) error {
valueType := reflect.TypeOf(v)
if valueType.Kind() != reflect.Ptr {
return errValueNotSettable
@@ -79,13 +79,13 @@ func (u *Unmarshaler) Unmarshal(i interface{}, v interface{}) error {
elemType := Deref(valueType)
switch iv := i.(type) {
case map[string]interface{}:
case map[string]any:
if elemType.Kind() != reflect.Struct {
return errTypeMismatch
}
return u.UnmarshalValuer(mapValuer(iv), v)
case []interface{}:
case []any:
if elemType.Kind() != reflect.Slice {
return errTypeMismatch
}
@@ -97,11 +97,11 @@ func (u *Unmarshaler) Unmarshal(i interface{}, v interface{}) error {
}
// UnmarshalValuer unmarshals m into v.
func (u *Unmarshaler) UnmarshalValuer(m Valuer, v interface{}) error {
func (u *Unmarshaler) UnmarshalValuer(m Valuer, v any) error {
return u.unmarshalWithFullName(simpleValuer{current: m}, v, "")
}
func (u *Unmarshaler) fillMap(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
func (u *Unmarshaler) fillMap(fieldType reflect.Type, value reflect.Value, mapValue any) error {
if !value.CanSet() {
return errValueNotSettable
}
@@ -121,7 +121,7 @@ func (u *Unmarshaler) fillMap(fieldType reflect.Type, value reflect.Value, mapVa
return nil
}
func (u *Unmarshaler) fillMapFromString(value reflect.Value, mapValue interface{}) error {
func (u *Unmarshaler) fillMapFromString(value reflect.Value, mapValue any) error {
if !value.CanSet() {
return errValueNotSettable
}
@@ -142,7 +142,7 @@ func (u *Unmarshaler) fillMapFromString(value reflect.Value, mapValue interface{
return nil
}
func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, mapValue any) error {
if !value.CanSet() {
return errValueNotSettable
}
@@ -172,7 +172,7 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
switch dereffedBaseKind {
case reflect.Struct:
target := reflect.New(dereffedBaseType)
if err := u.Unmarshal(ithValue.(map[string]interface{}), target.Interface()); err != nil {
if err := u.Unmarshal(ithValue.(map[string]any), target.Interface()); err != nil {
return err
}
@@ -196,8 +196,8 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
}
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value,
mapValue interface{}) error {
var slice []interface{}
mapValue any) error {
var slice []any
switch v := mapValue.(type) {
case fmt.Stringer:
if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil {
@@ -226,14 +226,14 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.
}
func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int,
baseKind reflect.Kind, value interface{}) error {
baseKind reflect.Kind, value any) error {
ithVal := slice.Index(index)
switch v := value.(type) {
case fmt.Stringer:
return setValueFromString(baseKind, ithVal, v.String())
case string:
return setValueFromString(baseKind, ithVal, v)
case map[string]interface{}:
case map[string]any:
return u.fillMap(ithVal.Type(), ithVal, value)
default:
// don't need to consider the difference between int, int8, int16, int32, int64,
@@ -281,7 +281,7 @@ func (u *Unmarshaler) fillSliceWithDefault(derefedType reflect.Type, value refle
return u.fillSlice(derefedType, value, slice)
}
func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue interface{}) (reflect.Value, error) {
func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue any) (reflect.Value, error) {
mapType := reflect.MapOf(keyType, elemType)
valueType := reflect.TypeOf(mapValue)
if mapType == valueType {
@@ -306,7 +306,7 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
targetValue.SetMapIndex(key, target.Elem())
case reflect.Struct:
keythMap, ok := keythData.(map[string]interface{})
keythMap, ok := keythData.(map[string]any)
if !ok {
return emptyValue, errTypeMismatch
}
@@ -318,7 +318,7 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
SetMapIndexValue(elemType, targetValue, key, target.Elem())
case reflect.Map:
keythMap, ok := keythData.(map[string]interface{})
keythMap, ok := keythData.(map[string]any)
if !ok {
return emptyValue, errTypeMismatch
}
@@ -513,7 +513,7 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
switch {
case valueKind == reflect.Map && typeKind == reflect.Struct:
mv, ok := mapValue.(map[string]interface{})
mv, ok := mapValue.(map[string]any)
if !ok {
return errTypeMismatch
}
@@ -536,7 +536,7 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
}
func (u *Unmarshaler) processFieldPrimitive(fieldType reflect.Type, value reflect.Value,
mapValue interface{}, opts *fieldOptionsWithContext, fullName string) error {
mapValue any, opts *fieldOptionsWithContext, fullName string) error {
typeKind := Deref(fieldType).Kind()
valueKind := reflect.TypeOf(mapValue).Kind()
@@ -631,7 +631,7 @@ func (u *Unmarshaler) processFieldStruct(fieldType reflect.Type, value reflect.V
}
func (u *Unmarshaler) processFieldTextUnmarshaler(fieldType reflect.Type, value reflect.Value,
mapValue interface{}) (bool, error) {
mapValue any) (bool, error) {
var tval encoding.TextUnmarshaler
var ok bool
@@ -756,7 +756,7 @@ func (u *Unmarshaler) processNamedFieldWithValue(fieldType reflect.Type, value r
}
func (u *Unmarshaler) processNamedFieldWithValueFromString(fieldType reflect.Type, value reflect.Value,
mapValue interface{}, key string, opts *fieldOptionsWithContext, fullName string) error {
mapValue any, key string, opts *fieldOptionsWithContext, fullName string) error {
valueKind := reflect.TypeOf(mapValue).Kind()
if valueKind != reflect.String {
return fmt.Errorf("the value in map is not string, but %s", valueKind)
@@ -832,7 +832,7 @@ func (u *Unmarshaler) processNamedFieldWithoutValue(fieldType reflect.Type, valu
return nil
}
func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v interface{}, fullName string) error {
func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v any, fullName string) error {
rv := reflect.ValueOf(v)
if err := ValidatePtr(&rv); err != nil {
return err
@@ -900,7 +900,7 @@ func fillDurationValue(fieldType reflect.Type, value reflect.Value, dur string)
return nil
}
func fillPrimitive(fieldType reflect.Type, value reflect.Value, mapValue interface{},
func fillPrimitive(fieldType reflect.Type, value reflect.Value, mapValue any,
opts *fieldOptionsWithContext, fullName string) error {
if !value.CanSet() {
return errValueNotSettable
@@ -929,7 +929,7 @@ func fillPrimitive(fieldType reflect.Type, value reflect.Value, mapValue interfa
}
}
func fillWithSameType(fieldType reflect.Type, value reflect.Value, mapValue interface{},
func fillWithSameType(fieldType reflect.Type, value reflect.Value, mapValue any,
opts *fieldOptionsWithContext) error {
if !value.CanSet() {
return errValueNotSettable
@@ -952,12 +952,12 @@ func fillWithSameType(fieldType reflect.Type, value reflect.Value, mapValue inte
}
// getValue gets the value for the specific key, the key can be in the format of parentKey.childKey
func getValue(m valuerWithParent, key string) (interface{}, bool) {
func getValue(m valuerWithParent, key string) (any, bool) {
keys := readKeys(key)
return getValueWithChainedKeys(m, keys)
}
func getValueWithChainedKeys(m valuerWithParent, keys []string) (interface{}, bool) {
func getValueWithChainedKeys(m valuerWithParent, keys []string) (any, bool) {
switch len(keys) {
case 0:
return nil, false
@@ -966,7 +966,7 @@ func getValueWithChainedKeys(m valuerWithParent, keys []string) (interface{}, bo
return v, ok
default:
if v, ok := m.Value(keys[0]); ok {
if nextm, ok := v.(map[string]interface{}); ok {
if nextm, ok := v.(map[string]any); ok {
return getValueWithChainedKeys(recursiveValuer{
current: mapValuer(nextm),
parent: m,
@@ -1025,7 +1025,7 @@ func readKeys(key string) []string {
return keys
}
func setSameKindValue(targetType reflect.Type, target reflect.Value, value interface{}) {
func setSameKindValue(targetType reflect.Type, target reflect.Value, value any) {
if reflect.ValueOf(value).Type().AssignableTo(targetType) {
target.Set(reflect.ValueOf(value))
} else {

File diff suppressed because it is too large Load Diff

View File

@@ -64,7 +64,7 @@ func Deref(t reflect.Type) reflect.Type {
}
// Repr returns the string representation of v.
func Repr(v interface{}) string {
func Repr(v any) string {
return lang.Repr(v)
}
@@ -89,7 +89,7 @@ func ValidatePtr(v *reflect.Value) error {
return nil
}
func convertTypeFromString(kind reflect.Kind, str string) (interface{}, error) {
func convertTypeFromString(kind reflect.Kind, str string) (any, error) {
switch kind {
case reflect.Bool:
switch strings.ToLower(str) {
@@ -484,7 +484,7 @@ func parseSegments(val string) []string {
return segments
}
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interface{}) error {
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v any) error {
switch kind {
case reflect.Bool:
value.SetBool(v.(bool))
@@ -536,7 +536,7 @@ func structValueRequired(tag string, tp reflect.Type) (bool, error) {
return required, err
}
func toFloat64(v interface{}) (float64, bool) {
func toFloat64(v any) (float64, bool) {
switch val := v.(type) {
case int:
return float64(val), true
@@ -623,7 +623,7 @@ func validateNumberRange(fv float64, nr *numberRange) error {
return nil
}
func validateValueInOptions(val interface{}, options []string) error {
func validateValueInOptions(val any, options []string) error {
if len(options) > 0 {
switch v := val.(type) {
case string:
@@ -640,7 +640,7 @@ func validateValueInOptions(val interface{}, options []string) error {
return nil
}
func validateValueRange(mapValue interface{}, opts *fieldOptionsWithContext) error {
func validateValueRange(mapValue any, opts *fieldOptionsWithContext) error {
if opts == nil || opts.Range == nil {
return nil
}

View File

@@ -258,7 +258,7 @@ func TestSetValueFormatErrors(t *testing.T) {
IntValue int
UintValue uint
FloatValue float32
MapValue map[string]interface{}
MapValue map[string]any
}
var bar Bar

View File

@@ -4,7 +4,7 @@ type (
// A Valuer interface defines the way to get values from the underlying object with keys.
Valuer interface {
// Value gets the value associated with the given key.
Value(key string) (interface{}, bool)
Value(key string) (any, bool)
}
// A valuerWithParent defines a node that has a parent node.
@@ -22,12 +22,12 @@ type (
// A valueWithParent is used to wrap the value with its parent.
valueWithParent struct {
value interface{}
value any
parent valuerWithParent
}
// mapValuer is a type for map to meet the Valuer interface.
mapValuer map[string]interface{}
mapValuer map[string]any
// simpleValuer is a type to get value from current node.
simpleValuer node
// recursiveValuer is a type to get the value recursively from current and parent nodes.
@@ -35,13 +35,13 @@ type (
)
// Value gets the value assciated with the given key from mv.
func (mv mapValuer) Value(key string) (interface{}, bool) {
func (mv mapValuer) Value(key string) (any, bool) {
v, ok := mv[key]
return v, ok
}
// Value gets the value associated with the given key from sv.
func (sv simpleValuer) Value(key string) (interface{}, bool) {
func (sv simpleValuer) Value(key string) (any, bool) {
v, ok := sv.current.Value(key)
return v, ok
}
@@ -60,7 +60,7 @@ func (sv simpleValuer) Parent() valuerWithParent {
// Value gets the value associated with the given key from rv,
// and it will inherit the value from parent nodes.
func (rv recursiveValuer) Value(key string) (interface{}, bool) {
func (rv recursiveValuer) Value(key string) (any, bool) {
val, ok := rv.current.Value(key)
if !ok {
if parent := rv.Parent(); parent != nil {
@@ -70,7 +70,7 @@ func (rv recursiveValuer) Value(key string) (interface{}, bool) {
return nil, false
}
vm, ok := val.(map[string]interface{})
vm, ok := val.(map[string]any)
if !ok {
return val, true
}
@@ -85,7 +85,7 @@ func (rv recursiveValuer) Value(key string) (interface{}, bool) {
return val, true
}
pm, ok := pv.(map[string]interface{})
pm, ok := pv.(map[string]any)
if !ok {
return val, true
}

View File

@@ -7,17 +7,17 @@ import (
)
func TestMapValuerWithInherit_Value(t *testing.T) {
input := map[string]interface{}{
"discovery": map[string]interface{}{
input := map[string]any{
"discovery": map[string]any{
"host": "localhost",
"port": 8080,
},
"component": map[string]interface{}{
"component": map[string]any{
"name": "test",
},
}
valuer := recursiveValuer{
current: mapValuer(input["component"].(map[string]interface{})),
current: mapValuer(input["component"].(map[string]any)),
parent: simpleValuer{
current: mapValuer(input),
},
@@ -26,24 +26,24 @@ func TestMapValuerWithInherit_Value(t *testing.T) {
val, ok := valuer.Value("discovery")
assert.True(t, ok)
m, ok := val.(map[string]interface{})
m, ok := val.(map[string]any)
assert.True(t, ok)
assert.Equal(t, "localhost", m["host"])
assert.Equal(t, 8080, m["port"])
}
func TestRecursiveValuer_Value(t *testing.T) {
input := map[string]interface{}{
"component": map[string]interface{}{
input := map[string]any{
"component": map[string]any{
"name": "test",
"foo": map[string]interface{}{
"foo": map[string]any{
"bar": "baz",
},
},
"foo": "value",
}
valuer := recursiveValuer{
current: mapValuer(input["component"].(map[string]interface{})),
current: mapValuer(input["component"].(map[string]any)),
parent: simpleValuer{
current: mapValuer(input),
},
@@ -51,7 +51,7 @@ func TestRecursiveValuer_Value(t *testing.T) {
val, ok := valuer.Value("foo")
assert.True(t, ok)
assert.EqualValues(t, map[string]interface{}{
assert.EqualValues(t, map[string]any{
"bar": "baz",
}, val)
}

View File

@@ -7,7 +7,7 @@ import (
)
// UnmarshalYamlBytes unmarshals content into v.
func UnmarshalYamlBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalYamlBytes(content []byte, v any, opts ...UnmarshalOption) error {
b, err := encoding.YamlToJson(content)
if err != nil {
return err
@@ -17,7 +17,7 @@ func UnmarshalYamlBytes(content []byte, v interface{}, opts ...UnmarshalOption)
}
// UnmarshalYamlReader unmarshals content from reader into v.
func UnmarshalYamlReader(reader io.Reader, v interface{}, opts ...UnmarshalOption) error {
func UnmarshalYamlReader(reader io.Reader, v any, opts ...UnmarshalOption) error {
b, err := io.ReadAll(reader)
if err != nil {
return err