Skip to content

Commit

Permalink
Check if integers and floats are too big
Browse files Browse the repository at this point in the history
  • Loading branch information
ldmberman committed Sep 21, 2015
1 parent 5e3bcc4 commit 1fc2dce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
20 changes: 18 additions & 2 deletions model_loader/model_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/centurylinkcloud/clc-go-cli/base"
"github.com/centurylinkcloud/clc-go-cli/parser"
"reflect"
"strconv"
"time"
)

Expand Down Expand Up @@ -50,7 +51,16 @@ func loadValue(key string, arg interface{}, field reflect.Value) error {
}
} else if reflect.ValueOf(arg).Kind() == reflect.String {
if valid.IsInt(arg.(string)) {
argInt, _ = valid.ToInt(arg.(string))
var err error
argInt, err = valid.ToInt(arg.(string))
if err != nil {
if num, ok := err.(*strconv.NumError); ok {
if num.Err == strconv.ErrRange {
return fmt.Errorf("Value `%s` is too big.", arg.(string))
}
}
return err
}
mismatch = false
}
}
Expand All @@ -68,7 +78,13 @@ func loadValue(key string, arg interface{}, field reflect.Value) error {
mismatch = false
} else if reflect.ValueOf(arg).Kind() == reflect.String {
if valid.IsFloat(arg.(string)) {
argFloat64, _ = valid.ToFloat(arg.(string))
var err error
argFloat64, err = valid.ToFloat(arg.(string))
if num, ok := err.(*strconv.NumError); ok {
if num.Err == strconv.ErrRange {
return fmt.Errorf("Value `%s` is too big.", arg.(string))
}
}
mismatch = false
}
}
Expand Down
14 changes: 14 additions & 0 deletions model_loader/model_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/centurylinkcloud/clc-go-cli/base"
"github.com/centurylinkcloud/clc-go-cli/model_loader"
"reflect"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -201,6 +202,19 @@ var testCases = []modelLoaderTestCase{
},
err: "Unknown option or argument: `UnknownField`.",
},
// Fails with numbers out of range.
{
args: map[string]interface{}{
"FieldInt": "99223372036854775808",
},
err: "Value `99223372036854775808` is too big.",
},
{
args: map[string]interface{}{
"FieldFloat": strings.Repeat("9", 310),
},
err: fmt.Sprintf("Value `%s` is too big.", strings.Repeat("9", 310)),
},
// Fails with different type mismatches.
{
args: map[string]interface{}{
Expand Down

0 comments on commit 1fc2dce

Please sign in to comment.