Skip to content

Commit

Permalink
💥 update: replace interface{} to any in remaining go files
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 24, 2023
1 parent 6b58d86 commit 526cd5a
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 183 deletions.
11 changes: 0 additions & 11 deletions _examples/httpdemo2/go.mod

This file was deleted.

63 changes: 0 additions & 63 deletions _examples/httpdemo2/go.sum

This file was deleted.

8 changes: 3 additions & 5 deletions _examples/httpdemo2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
func main() {
mux := http.NewServeMux()
// 字符串转整型
validate.AddFilter("newIsInt", func(val interface{}) interface{} {
validate.AddFilter("newIsInt", func(val any) any {
switch val.(type) {
case int:
return val.(int)
case string:
num, parseErr := strconv.Atoi(val.(string))
if parseErr != nil {
if parseErr != nil {
return errors.New("参数类型错误!")
}
return num
Expand All @@ -36,7 +36,7 @@ func main() {
if err != nil {
panic(err)
}
dump.P(data)
dump.P(data)
v := data.Create()
// setting rules
v.
Expand All @@ -58,8 +58,6 @@ dump.P(data)
}
})



mux.HandleFunc("/post-json", handler1)

color.Cyanln("server start on localhost:8090")
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type factory struct {
func newFactory() *factory {
f := &factory{}
f.pool.New = func() interface{} {
f.pool.New = func() any {
return newValidation(nil)
}
Expand Down
14 changes: 7 additions & 7 deletions filtering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ func TestAddFilter(t *testing.T) {
AddFilter("myFilter", func(v string) (bool, int) { return false, 0 })
})
is.Panics(func() {
AddFilter("myFilter", func() interface{} { return nil })
AddFilter("myFilter", func() any { return nil })
})

AddFilters(M{
"myFilter0": func(val interface{}) string { return "myFilter0" },
"myFilter0": func(val any) string { return "myFilter0" },
})
AddFilter("myFilter1", func(val interface{}) string { return "myFilter1" })
AddFilter("myFilter1", func(val any) string { return "myFilter1" })

v := New(map[string]interface{}{
v := New(map[string]any{
"name": " inhere ",
"age": " 50 ",
"key0": "val0",
"key1": "val1",
"tags": "go,php",
})
v.AddFilters(M{
"myFilter2": func(val interface{}, a, b string) (string, error) { return "myFilter2:" + a + b, nil },
"myFilter2": func(val any, a, b string) (string, error) { return "myFilter2:" + a + b, nil },
})
v.FilterRule("key0", "myFilter0")
v.FilterRules(MS{
Expand Down Expand Up @@ -127,11 +127,11 @@ func TestAddFilter(t *testing.T) {

// check panic caused nil value with custom filter
func TestFilterRuleNilValue(t *testing.T) {
AddFilter("X", func(in interface{}) interface{} {
AddFilter("X", func(in any) any {
return in
})

v := Map(map[string]interface{}{
v := Map(map[string]any{
"bad": nil,
})
v.FilterRule("bad", "X")
Expand Down
4 changes: 2 additions & 2 deletions locales/ruru/ruru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestRegister(t *testing.T) {
is := assert.New(t)
v := validate.Map(map[string]interface{}{
v := validate.Map(map[string]any{
"age": 23,
})

Expand All @@ -30,7 +30,7 @@ func TestRegisterGlobal(t *testing.T) {
RegisterGlobal()

is := assert.New(t)
v := validate.Map(map[string]interface{}{
v := validate.Map(map[string]any{
"age": 23,
})

Expand Down
4 changes: 2 additions & 2 deletions locales/zhcn/zhcn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestRegister(t *testing.T) {
is := assert.New(t)
v := validate.Map(map[string]interface{}{
v := validate.Map(map[string]any{
"age": 23,
})

Expand All @@ -30,7 +30,7 @@ func TestRegisterGlobal(t *testing.T) {
RegisterGlobal()

is := assert.New(t)
v := validate.Map(map[string]interface{}{
v := validate.Map(map[string]any{
"age": 23,
})

Expand Down
4 changes: 2 additions & 2 deletions locales/zhtw/zhtw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestRegister(t *testing.T) {
is := assert.New(t)
v := validate.Map(map[string]interface{}{
v := validate.Map(map[string]any{
"age": 23,
"name": "inhere",
})
Expand All @@ -31,7 +31,7 @@ func TestRegisterGlobal(t *testing.T) {
RegisterGlobal()

is := assert.New(t)
v := validate.Map(map[string]interface{}{
v := validate.Map(map[string]any{
"age": 23,
"name": "inhere",
})
Expand Down
4 changes: 2 additions & 2 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (t *Translator) HasMessage(key string) bool {
}

// Message get by validator name and field name.
func (t *Translator) Message(validator, field string, args ...interface{}) (msg string) {
func (t *Translator) Message(validator, field string, args ...any) (msg string) {
argLen := len(args)
errMsg := t.findMessage(validator, field, argLen)
if errMsg == "" {
Expand All @@ -426,7 +426,7 @@ func (t *Translator) Message(validator, field string, args ...interface{}) (msg
}

// format message for the validator
func (t *Translator) format(errMsg, field string, args []interface{}) string {
func (t *Translator) format(errMsg, field string, args []any) string {
argLen := len(args)

// fix: #111 argN maybe is a field name
Expand Down
4 changes: 2 additions & 2 deletions morecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestValidation_custom_type(t *testing.T) {
// Name: "tom",
// Age: 0,
// })
// v.AddValidator("required", func(val interface{}) bool {
// v.AddValidator("required", func(val any) bool {
// dump.V(val)
// // do something ...
// return false
Expand All @@ -62,7 +62,7 @@ func TestValidation_custom_required2(t *testing.T) {
})

buf := new(bytes.Buffer)
v.AddValidator("required_custom", func(val interface{}) bool {
v.AddValidator("required_custom", func(val any) bool {
buf.WriteString("value:")
buf.WriteString(fmt.Sprint(val))
return false
Expand Down
34 changes: 17 additions & 17 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var nilObj = NilObject{}
var nilRVal = reflect.ValueOf(nilObj)

// NilValue TODO a reflect nil value, use for instead of nilRVal
var NilValue = reflect.Zero(reflect.TypeOf((*interface{})(nil)).Elem())
var NilValue = reflect.Zero(reflect.TypeOf((*any)(nil)).Elem())

// From package "text/template" -> text/template/funcs.go
var (
Expand All @@ -34,13 +34,13 @@ var (
)

// IsNilObj check value is internal NilObject
func IsNilObj(val interface{}) bool {
func IsNilObj(val any) bool {
_, ok := val.(NilObject)
return ok
}

// CallByValue call func by reflect.Value
func CallByValue(fv reflect.Value, args ...interface{}) []reflect.Value {
func CallByValue(fv reflect.Value, args ...any) []reflect.Value {
if fv.Kind() != reflect.Func {
panicf("parameter must be an func type")
}
Expand Down Expand Up @@ -85,25 +85,25 @@ func stringSplit(str, sep string) (ss []string) {
}

// TODO use arrutil.StringsToSlice()
func strings2Args(strings []string) []interface{} {
args := make([]interface{}, len(strings))
func strings2Args(strings []string) []any {
args := make([]any, len(strings))
for i, s := range strings {
args[i] = s
}
return args
}

// TODO use arrutil.SliceToStrings()
func args2strings(args []interface{}) []string {
func args2strings(args []any) []string {
strSlice := make([]string, len(args))
for i, a := range args {
strSlice[i] = strutil.QuietString(a)
}
return strSlice
}

func buildArgs(val interface{}, args []interface{}) []interface{} {
newArgs := make([]interface{}, len(args)+1)
func buildArgs(val any, args []any) []any {
newArgs := make([]any, len(args)+1)
newArgs[0] = val
// as[1:] = args // error
copy(newArgs[1:], args)
Expand All @@ -125,7 +125,7 @@ func ValueLen(v reflect.Value) int {
// ErrConvertFail error
var ErrConvertFail = errors.New("convert value is failure")

func valueToInt64(v interface{}, strict bool) (i64 int64, err error) {
func valueToInt64(v any, strict bool) (i64 int64, err error) {
switch tVal := v.(type) {
case string:
if strict {
Expand Down Expand Up @@ -169,7 +169,7 @@ func valueToInt64(v interface{}, strict bool) (i64 int64, err error) {
}

// CalcLength for input value
func CalcLength(val interface{}) int {
func CalcLength(val any) int {
if val == nil {
return -1
}
Expand All @@ -181,7 +181,7 @@ func CalcLength(val interface{}) int {
// value compare.
//
// only check for: int(X), uint(X), float(X), string.
func valueCompare(srcVal, dstVal interface{}, op string) (ok bool) {
func valueCompare(srcVal, dstVal any, op string) (ok bool) {
// string compare
if str1, ok := srcVal.(string); ok {
str2, err := strutil.ToString(dstVal)
Expand Down Expand Up @@ -211,7 +211,7 @@ func getVariadicKind(typ reflect.Type) reflect.Kind {
// convTypeByBaseKind convert value type by base kind
//
//nolint:forcetypeassert
func convTypeByBaseKind(srcVal interface{}, srcKind kind, dstType reflect.Kind) (interface{}, error) {
func convTypeByBaseKind(srcVal any, srcKind kind, dstType reflect.Kind) (any, error) {
switch srcKind {
case stringKind:
switch dstType {
Expand Down Expand Up @@ -243,7 +243,7 @@ func convTypeByBaseKind(srcVal interface{}, srcKind kind, dstType reflect.Kind)

// convert custom type to generic basic int, string, unit.
// returns string, int64 or error
func convToBasicType(val interface{}) (value interface{}, err error) {
func convToBasicType(val any) (value any, err error) {
v := reflect.Indirect(reflect.ValueOf(val))

switch v.Kind() {
Expand All @@ -259,11 +259,11 @@ func convToBasicType(val interface{}) (value interface{}, err error) {
return
}

func panicf(format string, args ...interface{}) {
func panicf(format string, args ...any) {
panic("validate: " + fmt.Sprintf(format, args...))
}

func checkValidatorFunc(name string, fn interface{}) reflect.Value {
func checkValidatorFunc(name string, fn any) reflect.Value {
if !goodName(name) {
panicf("validate name %s is not a valid identifier", name)
}
Expand All @@ -286,7 +286,7 @@ func checkValidatorFunc(name string, fn interface{}) reflect.Value {
return fv
}

func checkFilterFunc(name string, fn interface{}) reflect.Value {
func checkFilterFunc(name string, fn any) reflect.Value {
if !goodName(name) {
panicf("filter name %s is not a valid identifier", name)
}
Expand Down Expand Up @@ -430,7 +430,7 @@ func eq(arg1 reflect.Value, arg2 reflect.Value) (bool, error) {
}

// from package: github.com/stretchr/testify/assert/assertions.go
func includeElement(list, element interface{}) (ok, found bool) {
func includeElement(list, element any) (ok, found bool) {
listValue := reflect.ValueOf(list)
elementValue := reflect.ValueOf(element)
listKind := listValue.Type().Kind()
Expand Down

0 comments on commit 526cd5a

Please sign in to comment.