Skip to content

Commit

Permalink
Reduce the complexity of refelct functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Dec 21, 2023
1 parent 0069819 commit d35ad22
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions helper/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ import (
"time"
)

// kindToBits maps numeric kinds (e.g., int, float) to their corresponding sizes in bits.
var kindToBits = map[reflect.Kind]int{
reflect.Int: strconv.IntSize,
reflect.Int8: 8,
reflect.Int16: 16,
reflect.Int32: 32,
reflect.Int64: 64,
reflect.Uint: bits.UintSize,
reflect.Uint16: 16,
reflect.Uint32: 32,
reflect.Uint64: 64,
reflect.Float32: 32,
reflect.Float64: 64,
}

// setReflectValueFromBool assigns the parsed boolean value to the specified variable.
func setReflectValueFromBool(value reflect.Value, stringValue string) error {
actualValue, err := strconv.ParseBool(stringValue)
Expand Down Expand Up @@ -74,41 +89,14 @@ func setReflectValue(value reflect.Value, stringValue string, format string) err
case reflect.Bool:
return setReflectValueFromBool(value, stringValue)

case reflect.Int:
return setReflectValueFromInt(value, stringValue, bits.UintSize)

case reflect.Int8:
return setReflectValueFromInt(value, stringValue, 8)

case reflect.Int16:
return setReflectValueFromInt(value, stringValue, 16)

case reflect.Int32:
return setReflectValueFromInt(value, stringValue, 32)

case reflect.Int64:
return setReflectValueFromInt(value, stringValue, 64)

case reflect.Uint:
return setReflectValueFromUint(value, stringValue, bits.UintSize)

case reflect.Uint8:
return setReflectValueFromUint(value, stringValue, 8)

case reflect.Uint16:
return setReflectValueFromUint(value, stringValue, 16)

case reflect.Uint32:
return setReflectValueFromUint(value, stringValue, 32)

case reflect.Uint64:
return setReflectValueFromUint(value, stringValue, 64)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setReflectValueFromInt(value, stringValue, kindToBits[kind])

case reflect.Float32:
return setReflectValueFromFloat(value, stringValue, 32)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setReflectValueFromUint(value, stringValue, kindToBits[kind])

case reflect.Float64:
return setReflectValueFromFloat(value, stringValue, 64)
case reflect.Float32, reflect.Float64:
return setReflectValueFromFloat(value, stringValue, kindToBits[kind])

case reflect.Struct:
typeString := value.Type().String()
Expand Down Expand Up @@ -143,11 +131,8 @@ func getReflectValue(value reflect.Value, format string) (string, error) {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(value.Uint(), 10), nil

case reflect.Float32:
return strconv.FormatFloat(value.Float(), 'g', -1, 32), nil

case reflect.Float64:
return strconv.FormatFloat(value.Float(), 'g', -1, 64), nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(value.Float(), 'g', -1, kindToBits[kind]), nil

case reflect.Struct:
typeString := value.Type().String()
Expand Down

0 comments on commit d35ad22

Please sign in to comment.