diff --git a/defaults.go b/defaults.go index 8ab1150..e4ff5e8 100644 --- a/defaults.go +++ b/defaults.go @@ -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. @@ -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 } @@ -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: