util.go (1440B)
1 package form 2 3 import ( 4 "reflect" 5 "strconv" 6 ) 7 8 // ExtractType gets the actual underlying type of field value. 9 // it is exposed for use within you Custom Functions 10 func ExtractType(current reflect.Value) (reflect.Value, reflect.Kind) { 11 12 switch current.Kind() { 13 case reflect.Ptr: 14 15 if current.IsNil() { 16 return current, reflect.Ptr 17 } 18 19 return ExtractType(current.Elem()) 20 21 case reflect.Interface: 22 23 if current.IsNil() { 24 return current, reflect.Interface 25 } 26 27 return ExtractType(current.Elem()) 28 29 default: 30 return current, current.Kind() 31 } 32 } 33 34 func parseBool(str string) (bool, error) { 35 36 switch str { 37 case "1", "t", "T", "true", "TRUE", "True", "on", "yes", "ok": 38 return true, nil 39 case "", "0", "f", "F", "false", "FALSE", "False", "off", "no": 40 return false, nil 41 } 42 43 // strconv.NumError mimicing exactly the strconv.ParseBool(..) error and type 44 // to ensure compatibility with std library and beyond. 45 return false, &strconv.NumError{Func: "ParseBool", Num: str, Err: strconv.ErrSyntax} 46 } 47 48 // hasValue determines if a reflect.Value is it's default value 49 func hasValue(field reflect.Value) bool { 50 switch field.Kind() { 51 case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func: 52 return !field.IsNil() 53 default: 54 if !field.IsValid() { 55 return false 56 } 57 if !field.Type().Comparable() { 58 return true 59 } 60 return field.Interface() != reflect.Zero(field.Type()).Interface() 61 } 62 }