github.com/googollee/clic@v0.1.5/structtags/field_parser.go (about) 1 package structtags 2 3 import ( 4 "encoding" 5 "flag" 6 "fmt" 7 "reflect" 8 "strconv" 9 "strings" 10 "time" 11 ) 12 13 type ParseFieldFunc func(v reflect.Value, str string) error 14 15 // getParseFieldFunc doesn't accept a Pointer Type. Caller should check pointer and pass in Pointer.Elem(). 16 func getParseFieldFunc(t reflect.Type) ParseFieldFunc { 17 if reflect.PointerTo(t).Implements(umarshalerType) { 18 return parseFieldUmarshaler 19 } 20 21 if reflect.PointerTo(t).Implements(flagValueType) { 22 return parseFieldFlagValue 23 } 24 25 if t == durationType { 26 return parseFieldDuration 27 } 28 29 return parsers[t] 30 } 31 32 var parsers = map[reflect.Type]ParseFieldFunc{ 33 reflect.TypeOf(""): parseFieldString, 34 reflect.TypeOf(int(0)): parseFieldInt[int], 35 reflect.TypeOf(int64(0)): parseFieldInt[int64], 36 reflect.TypeOf(int32(0)): parseFieldInt[int32], 37 reflect.TypeOf(int16(0)): parseFieldInt[int16], 38 reflect.TypeOf(int8(0)): parseFieldInt[int8], 39 reflect.TypeOf(uint(0)): parseFieldUint[uint], 40 reflect.TypeOf(uint64(0)): parseFieldUint[uint64], 41 reflect.TypeOf(uint32(0)): parseFieldUint[uint32], 42 reflect.TypeOf(uint16(0)): parseFieldUint[uint16], 43 reflect.TypeOf(uint8(0)): parseFieldUint[uint8], 44 reflect.TypeOf(float64(0)): parseFieldFloat[float64], 45 reflect.TypeOf(float32(0)): parseFieldFloat[float32], 46 reflect.TypeOf(bool(false)): parseFieldBool, 47 } 48 49 func parseFieldString(v reflect.Value, str string) error { 50 v.Set(reflect.ValueOf(str)) 51 return nil 52 } 53 54 func parseFieldInt[Int ~int | ~int8 | ~int16 | ~int32 | ~int64](v reflect.Value, str string) error { 55 i64, err := strconv.ParseInt(str, 10, 64) 56 if err != nil { 57 return fmt.Errorf("can't parse %q to an integer: %w", str, err) 58 } 59 60 v.Set(reflect.ValueOf(Int(i64))) 61 return nil 62 } 63 64 func parseFieldUint[UInt ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](v reflect.Value, str string) error { 65 base := 10 66 if strings.HasPrefix(str, "0x") { 67 base = 16 68 str = str[2:] 69 } else if strings.HasPrefix(str, "0b") { 70 base = 2 71 str = str[2:] 72 } else if strings.HasPrefix(str, "0o") { 73 base = 8 74 str = str[2:] 75 } else if strings.HasPrefix(str, "0") && str != "0" { 76 base = 8 77 str = str[1:] 78 } 79 80 u64, err := strconv.ParseUint(str, base, 64) 81 if err != nil { 82 return fmt.Errorf("can't parse %q to an unsigned-integer: %w", str, err) 83 } 84 85 v.Set(reflect.ValueOf(UInt(u64))) 86 return nil 87 } 88 89 func parseFieldFloat[Float ~float32 | ~float64](v reflect.Value, str string) error { 90 f64, err := strconv.ParseFloat(str, 64) 91 if err != nil { 92 return fmt.Errorf("can't parse %q to a float: %w", str, err) 93 } 94 95 v.Set(reflect.ValueOf(Float(f64))) 96 return nil 97 } 98 99 func parseFieldBool(v reflect.Value, str string) error { 100 b, err := strconv.ParseBool(str) 101 if err != nil { 102 return fmt.Errorf("can't parse %q to a bool: %w", str, err) 103 } 104 105 v.Set(reflect.ValueOf(b)) 106 return nil 107 } 108 109 var umarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]() 110 111 func parseFieldUmarshaler(v reflect.Value, str string) error { 112 if v.Kind() != reflect.Pointer { 113 v = v.Addr() 114 } 115 116 return v.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(str)) 117 } 118 119 var flagValueType = reflect.TypeFor[flag.Value]() 120 121 func parseFieldFlagValue(v reflect.Value, str string) error { 122 if v.Kind() != reflect.Pointer { 123 v = v.Addr() 124 } 125 126 return v.Interface().(flag.Value).Set(str) 127 } 128 129 var durationType = reflect.TypeFor[time.Duration]() 130 131 func parseFieldDuration(v reflect.Value, str string) error { 132 dur, err := time.ParseDuration(str) 133 if err != nil { 134 return err 135 } 136 137 v.Set(reflect.ValueOf(dur)) 138 return nil 139 }