fix gocyclo warnings (#468)
This commit is contained in:
@@ -200,7 +200,7 @@ func (u *Unmarshaler) processFieldNotFromString(field reflect.StructField, value
|
|||||||
case valueKind == reflect.Map && typeKind == reflect.Struct:
|
case valueKind == reflect.Map && typeKind == reflect.Struct:
|
||||||
return u.processFieldStruct(field, value, mapValue, fullName)
|
return u.processFieldStruct(field, value, mapValue, fullName)
|
||||||
case valueKind == reflect.String && typeKind == reflect.Slice:
|
case valueKind == reflect.String && typeKind == reflect.Slice:
|
||||||
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
|
return u.fillSliceFromString(fieldType, value, mapValue)
|
||||||
case valueKind == reflect.String && derefedFieldType == durationType:
|
case valueKind == reflect.String && derefedFieldType == durationType:
|
||||||
return fillDurationValue(fieldType.Kind(), value, mapValue.(string))
|
return fillDurationValue(fieldType.Kind(), value, mapValue.(string))
|
||||||
default:
|
default:
|
||||||
@@ -464,8 +464,7 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value,
|
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
|
||||||
mapValue interface{}, fullName string) error {
|
|
||||||
var slice []interface{}
|
var slice []interface{}
|
||||||
if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
|
if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -69,44 +69,7 @@ func Repr(v interface{}) string {
|
|||||||
val = val.Elem()
|
val = val.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
switch vt := val.Interface().(type) {
|
return reprOfValue(val)
|
||||||
case bool:
|
|
||||||
return strconv.FormatBool(vt)
|
|
||||||
case error:
|
|
||||||
return vt.Error()
|
|
||||||
case float32:
|
|
||||||
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
|
|
||||||
case float64:
|
|
||||||
return strconv.FormatFloat(vt, 'f', -1, 64)
|
|
||||||
case fmt.Stringer:
|
|
||||||
return vt.String()
|
|
||||||
case int:
|
|
||||||
return strconv.Itoa(vt)
|
|
||||||
case int8:
|
|
||||||
return strconv.Itoa(int(vt))
|
|
||||||
case int16:
|
|
||||||
return strconv.Itoa(int(vt))
|
|
||||||
case int32:
|
|
||||||
return strconv.Itoa(int(vt))
|
|
||||||
case int64:
|
|
||||||
return strconv.FormatInt(vt, 10)
|
|
||||||
case string:
|
|
||||||
return vt
|
|
||||||
case uint:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint8:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint16:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint32:
|
|
||||||
return strconv.FormatUint(uint64(vt), 10)
|
|
||||||
case uint64:
|
|
||||||
return strconv.FormatUint(vt, 10)
|
|
||||||
case []byte:
|
|
||||||
return string(vt)
|
|
||||||
default:
|
|
||||||
return fmt.Sprint(val.Interface())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidatePtr(v *reflect.Value) error {
|
func ValidatePtr(v *reflect.Value) error {
|
||||||
@@ -163,48 +126,8 @@ func doParseKeyAndOptions(field reflect.StructField, value string) (string, *fie
|
|||||||
var fieldOpts fieldOptions
|
var fieldOpts fieldOptions
|
||||||
for _, segment := range options {
|
for _, segment := range options {
|
||||||
option := strings.TrimSpace(segment)
|
option := strings.TrimSpace(segment)
|
||||||
switch {
|
if err := parseOption(&fieldOpts, field.Name, option); err != nil {
|
||||||
case option == stringOption:
|
return "", nil, err
|
||||||
fieldOpts.FromString = true
|
|
||||||
case strings.HasPrefix(option, optionalOption):
|
|
||||||
segs := strings.Split(option, equalToken)
|
|
||||||
switch len(segs) {
|
|
||||||
case 1:
|
|
||||||
fieldOpts.Optional = true
|
|
||||||
case 2:
|
|
||||||
fieldOpts.Optional = true
|
|
||||||
fieldOpts.OptionalDep = segs[1]
|
|
||||||
default:
|
|
||||||
return "", nil, fmt.Errorf("field %s has wrong optional", field.Name)
|
|
||||||
}
|
|
||||||
case option == optionalOption:
|
|
||||||
fieldOpts.Optional = true
|
|
||||||
case strings.HasPrefix(option, optionsOption):
|
|
||||||
segs := strings.Split(option, equalToken)
|
|
||||||
if len(segs) != 2 {
|
|
||||||
return "", nil, fmt.Errorf("field %s has wrong options", field.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldOpts.Options = strings.Split(segs[1], optionSeparator)
|
|
||||||
case strings.HasPrefix(option, defaultOption):
|
|
||||||
segs := strings.Split(option, equalToken)
|
|
||||||
if len(segs) != 2 {
|
|
||||||
return "", nil, fmt.Errorf("field %s has wrong default option", field.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldOpts.Default = strings.TrimSpace(segs[1])
|
|
||||||
case strings.HasPrefix(option, rangeOption):
|
|
||||||
segs := strings.Split(option, equalToken)
|
|
||||||
if len(segs) != 2 {
|
|
||||||
return "", nil, fmt.Errorf("field %s has wrong range", field.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
nr, err := parseNumberRange(segs[1])
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldOpts.Range = nr
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,6 +271,95 @@ func parseNumberRange(str string) (*numberRange, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseOption(fieldOpts *fieldOptions, fieldName string, option string) error {
|
||||||
|
switch {
|
||||||
|
case option == stringOption:
|
||||||
|
fieldOpts.FromString = true
|
||||||
|
case strings.HasPrefix(option, optionalOption):
|
||||||
|
segs := strings.Split(option, equalToken)
|
||||||
|
switch len(segs) {
|
||||||
|
case 1:
|
||||||
|
fieldOpts.Optional = true
|
||||||
|
case 2:
|
||||||
|
fieldOpts.Optional = true
|
||||||
|
fieldOpts.OptionalDep = segs[1]
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("field %s has wrong optional", fieldName)
|
||||||
|
}
|
||||||
|
case option == optionalOption:
|
||||||
|
fieldOpts.Optional = true
|
||||||
|
case strings.HasPrefix(option, optionsOption):
|
||||||
|
segs := strings.Split(option, equalToken)
|
||||||
|
if len(segs) != 2 {
|
||||||
|
return fmt.Errorf("field %s has wrong options", fieldName)
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldOpts.Options = strings.Split(segs[1], optionSeparator)
|
||||||
|
case strings.HasPrefix(option, defaultOption):
|
||||||
|
segs := strings.Split(option, equalToken)
|
||||||
|
if len(segs) != 2 {
|
||||||
|
return fmt.Errorf("field %s has wrong default option", fieldName)
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldOpts.Default = strings.TrimSpace(segs[1])
|
||||||
|
case strings.HasPrefix(option, rangeOption):
|
||||||
|
segs := strings.Split(option, equalToken)
|
||||||
|
if len(segs) != 2 {
|
||||||
|
return fmt.Errorf("field %s has wrong range", fieldName)
|
||||||
|
}
|
||||||
|
|
||||||
|
nr, err := parseNumberRange(segs[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldOpts.Range = nr
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func reprOfValue(val reflect.Value) string {
|
||||||
|
switch vt := val.Interface().(type) {
|
||||||
|
case bool:
|
||||||
|
return strconv.FormatBool(vt)
|
||||||
|
case error:
|
||||||
|
return vt.Error()
|
||||||
|
case float32:
|
||||||
|
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
|
||||||
|
case float64:
|
||||||
|
return strconv.FormatFloat(vt, 'f', -1, 64)
|
||||||
|
case fmt.Stringer:
|
||||||
|
return vt.String()
|
||||||
|
case int:
|
||||||
|
return strconv.Itoa(vt)
|
||||||
|
case int8:
|
||||||
|
return strconv.Itoa(int(vt))
|
||||||
|
case int16:
|
||||||
|
return strconv.Itoa(int(vt))
|
||||||
|
case int32:
|
||||||
|
return strconv.Itoa(int(vt))
|
||||||
|
case int64:
|
||||||
|
return strconv.FormatInt(vt, 10)
|
||||||
|
case string:
|
||||||
|
return vt
|
||||||
|
case uint:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint8:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint16:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint32:
|
||||||
|
return strconv.FormatUint(uint64(vt), 10)
|
||||||
|
case uint64:
|
||||||
|
return strconv.FormatUint(vt, 10)
|
||||||
|
case []byte:
|
||||||
|
return string(vt)
|
||||||
|
default:
|
||||||
|
return fmt.Sprint(val.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interface{}) error {
|
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interface{}) error {
|
||||||
switch kind {
|
switch kind {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
|
|||||||
@@ -77,32 +77,40 @@ func (t *Tree) next(n *node, route string, result *Result) bool {
|
|||||||
for i := range route {
|
for i := range route {
|
||||||
if route[i] == slash {
|
if route[i] == slash {
|
||||||
token := route[:i]
|
token := route[:i]
|
||||||
for _, children := range n.children {
|
return n.forEach(func(k string, v *node) bool {
|
||||||
for k, v := range children {
|
if r := match(k, token); r.found {
|
||||||
if r := match(k, token); r.found {
|
if t.next(v, route[i+1:], result) {
|
||||||
if t.next(v, route[i+1:], result) {
|
if r.named {
|
||||||
if r.named {
|
addParam(result, r.key, r.value)
|
||||||
addParam(result, r.key, r.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, children := range n.children {
|
return n.forEach(func(k string, v *node) bool {
|
||||||
for k, v := range children {
|
if r := match(k, route); r.found && v.item != nil {
|
||||||
if r := match(k, route); r.found && v.item != nil {
|
result.Item = v.item
|
||||||
result.Item = v.item
|
if r.named {
|
||||||
if r.named {
|
addParam(result, r.key, r.value)
|
||||||
addParam(result, r.key, r.value)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nd *node) forEach(fn func(string, *node) bool) bool {
|
||||||
|
for _, children := range nd.children {
|
||||||
|
for k, v := range children {
|
||||||
|
if fn(k, v) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user