diff options
Diffstat (limited to 'vendor/github.com/spf13/cast')
| -rw-r--r-- | vendor/github.com/spf13/cast/.editorconfig | 15 | ||||
| -rw-r--r-- | vendor/github.com/spf13/cast/.golangci.yaml | 39 | ||||
| -rw-r--r-- | vendor/github.com/spf13/cast/README.md | 12 | ||||
| -rw-r--r-- | vendor/github.com/spf13/cast/cast.go | 18 | ||||
| -rw-r--r-- | vendor/github.com/spf13/cast/caste.go | 272 |
5 files changed, 197 insertions, 159 deletions
diff --git a/vendor/github.com/spf13/cast/.editorconfig b/vendor/github.com/spf13/cast/.editorconfig new file mode 100644 index 000000000..a85749f19 --- /dev/null +++ b/vendor/github.com/spf13/cast/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.go] +indent_style = tab + +[{*.yml,*.yaml}] +indent_size = 2 diff --git a/vendor/github.com/spf13/cast/.golangci.yaml b/vendor/github.com/spf13/cast/.golangci.yaml new file mode 100644 index 000000000..e00fd47aa --- /dev/null +++ b/vendor/github.com/spf13/cast/.golangci.yaml @@ -0,0 +1,39 @@ +version: "2" + +run: + timeout: 10m + +linters: + enable: + - errcheck + - govet + - ineffassign + - misspell + - nolintlint + # - revive + - unused + + disable: + - staticcheck + + settings: + misspell: + locale: US + nolintlint: + allow-unused: false # report any unused nolint directives + require-specific: false # don't require nolint directives to be specific about which linter is being skipped + +formatters: + enable: + - gci + - gofmt + # - gofumpt + - goimports + # - golines + + settings: + gci: + sections: + - standard + - default + - localmodule diff --git a/vendor/github.com/spf13/cast/README.md b/vendor/github.com/spf13/cast/README.md index 1be666a45..c58eccb3f 100644 --- a/vendor/github.com/spf13/cast/README.md +++ b/vendor/github.com/spf13/cast/README.md @@ -1,9 +1,9 @@ # cast -[](https://github.com/spf13/cast/actions/workflows/test.yaml) -[](https://pkg.go.dev/mod/github.com/spf13/cast) - -[](https://goreportcard.com/report/github.com/spf13/cast) +[](https://github.com/spf13/cast/actions/workflows/ci.yaml) +[](https://pkg.go.dev/mod/github.com/spf13/cast) + +[](https://deps.dev/go/github.com%252Fspf13%252Fcast) Easy and safe casting from one type to another in Go @@ -73,3 +73,7 @@ the code for a complete set. var eight interface{} = 8 cast.ToInt(eight) // 8 cast.ToInt(nil) // 0 + +## License + +The project is licensed under the [MIT License](LICENSE). diff --git a/vendor/github.com/spf13/cast/cast.go b/vendor/github.com/spf13/cast/cast.go index 0cfe9418d..386ba80a9 100644 --- a/vendor/github.com/spf13/cast/cast.go +++ b/vendor/github.com/spf13/cast/cast.go @@ -169,6 +169,24 @@ func ToIntSlice(i interface{}) []int { return v } +// ToInt64Slice casts an interface to a []int64 type. +func ToInt64Slice(i interface{}) []int64 { + v, _ := ToInt64SliceE(i) + return v +} + +// ToUintSlice casts an interface to a []uint type. +func ToUintSlice(i interface{}) []uint { + v, _ := ToUintSliceE(i) + return v +} + +// ToFloat64Slice casts an interface to a []float64 type. +func ToFloat64Slice(i interface{}) []float64 { + v, _ := ToFloat64SliceE(i) + return v +} + // ToDurationSlice casts an interface to a []time.Duration type. func ToDurationSlice(i interface{}) []time.Duration { v, _ := ToDurationSliceE(i) diff --git a/vendor/github.com/spf13/cast/caste.go b/vendor/github.com/spf13/cast/caste.go index 4181a2e75..4d4ca8db1 100644 --- a/vendor/github.com/spf13/cast/caste.go +++ b/vendor/github.com/spf13/cast/caste.go @@ -615,9 +615,6 @@ func ToUint64E(i interface{}) (uint64, error) { case string: v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0) if err == nil { - if v < 0 { - return 0, errNegativeNotAllowed - } return v, nil } return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i) @@ -1000,36 +997,57 @@ func ToStringE(i interface{}) (string, error) { } } -// ToStringMapStringE casts an interface to a map[string]string type. -func ToStringMapStringE(i interface{}) (map[string]string, error) { - m := map[string]string{} +func toMapE[K comparable, V any](i any, keyFn func(any) K, valFn func(any) V) (map[K]V, error) { + m := map[K]V{} + + if i == nil { + return m, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, m) + } switch v := i.(type) { - case map[string]string: + case map[K]V: return v, nil - case map[string]interface{}: + + case map[K]any: for k, val := range v { - m[ToString(k)] = ToString(val) + m[k] = valFn(val) } + return m, nil - case map[interface{}]string: + + case map[any]V: for k, val := range v { - m[ToString(k)] = ToString(val) + m[keyFn(k)] = val } + return m, nil - case map[interface{}]interface{}: + + case map[any]any: for k, val := range v { - m[ToString(k)] = ToString(val) + m[keyFn(k)] = valFn(val) } + return m, nil + case string: err := jsonStringToObject(v, &m) + return m, err + default: - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]string", i, i) + return m, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, m) } } +func toStringMapE[T any](i any, fn func(any) T) (map[string]T, error) { + return toMapE(i, ToString, fn) +} + +// ToStringMapStringE casts an interface to a map[string]string type. +func ToStringMapStringE(i any) (map[string]string, error) { + return toStringMapE(i, ToString) +} + // ToStringMapStringSliceE casts an interface to a map[string][]string type. func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) { m := map[string][]string{} @@ -1096,130 +1114,83 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) { // ToStringMapBoolE casts an interface to a map[string]bool type. func ToStringMapBoolE(i interface{}) (map[string]bool, error) { - m := map[string]bool{} - - switch v := i.(type) { - case map[interface{}]interface{}: - for k, val := range v { - m[ToString(k)] = ToBool(val) - } - return m, nil - case map[string]interface{}: - for k, val := range v { - m[ToString(k)] = ToBool(val) - } - return m, nil - case map[string]bool: - return v, nil - case string: - err := jsonStringToObject(v, &m) - return m, err - default: - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]bool", i, i) - } + return toStringMapE(i, ToBool) } // ToStringMapE casts an interface to a map[string]interface{} type. func ToStringMapE(i interface{}) (map[string]interface{}, error) { - m := map[string]interface{}{} + fn := func(i any) any { return i } - switch v := i.(type) { - case map[interface{}]interface{}: - for k, val := range v { - m[ToString(k)] = val - } - return m, nil - case map[string]interface{}: - return v, nil - case string: - err := jsonStringToObject(v, &m) - return m, err - default: - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i) - } + return toStringMapE(i, fn) } -// ToStringMapIntE casts an interface to a map[string]int{} type. -func ToStringMapIntE(i interface{}) (map[string]int, error) { - m := map[string]int{} +func toStringMapIntE[T int | int64](i any, fn func(any) T, fnE func(any) (T, error)) (map[string]T, error) { + m := map[string]T{} + if i == nil { - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i) + return m, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, m) } switch v := i.(type) { - case map[interface{}]interface{}: - for k, val := range v { - m[ToString(k)] = ToInt(val) - } - return m, nil - case map[string]interface{}: - for k, val := range v { - m[k] = ToInt(val) - } - return m, nil - case map[string]int: + case map[string]T: return v, nil - case string: - err := jsonStringToObject(v, &m) - return m, err - } - if reflect.TypeOf(i).Kind() != reflect.Map { - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i) - } - - mVal := reflect.ValueOf(m) - v := reflect.ValueOf(i) - for _, keyVal := range v.MapKeys() { - val, err := ToIntE(v.MapIndex(keyVal).Interface()) - if err != nil { - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i) + case map[string]any: + for k, val := range v { + m[k] = fn(val) } - mVal.SetMapIndex(keyVal, reflect.ValueOf(val)) - } - return m, nil -} -// ToStringMapInt64E casts an interface to a map[string]int64{} type. -func ToStringMapInt64E(i interface{}) (map[string]int64, error) { - m := map[string]int64{} - if i == nil { - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i) - } + return m, nil - switch v := i.(type) { - case map[interface{}]interface{}: + case map[any]T: for k, val := range v { - m[ToString(k)] = ToInt64(val) + m[ToString(k)] = val } + return m, nil - case map[string]interface{}: + + case map[any]any: for k, val := range v { - m[k] = ToInt64(val) + m[ToString(k)] = fn(val) } + return m, nil - case map[string]int64: - return v, nil + case string: err := jsonStringToObject(v, &m) + return m, err } if reflect.TypeOf(i).Kind() != reflect.Map { - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i) + return m, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, m) } + mVal := reflect.ValueOf(m) v := reflect.ValueOf(i) + for _, keyVal := range v.MapKeys() { - val, err := ToInt64E(v.MapIndex(keyVal).Interface()) + val, err := fnE(v.MapIndex(keyVal).Interface()) if err != nil { - return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i) + return m, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, m) } + mVal.SetMapIndex(keyVal, reflect.ValueOf(val)) } + return m, nil } +// ToStringMapIntE casts an interface to a map[string]int{} type. +func ToStringMapIntE(i any) (map[string]int, error) { + return toStringMapIntE(i, ToInt, ToIntE) +} + +// ToStringMapInt64E casts an interface to a map[string]int64{} type. +func ToStringMapInt64E(i interface{}) (map[string]int64, error) { + return toStringMapIntE(i, ToInt64, ToInt64E) +} + // ToSliceE casts an interface to a []interface{} type. func ToSliceE(i interface{}) ([]interface{}, error) { var s []interface{} @@ -1237,14 +1208,13 @@ func ToSliceE(i interface{}) ([]interface{}, error) { } } -// ToBoolSliceE casts an interface to a []bool type. -func ToBoolSliceE(i interface{}) ([]bool, error) { +func toSliceE[T any](i any, fn func(any) (T, error)) ([]T, error) { if i == nil { - return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i) + return []T{}, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, []T{}) } switch v := i.(type) { - case []bool: + case []T: return v, nil } @@ -1252,20 +1222,25 @@ func ToBoolSliceE(i interface{}) ([]bool, error) { switch kind { case reflect.Slice, reflect.Array: s := reflect.ValueOf(i) - a := make([]bool, s.Len()) + a := make([]T, s.Len()) for j := 0; j < s.Len(); j++ { - val, err := ToBoolE(s.Index(j).Interface()) + val, err := fn(s.Index(j).Interface()) if err != nil { - return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i) + return []T{}, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, []T{}) } a[j] = val } return a, nil default: - return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i) + return []T{}, fmt.Errorf("unable to cast %#v of type %T to %T", i, i, []T{}) } } +// ToBoolSliceE casts an interface to a []bool type. +func ToBoolSliceE(i interface{}) ([]bool, error) { + return toSliceE(i, ToBoolE) +} + // ToStringSliceE casts an interface to a []string type. func ToStringSliceE(i interface{}) ([]string, error) { var a []string @@ -1298,6 +1273,26 @@ func ToStringSliceE(i interface{}) ([]string, error) { a = append(a, ToString(u)) } return a, nil + case []uint8: + for _, u := range v { + a = append(a, ToString(u)) + } + return a, nil + case []uint: + for _, u := range v { + a = append(a, ToString(u)) + } + return a, nil + case []uint32: + for _, u := range v { + a = append(a, ToString(u)) + } + return a, nil + case []uint64: + for _, u := range v { + a = append(a, ToString(u)) + } + return a, nil case []float32: for _, u := range v { a = append(a, ToString(u)) @@ -1328,60 +1323,27 @@ func ToStringSliceE(i interface{}) ([]string, error) { // ToIntSliceE casts an interface to a []int type. func ToIntSliceE(i interface{}) ([]int, error) { - if i == nil { - return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i) - } + return toSliceE(i, ToIntE) +} - switch v := i.(type) { - case []int: - return v, nil - } +// ToUintSliceE casts an interface to a []uint type. +func ToUintSliceE(i interface{}) ([]uint, error) { + return toSliceE(i, ToUintE) +} - kind := reflect.TypeOf(i).Kind() - switch kind { - case reflect.Slice, reflect.Array: - s := reflect.ValueOf(i) - a := make([]int, s.Len()) - for j := 0; j < s.Len(); j++ { - val, err := ToIntE(s.Index(j).Interface()) - if err != nil { - return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i) - } - a[j] = val - } - return a, nil - default: - return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i) - } +// ToFloat64SliceE casts an interface to a []float64 type. +func ToFloat64SliceE(i interface{}) ([]float64, error) { + return toSliceE(i, ToFloat64E) +} + +// ToInt64SliceE casts an interface to a []int64 type. +func ToInt64SliceE(i interface{}) ([]int64, error) { + return toSliceE(i, ToInt64E) } // ToDurationSliceE casts an interface to a []time.Duration type. func ToDurationSliceE(i interface{}) ([]time.Duration, error) { - if i == nil { - return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i) - } - - switch v := i.(type) { - case []time.Duration: - return v, nil - } - - kind := reflect.TypeOf(i).Kind() - switch kind { - case reflect.Slice, reflect.Array: - s := reflect.ValueOf(i) - a := make([]time.Duration, s.Len()) - for j := 0; j < s.Len(); j++ { - val, err := ToDurationE(s.Index(j).Interface()) - if err != nil { - return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i) - } - a[j] = val - } - return a, nil - default: - return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i) - } + return toSliceE(i, ToDurationE) } // StringToDate attempts to parse a string into a time.Time type using a |
