From 1fc2dcea817fd6afefbffcc2b3f6382594bdd32b Mon Sep 17 00:00:00 2001 From: Lev Berman Date: Mon, 21 Sep 2015 14:08:06 +0300 Subject: [PATCH] Check if integers and floats are too big --- model_loader/model_loader.go | 20 ++++++++++++++++++-- model_loader/model_loader_test.go | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/model_loader/model_loader.go b/model_loader/model_loader.go index ede11dc..472c3ce 100644 --- a/model_loader/model_loader.go +++ b/model_loader/model_loader.go @@ -7,6 +7,7 @@ import ( "github.com/centurylinkcloud/clc-go-cli/base" "github.com/centurylinkcloud/clc-go-cli/parser" "reflect" + "strconv" "time" ) @@ -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 } } @@ -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 } } diff --git a/model_loader/model_loader_test.go b/model_loader/model_loader_test.go index c835926..1416fb8 100644 --- a/model_loader/model_loader_test.go +++ b/model_loader/model_loader_test.go @@ -5,6 +5,7 @@ import ( "github.com/centurylinkcloud/clc-go-cli/base" "github.com/centurylinkcloud/clc-go-cli/model_loader" "reflect" + "strings" "testing" "time" ) @@ -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{}{