-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add support for float type in mapstriface #6870
Changes from 1 commit
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 |
---|---|---|
|
@@ -193,9 +193,43 @@ func toInteger(key string, data map[string]interface{}) (interface{}, error) { | |
if err == nil { | ||
return int64(f64), nil | ||
} | ||
return 0, fmt.Errorf("Expected integer, found json.Number (%v) that cannot be converted", num) | ||
return 0, fmt.Errorf("expected integer, found json.Number (%v) that cannot be converted", num) | ||
default: | ||
return 0, fmt.Errorf("Expected integer, found %T", emptyIface) | ||
return 0, fmt.Errorf("expected integer, found %T", emptyIface) | ||
} | ||
} | ||
|
||
// Float creates a Conv object for converting floats. Acceptable input | ||
// types are int64, int, and float64. | ||
func Float(key string, opts ...schema.SchemaOption) schema.Conv { | ||
return schema.SetOptions(schema.Conv{Key: key, Func: toFloat}, opts) | ||
} | ||
|
||
func toFloat(key string, data map[string]interface{}) (interface{}, error) { | ||
emptyIface, exists := data[key] | ||
if !exists { | ||
return 0, fmt.Errorf("Key %s not found", key) | ||
} | ||
switch emptyIface.(type) { | ||
case float64: | ||
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. Should 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. +1 on parsing string via 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. So far we don't need it and it's also not supported in the Int type. I would add it when we need it. 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. When I've reviewed this, I've made the same conclusion by look at the @andrewkroh @urso for me its not necessary right now, wdyt? 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. That’s fine by me to omit it for now. It’s an easy add at any point. |
||
return emptyIface.(float64), nil | ||
case int: | ||
return float64(emptyIface.(int)), nil | ||
case int64: | ||
return float64(emptyIface.(int64)), nil | ||
case json.Number: | ||
num := emptyIface.(json.Number) | ||
i64, err := num.Float64() | ||
if err == nil { | ||
return i64, nil | ||
} | ||
f64, err := num.Float64() | ||
if err == nil { | ||
return f64, nil | ||
} | ||
return 0, fmt.Errorf("expected float, found json.Number (%v) that cannot be converted", num) | ||
default: | ||
return 0, fmt.Errorf("expected float, found %T", emptyIface) | ||
} | ||
} | ||
|
||
|
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.
s/Key/key/
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.
changed