Skip to content

Commit 56ca59e

Browse files
committed
chore: rename some func, add more unit tests and more
- update readme - add more validator alias
1 parent f48988b commit 56ca59e

File tree

12 files changed

+157
-110
lines changed

12 files changed

+157
-110
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ validator/aliases | description
596596
`intEq/intEqual` | Check value is int and equals to the given value.
597597
`len/length` | Check value length is equals to the given size(use for `string` `array` `slice` `map`).
598598
`regex/regexp` | Check if the value can pass the regular verification
599-
`arr/array/isArray` | Check value is array type
599+
`arr/list/array/isArray` | Check value is array, slice type
600600
`map/isMap` | Check value is a MAP type
601601
`strings/isStrings` | Check value is string slice type(only allow `[]string`).
602602
`ints/isInts` | Check value is int slice type(only allow `[]int`).

README.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func main() {
528528
`max_len/maxLen/maxLength` | 检查值的最大长度是给定大小
529529
`email/isEmail` | 检查值是Email地址字符串
530530
`regex/regexp` | 检查该值是否可以通过正则验证
531-
`arr/array/isArray` | 检查值是数组`array`类型
531+
`arr/list/array/isArray` | 检查值是 `array` 或者 `slice`类型
532532
`map/isMap` | 检查值是 `map` 类型
533533
`strings/isStrings` | 检查值是字符串切片类型(`[]string`)
534534
`ints/isInts` | 检查值是`int` slice类型(only allow `[]int`)

cache.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package validate
22

3-
import (
4-
"database/sql/driver"
5-
"fmt"
6-
)
7-
83
/*
94
// TODO: idea for optimize ...
105
// - use pool for Validation
@@ -46,8 +41,29 @@ func (f *factory) put(v *Validation) {
4641
}
4742
*/
4843

49-
func test() {
50-
i32 := driver.Int32
44+
// func test() {
45+
// i32 := driver.Int32
46+
// fmt.Println(i32)
47+
// }
5148

52-
fmt.Println(i32)
49+
/*
50+
// TODO cache struct reflect value, tags and more
51+
type structMeta struct {
5352
}
53+
54+
type cache struct {
55+
m sync.Map
56+
// m atomic.Value
57+
// map[reflect.Type]*cStruct
58+
}
59+
60+
func (c *cache) get(rt reflect.Type) *structMeta {
61+
// key := rt.PkgPath() + rt.Name()
62+
return c.m.Load(rt)
63+
}
64+
65+
func (c *cache) set(rt reflect.Type, meta structMeta) {
66+
// key := rt.PkgPath() + rt.Name()
67+
c.m.Store(rt, data)
68+
}
69+
*/

data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ func (d *StructData) Set(field string, val interface{}) (newVal interface{}, err
635635
return nil, err
636636
}
637637

638-
newVal, err = convertType(val, srcKind, fv.Kind())
638+
newVal, err = convTypeByBaseKind(val, srcKind, fv.Kind())
639639
if err != nil {
640640
return nil, err
641641
}

register.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,26 @@ var validatorAliases = map[string]string{
126126
"integer": "isInt",
127127
"uint": "isUint",
128128
"bool": "isBool",
129+
"boolean": "isBool",
129130
"float": "isFloat",
130131
"map": "isMap",
131132
"ints": "isInts", // []int
132133
"int_slice": "isInts",
134+
"int_list": "isInts",
133135
"str": "isString",
134136
"string": "isString",
135137
"strings": "isStrings", // []string
138+
"str_list": "isStrings",
136139
"str_slice": "isStrings",
137140
"arr": "isArray",
141+
"list": "isArray",
138142
"array": "isArray",
139143
"slice": "isSlice",
140144
// val
141145
"regex": "regexp",
142146
"eq": "isEqual",
143147
"equal": "isEqual",
148+
"equals": "isEqual",
144149
"intEq": "intEqual",
145150
"int_eq": "intEqual",
146151
"ne": "notEqual",

util.go

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,6 @@ func parseArgString(argStr string) (ss []string) {
310310
return stringSplit(argStr, ",")
311311
}
312312

313-
func toInt64Slice(enum interface{}) (ret []int64, ok bool) {
314-
rv := reflect.ValueOf(enum)
315-
if rv.Kind() != reflect.Slice {
316-
return
317-
}
318-
319-
for i := 0; i < rv.Len(); i++ {
320-
i64, err := mathutil.ToInt64(rv.Index(i).Interface())
321-
if err != nil {
322-
return []int64{}, false
323-
}
324-
325-
ret = append(ret, i64)
326-
}
327-
328-
ok = true
329-
return
330-
}
331-
332313
func getVariadicKind(typString string) reflect.Kind {
333314
switch typString {
334315
case "[]int":
@@ -359,8 +340,8 @@ func getVariadicKind(typString string) reflect.Kind {
359340
return reflect.Invalid
360341
}
361342

362-
// convertType use basic kind
363-
func convertType(srcVal interface{}, srcKind kind, dstType reflect.Kind) (interface{}, error) {
343+
// convTypeByBaseKind convert value type by base kind
344+
func convTypeByBaseKind(srcVal interface{}, srcKind kind, dstType reflect.Kind) (interface{}, error) {
364345
switch srcKind {
365346
case stringKind:
366347
switch dstType {
@@ -377,7 +358,6 @@ func convertType(srcVal interface{}, srcKind kind, dstType reflect.Kind) (interf
377358
case reflect.Int64:
378359
return i64, nil
379360
case reflect.String:
380-
// fmt is slow : return fmt.Sprint(i64), nil
381361
return strutil.ToString(srcVal)
382362
}
383363
default:
@@ -389,6 +369,24 @@ func convertType(srcVal interface{}, srcKind kind, dstType reflect.Kind) (interf
389369
return nil, ErrConvertFail
390370
}
391371

372+
// convert custom type to generic basic int, string, unit.
373+
// returns string, int64 or error
374+
func convToBasicType(val interface{}) (value interface{}, err error) {
375+
v := reflect.ValueOf(val)
376+
377+
switch v.Kind() {
378+
case reflect.String:
379+
value = v.String()
380+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
381+
value = v.Int()
382+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
383+
value = v.Int() // always return int64
384+
default:
385+
err = ErrConvertFail
386+
}
387+
return
388+
}
389+
392390
func panicf(format string, args ...interface{}) {
393391
panic("validate: " + fmt.Sprintf(format, args...))
394392
}
@@ -424,7 +422,7 @@ func checkValidatorFunc(name string, fn interface{}) reflect.Value {
424422

425423
func checkFilterFunc(name string, fn interface{}) reflect.Value {
426424
if !goodName(name) {
427-
panic(fmt.Errorf("filter name %s is not a valid identifier", name))
425+
panicf("filter name %s is not a valid identifier", name)
428426
}
429427

430428
fv := reflect.ValueOf(fn)
@@ -473,32 +471,6 @@ func goodName(name string) bool {
473471
return true
474472
}
475473

476-
// ---- From package "text/template" -> text/template/exec.go
477-
// indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
478-
// func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
479-
// for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
480-
// if v.IsNil() {
481-
// return v, true
482-
// }
483-
// }
484-
// return v, false
485-
// }
486-
487-
// indirectInterface returns the concrete value in an interface value,
488-
// or else the zero reflect.Value.
489-
// That is, if v represents the interface value x, the result is the same as reflect.ValueOf(x):
490-
// the fact that x was an interface value is forgotten.
491-
func indirectInterface(v reflect.Value) reflect.Value {
492-
if v.Kind() != reflect.Interface {
493-
return v
494-
}
495-
496-
if v.IsNil() {
497-
return emptyValue
498-
}
499-
return v.Elem()
500-
}
501-
502474
/*************************************************************
503475
* Comparison:
504476
* From package "text/template" -> text/template/funcs.go
@@ -514,6 +486,7 @@ var (
514486

515487
type kind int
516488

489+
// base kinds
517490
const (
518491
invalidKind kind = iota
519492
boolKind
@@ -524,10 +497,6 @@ const (
524497
uintKind
525498
)
526499

527-
func basicKind(v reflect.Value) (kind, error) {
528-
return basicKindV2(v.Kind())
529-
}
530-
531500
func basicKindV2(kind reflect.Kind) (kind, error) {
532501
switch kind {
533502
case reflect.Bool:
@@ -640,6 +609,7 @@ func includeElement(list, element interface{}) (ok, found bool) {
640609

641610
// IsZero reports whether v is the zero value for its type.
642611
// It panics if the argument is invalid.
612+
//
643613
// NOTICE: this built-in method in reflect/value.go since go 1.13
644614
func IsZero(v reflect.Value) bool {
645615
switch v.Kind() {
@@ -696,3 +666,30 @@ func removeValuePtr(t reflect.Value) reflect.Value {
696666
}
697667
return t
698668
}
669+
670+
// ---- From package "text/template" -> text/template/exec.go
671+
672+
// indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
673+
// func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
674+
// for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
675+
// if v.IsNil() {
676+
// return v, true
677+
// }
678+
// }
679+
// return v, false
680+
// }
681+
682+
// indirectInterface returns the concrete value in an interface value,
683+
// or else the zero reflect.Value.
684+
// That is, if v represents the interface value x, the result is the same as reflect.ValueOf(x):
685+
// the fact that x was an interface value is forgotten.
686+
func indirectInterface(v reflect.Value) reflect.Value {
687+
if v.Kind() != reflect.Interface {
688+
return v
689+
}
690+
691+
if v.IsNil() {
692+
return emptyValue
693+
}
694+
return v.Elem()
695+
}

validate_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,21 @@ func TestUtil_Func_goodName(t *testing.T) {
8282
}
8383

8484
func Test_Util_Func_convertType(t *testing.T) {
85-
nVal, err := convertType(23, intKind, reflect.String)
85+
nVal, err := convTypeByBaseKind(23, intKind, reflect.String)
8686
assert.NoError(t, err)
8787
assert.Equal(t, "23", nVal)
8888

89-
nVal, err = convertType(uint(23), uintKind, reflect.String)
89+
nVal, err = convTypeByBaseKind(uint(23), uintKind, reflect.String)
9090
assert.NoError(t, err)
9191
assert.Equal(t, "23", nVal)
9292
}
9393

9494
func Test_IsZero(t *testing.T) {
9595
assert.True(t, IsZero(reflect.ValueOf([2]int{})))
9696
assert.True(t, IsZero(reflect.ValueOf(false)))
97+
assert.Panics(t, func() {
98+
IsZero(reflect.ValueOf(nil))
99+
})
97100
}
98101

99102
func TestMS_String(t *testing.T) {

validation.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package validate
33
import (
44
"fmt"
55
"reflect"
6+
"strings"
67
)
78

89
// some default value settings.
@@ -379,12 +380,17 @@ func (v *Validation) RawVal(key string) interface{} {
379380
return val
380381
}
381382

382-
// Get value by key
383+
// Get value by key.
383384
func (v *Validation) Get(key string) (interface{}, bool) {
384385
if v.data == nil { // check input data
385386
return nil, false
386387
}
387388

389+
// if end withs: .*, return the parent value
390+
if strings.HasSuffix(key, ".*") {
391+
key = key[0 : len(key)-2]
392+
}
393+
388394
// find from filtered data.
389395
if val, ok := v.filteredData[key]; ok {
390396
return val, true
@@ -395,6 +401,8 @@ func (v *Validation) Get(key string) (interface{}, bool) {
395401
return val, true
396402
}
397403

404+
// TODO add cache data v.caches[key]
405+
398406
// get from source data
399407
return v.data.Get(key)
400408
}

0 commit comments

Comments
 (0)