-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to use integer literals #19
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: go | ||
go: 1.11 | ||
go: 1.13 | ||
|
||
branches: | ||
only: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,13 @@ var ( | |
|
||
const ( | ||
fieldName = "default" | ||
|
||
autoBase = 0 | ||
|
||
bitSize8 = 8 | ||
bitSize16 = 16 | ||
bitSize32 = 32 | ||
bitSize64 = 64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really see the point in defining these constants... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i agree with bitXXSize constants , but autoBase add context to 0 value in base but it is not so significant therefore I will change that |
||
) | ||
|
||
// Set initializes members in a struct referenced by a pointer. | ||
|
@@ -58,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, 10, 64); err == nil { | ||
if val, err := strconv.ParseInt(defaultVal, autoBase, strconv.IntSize); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nice 👍 |
||
field.Set(reflect.ValueOf(int(val)).Convert(field.Type())) | ||
} | ||
case reflect.Int8: | ||
if val, err := strconv.ParseInt(defaultVal, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/creasty/defaults | ||
|
||
go 1.11 | ||
go 1.13 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self:
"This PR require at least go1.13"
https://golang.org/doc/go1.13#language