Skip to content

Commit

Permalink
use constants for bit size especially int and uint
Browse files Browse the repository at this point in the history
  • Loading branch information
licaonfee committed Jul 16, 2020
1 parent cb916d0 commit a6133ac
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ var (

const (
fieldName = "default"

autoBase = 0

bitSize8 = 8
bitSize16 = 16
bitSize32 = 32
bitSize64 = 64
)

// Set initializes members in a struct referenced by a pointer.
Expand Down Expand Up @@ -43,7 +50,6 @@ func Set(ptr interface{}) error {
}

func setField(field reflect.Value, defaultVal string) error {
const defaultBase = 0
if !field.CanSet() {
return nil
}
Expand All @@ -59,57 +65,57 @@ func setField(field reflect.Value, defaultVal string) error {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
}
case reflect.Int:
if val, err := strconv.ParseInt(defaultVal, defaultBase, 64); err == nil {
if val, err := strconv.ParseInt(defaultVal, autoBase, strconv.IntSize); err == nil {
field.Set(reflect.ValueOf(int(val)).Convert(field.Type()))
}
case reflect.Int8:
if val, err := strconv.ParseInt(defaultVal, defaultBase, 8); err == nil {
if val, err := strconv.ParseInt(defaultVal, autoBase, bitSize8); err == nil {
field.Set(reflect.ValueOf(int8(val)).Convert(field.Type()))
}
case reflect.Int16:
if val, err := strconv.ParseInt(defaultVal, defaultBase, 16); err == nil {
if val, err := strconv.ParseInt(defaultVal, autoBase, bitSize16); err == nil {
field.Set(reflect.ValueOf(int16(val)).Convert(field.Type()))
}
case reflect.Int32:
if val, err := strconv.ParseInt(defaultVal, defaultBase, 32); err == nil {
if val, err := strconv.ParseInt(defaultVal, autoBase, bitSize32); err == nil {
field.Set(reflect.ValueOf(int32(val)).Convert(field.Type()))
}
case reflect.Int64:
if val, err := time.ParseDuration(defaultVal); err == nil {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
} else if val, err := strconv.ParseInt(defaultVal, defaultBase, 64); err == nil {
} else if val, err := strconv.ParseInt(defaultVal, autoBase, bitSize64); err == nil {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
}
case reflect.Uint:
if val, err := strconv.ParseUint(defaultVal, defaultBase, 64); err == nil {
if val, err := strconv.ParseUint(defaultVal, autoBase, strconv.IntSize); err == nil {
field.Set(reflect.ValueOf(uint(val)).Convert(field.Type()))
}
case reflect.Uint8:
if val, err := strconv.ParseUint(defaultVal, defaultBase, 8); err == nil {
if val, err := strconv.ParseUint(defaultVal, autoBase, bitSize8); err == nil {
field.Set(reflect.ValueOf(uint8(val)).Convert(field.Type()))
}
case reflect.Uint16:
if val, err := strconv.ParseUint(defaultVal, defaultBase, 16); err == nil {
if val, err := strconv.ParseUint(defaultVal, autoBase, bitSize16); err == nil {
field.Set(reflect.ValueOf(uint16(val)).Convert(field.Type()))
}
case reflect.Uint32:
if val, err := strconv.ParseUint(defaultVal, defaultBase, 32); err == nil {
if val, err := strconv.ParseUint(defaultVal, autoBase, bitSize32); err == nil {
field.Set(reflect.ValueOf(uint32(val)).Convert(field.Type()))
}
case reflect.Uint64:
if val, err := strconv.ParseUint(defaultVal, defaultBase, 64); err == nil {
if val, err := strconv.ParseUint(defaultVal, autoBase, bitSize64); err == nil {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
}
case reflect.Uintptr:
if val, err := strconv.ParseUint(defaultVal, defaultBase, 64); err == nil {
if val, err := strconv.ParseUint(defaultVal, autoBase, strconv.IntSize); err == nil {
field.Set(reflect.ValueOf(uintptr(val)).Convert(field.Type()))
}
case reflect.Float32:
if val, err := strconv.ParseFloat(defaultVal, 32); err == nil {
if val, err := strconv.ParseFloat(defaultVal, bitSize32); err == nil {
field.Set(reflect.ValueOf(float32(val)).Convert(field.Type()))
}
case reflect.Float64:
if val, err := strconv.ParseFloat(defaultVal, 64); err == nil {
if val, err := strconv.ParseFloat(defaultVal, bitSize64); err == nil {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
}
case reflect.String:
Expand Down

0 comments on commit a6133ac

Please sign in to comment.