@@ -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-
332313func 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+
392390func 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
425423func 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
515487type kind int
516488
489+ // base kinds
517490const (
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-
531500func 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
644614func 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+ }
0 commit comments