1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-28 17:03:58 +08:00

commands: Fixed handling of int/uint option values

This commit is contained in:
Matt Bell
2014-11-10 22:41:01 -08:00
committed by Juan Batiz-Benet
parent 17c5923160
commit a9bd172414

View File

@ -129,10 +129,18 @@ var converters = map[reflect.Kind]converter{
return strconv.ParseBool(v) return strconv.ParseBool(v)
}, },
Int: func(v string) (interface{}, error) { Int: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 0, 32) val, err := strconv.ParseInt(v, 0, 32)
if err != nil {
return nil, err
}
return int(val), err
}, },
Uint: func(v string) (interface{}, error) { Uint: func(v string) (interface{}, error) {
return strconv.ParseInt(v, 0, 32) val, err := strconv.ParseUint(v, 0, 32)
if err != nil {
return nil, err
}
return int(val), err
}, },
Float: func(v string) (interface{}, error) { Float: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64) return strconv.ParseFloat(v, 64)
@ -140,8 +148,6 @@ var converters = map[reflect.Kind]converter{
} }
func (r *request) ConvertOptions() error { func (r *request) ConvertOptions() error {
converted := make(map[string]interface{})
for k, v := range r.options { for k, v := range r.options {
opt, ok := r.optionDefs[k] opt, ok := r.optionDefs[k]
if !ok { if !ok {
@ -149,8 +155,6 @@ func (r *request) ConvertOptions() error {
} }
kind := reflect.TypeOf(v).Kind() kind := reflect.TypeOf(v).Kind()
var value interface{}
if kind != opt.Type { if kind != opt.Type {
if kind == String { if kind == String {
convert := converters[opt.Type] convert := converters[opt.Type]
@ -163,14 +167,14 @@ func (r *request) ConvertOptions() error {
return fmt.Errorf("Could not convert string value '%s' to type '%s'", return fmt.Errorf("Could not convert string value '%s' to type '%s'",
v, opt.Type.String()) v, opt.Type.String())
} }
value = val r.options[k] = val
} else { } else {
return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'", return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'",
k, opt.Type.String(), kind.String()) k, opt.Type.String(), kind.String())
} }
} else { } else {
value = v r.options[k] = v
} }
for _, name := range opt.Names { for _, name := range opt.Names {
@ -178,12 +182,9 @@ func (r *request) ConvertOptions() error {
return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')", return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')",
k, name) k, name)
} }
converted[name] = value
} }
} }
r.options = converted
return nil return nil
} }