From a050385fe525ab82208ab910cbbf603c92fb94b3 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Thu, 17 Jul 2014 17:05:10 +0300 Subject: [PATCH] Changed the cast methods - Added "Check" prefix to the ones that also return an error. - Removed the "Must" prefix from those that always return a valid value. fixes issue #15 --- simplejson.go | 140 ++++++++++++++++++++-------------------- simplejson_go10.go | 12 ++-- simplejson_go10_test.go | 16 ++--- simplejson_go11.go | 16 ++--- simplejson_go11_test.go | 20 +++--- simplejson_test.go | 76 +++++++++++----------- 6 files changed, 140 insertions(+), 140 deletions(-) diff --git a/simplejson.go b/simplejson.go index 3e8c6eb..9307005 100644 --- a/simplejson.go +++ b/simplejson.go @@ -33,8 +33,8 @@ func New() *Json { } } -// Interface returns the underlying data -func (j *Json) Interface() interface{} { +// CheckInterface returns the underlying data +func (j *Json) CheckInterface() interface{} { return j.data } @@ -56,7 +56,7 @@ func (j *Json) MarshalJSON() ([]byte, error) { // Set modifies `Json` map by `key` and `value` // Useful for changing single key/value in a `Json` object easily. func (j *Json) Set(key string, val interface{}) { - m, err := j.Map() + m, err := j.CheckMap() if err != nil { return } @@ -104,7 +104,7 @@ func (j *Json) SetPath(branch []string, val interface{}) { // Del modifies `Json` map by deleting `key` if it is present. func (j *Json) Del(key string) { - m, err := j.Map() + m, err := j.CheckMap() if err != nil { return } @@ -115,7 +115,7 @@ func (j *Json) Del(key string) { // for `key` in its `map` representation // and a bool identifying success or failure func (j *Json) getKey(key string) (*Json, bool) { - m, err := j.Map() + m, err := j.CheckMap() if err == nil { if val, ok := m[key]; ok { return &Json{val}, true @@ -128,7 +128,7 @@ func (j *Json) getKey(key string) (*Json, bool) { // for `index` in its `array` representation // and a bool identifying success or failure func (j *Json) getIndex(index int) (*Json, bool) { - a, err := j.Array() + a, err := j.CheckArray() if err == nil { if len(a) > index { return &Json{a[index]}, true @@ -175,9 +175,9 @@ func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { return jin, true } -// JsonMap returns a copy of a Json map, but with values as Jsons -func (j *Json) JsonMap() (map[string]*Json, error) { - m, err := j.Map() +// CheckJsonMap returns a copy of a Json map, but with values as Jsons +func (j *Json) CheckJsonMap() (map[string]*Json, error) { + m, err := j.CheckMap() if err != nil { return nil, err } @@ -188,9 +188,9 @@ func (j *Json) JsonMap() (map[string]*Json, error) { return jm, nil } -// JsonArray returns a copy of an array, but with each value as a Json -func (j *Json) JsonArray() ([]*Json, error) { - a, err := j.Array() +// CheckJsonArray returns a copy of an array, but with each value as a Json +func (j *Json) CheckJsonArray() ([]*Json, error) { + a, err := j.CheckArray() if err != nil { return nil, err } @@ -201,48 +201,48 @@ func (j *Json) JsonArray() ([]*Json, error) { return ja, nil } -// Map type asserts to `map` -func (j *Json) Map() (map[string]interface{}, error) { +// CheckMap type asserts to `map` +func (j *Json) CheckMap() (map[string]interface{}, error) { if m, ok := (j.data).(map[string]interface{}); ok { return m, nil } return nil, errors.New("type assertion to map[string]interface{} failed") } -// Array type asserts to an `array` -func (j *Json) Array() ([]interface{}, error) { +// CheckArray type asserts to an `array` +func (j *Json) CheckArray() ([]interface{}, error) { if a, ok := (j.data).([]interface{}); ok { return a, nil } return nil, errors.New("type assertion to []interface{} failed") } -// Bool type asserts to `bool` -func (j *Json) Bool() (bool, error) { +// CheckBool type asserts to `bool` +func (j *Json) CheckBool() (bool, error) { if s, ok := (j.data).(bool); ok { return s, nil } return false, errors.New("type assertion to bool failed") } -// String type asserts to `string` -func (j *Json) String() (string, error) { +// CheckString type asserts to `string` +func (j *Json) CheckString() (string, error) { if s, ok := (j.data).(string); ok { return s, nil } return "", errors.New("type assertion to string failed") } -// Bytes type asserts to `[]byte` -func (j *Json) Bytes() ([]byte, error) { +// CheckBytes type asserts to `[]byte` +func (j *Json) CheckBytes() ([]byte, error) { if s, ok := (j.data).(string); ok { return []byte(s), nil } return nil, errors.New("type assertion to []byte failed") } -// MustJsonArray guarantees the return of a `[]interface{}` (with optional default) -func (j *Json) MustJsonArray(args ...[]*Json) []*Json { +// JsonArray guarantees the return of a `[]interface{}` (with optional default) +func (j *Json) JsonArray(args ...[]*Json) []*Json { var def []*Json switch len(args) { @@ -250,10 +250,10 @@ func (j *Json) MustJsonArray(args ...[]*Json) []*Json { case 1: def = args[0] default: - log.Panicf("MustJsonArray() received too many arguments %d", len(args)) + log.Panicf("JsonArray() received too many arguments %d", len(args)) } - a, err := j.JsonArray() + a, err := j.CheckJsonArray() if err == nil { return a } @@ -261,8 +261,8 @@ func (j *Json) MustJsonArray(args ...[]*Json) []*Json { return def } -// MustJsonMap guarantees the return of a `map[string]interface{}` (with optional default) -func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { +// JsonMap guarantees the return of a `map[string]interface{}` (with optional default) +func (j *Json) JsonMap(args ...map[string]*Json) map[string]*Json { var def map[string]*Json switch len(args) { @@ -270,10 +270,10 @@ func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { case 1: def = args[0] default: - log.Panicf("MustJsonMap() received too many arguments %d", len(args)) + log.Panicf("JsonMap() received too many arguments %d", len(args)) } - a, err := j.JsonMap() + a, err := j.CheckJsonMap() if err == nil { return a } @@ -281,13 +281,13 @@ func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { return def } -// MustArray guarantees the return of a `[]interface{}` (with optional default) +// Array guarantees the return of a `[]interface{}` (with optional default) // // useful when you want to interate over array values in a succinct manner: -// for i, v := range js.Get("results").MustArray() { +// for i, v := range js.Get("results").Array() { // fmt.Println(i, v) // } -func (j *Json) MustArray(args ...[]interface{}) []interface{} { +func (j *Json) Array(args ...[]interface{}) []interface{} { var def []interface{} switch len(args) { @@ -295,10 +295,10 @@ func (j *Json) MustArray(args ...[]interface{}) []interface{} { case 1: def = args[0] default: - log.Panicf("MustArray() received too many arguments %d", len(args)) + log.Panicf("Array() received too many arguments %d", len(args)) } - a, err := j.Array() + a, err := j.CheckArray() if err == nil { return a } @@ -306,13 +306,13 @@ func (j *Json) MustArray(args ...[]interface{}) []interface{} { return def } -// MustMap guarantees the return of a `map[string]interface{}` (with optional default) +// Map guarantees the return of a `map[string]interface{}` (with optional default) // // useful when you want to interate over map values in a succinct manner: -// for k, v := range js.Get("dictionary").MustMap() { +// for k, v := range js.Get("dictionary").Map() { // fmt.Println(k, v) // } -func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { +func (j *Json) Map(args ...map[string]interface{}) map[string]interface{} { var def map[string]interface{} switch len(args) { @@ -320,10 +320,10 @@ func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { case 1: def = args[0] default: - log.Panicf("MustMap() received too many arguments %d", len(args)) + log.Panicf("Map() received too many arguments %d", len(args)) } - a, err := j.Map() + a, err := j.CheckMap() if err == nil { return a } @@ -331,11 +331,11 @@ func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { return def } -// MustString guarantees the return of a `string` (with optional default) +// String guarantees the return of a `string` (with optional default) // // useful when you explicitly want a `string` in a single value return context: -// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default")) -func (j *Json) MustString(args ...string) string { +// myFunc(js.Get("param1").String(), js.Get("optional_param").String("my_default")) +func (j *Json) String(args ...string) string { var def string switch len(args) { @@ -343,10 +343,10 @@ func (j *Json) MustString(args ...string) string { case 1: def = args[0] default: - log.Panicf("MustString() received too many arguments %d", len(args)) + log.Panicf("String() received too many arguments %d", len(args)) } - s, err := j.String() + s, err := j.CheckString() if err == nil { return s } @@ -354,11 +354,11 @@ func (j *Json) MustString(args ...string) string { return def } -// MustInt guarantees the return of an `int` (with optional default) +// Int guarantees the return of an `int` (with optional default) // // useful when you explicitly want an `int` in a single value return context: -// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150)) -func (j *Json) MustInt(args ...int) int { +// myFunc(js.Get("param1").Int(), js.Get("optional_param").Int(5150)) +func (j *Json) Int(args ...int) int { var def int switch len(args) { @@ -366,10 +366,10 @@ func (j *Json) MustInt(args ...int) int { case 1: def = args[0] default: - log.Panicf("MustInt() received too many arguments %d", len(args)) + log.Panicf("Int() received too many arguments %d", len(args)) } - i, err := j.Int() + i, err := j.CheckInt() if err == nil { return i } @@ -377,11 +377,11 @@ func (j *Json) MustInt(args ...int) int { return def } -// MustFloat64 guarantees the return of a `float64` (with optional default) +// Float64 guarantees the return of a `float64` (with optional default) // // useful when you explicitly want a `float64` in a single value return context: -// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150)) -func (j *Json) MustFloat64(args ...float64) float64 { +// myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(5.150)) +func (j *Json) Float64(args ...float64) float64 { var def float64 switch len(args) { @@ -389,10 +389,10 @@ func (j *Json) MustFloat64(args ...float64) float64 { case 1: def = args[0] default: - log.Panicf("MustFloat64() received too many arguments %d", len(args)) + log.Panicf("Float64() received too many arguments %d", len(args)) } - f, err := j.Float64() + f, err := j.CheckFloat64() if err == nil { return f } @@ -400,11 +400,11 @@ func (j *Json) MustFloat64(args ...float64) float64 { return def } -// MustBool guarantees the return of a `bool` (with optional default) +// Bool guarantees the return of a `bool` (with optional default) // // useful when you explicitly want a `bool` in a single value return context: -// myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true)) -func (j *Json) MustBool(args ...bool) bool { +// myFunc(js.Get("param1").Bool(), js.Get("optional_param").Bool(true)) +func (j *Json) Bool(args ...bool) bool { var def bool switch len(args) { @@ -412,10 +412,10 @@ func (j *Json) MustBool(args ...bool) bool { case 1: def = args[0] default: - log.Panicf("MustBool() received too many arguments %d", len(args)) + log.Panicf("Bool() received too many arguments %d", len(args)) } - b, err := j.Bool() + b, err := j.CheckBool() if err == nil { return b } @@ -423,11 +423,11 @@ func (j *Json) MustBool(args ...bool) bool { return def } -// MustInt64 guarantees the return of an `int64` (with optional default) +// Int64 guarantees the return of an `int64` (with optional default) // // useful when you explicitly want an `int64` in a single value return context: -// myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150)) -func (j *Json) MustInt64(args ...int64) int64 { +// myFunc(js.Get("param1").Int64(), js.Get("optional_param").Int64(5150)) +func (j *Json) Int64(args ...int64) int64 { var def int64 switch len(args) { @@ -435,10 +435,10 @@ func (j *Json) MustInt64(args ...int64) int64 { case 1: def = args[0] default: - log.Panicf("MustInt64() received too many arguments %d", len(args)) + log.Panicf("Int64() received too many arguments %d", len(args)) } - i, err := j.Int64() + i, err := j.CheckInt64() if err == nil { return i } @@ -446,11 +446,11 @@ func (j *Json) MustInt64(args ...int64) int64 { return def } -// MustUInt64 guarantees the return of an `uint64` (with optional default) +// UInt64 guarantees the return of an `uint64` (with optional default) // // useful when you explicitly want an `uint64` in a single value return context: -// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150)) -func (j *Json) MustUint64(args ...uint64) uint64 { +// myFunc(js.Get("param1").Uint64(), js.Get("optional_param").Uint64(5150)) +func (j *Json) Uint64(args ...uint64) uint64 { var def uint64 switch len(args) { @@ -458,10 +458,10 @@ func (j *Json) MustUint64(args ...uint64) uint64 { case 1: def = args[0] default: - log.Panicf("MustUint64() received too many arguments %d", len(args)) + log.Panicf("Uint64() received too many arguments %d", len(args)) } - i, err := j.Uint64() + i, err := j.CheckUint64() if err == nil { return i } diff --git a/simplejson_go10.go b/simplejson_go10.go index c9151e9..14280dc 100644 --- a/simplejson_go10.go +++ b/simplejson_go10.go @@ -22,8 +22,8 @@ func (j *Json) UnmarshalJSON(p []byte) error { return json.Unmarshal(p, &j.data) } -// Float64 coerces into a float64 -func (j *Json) Float64() (float64, error) { +// CheckFloat64 coerces into a float64 +func (j *Json) CheckFloat64() (float64, error) { switch j.data.(type) { case float32, float64: return reflect.ValueOf(j.data).Float(), nil @@ -48,8 +48,8 @@ func (j *Json) Int() (int, error) { return 0, errors.New("invalid value type") } -// Int64 coerces into an int64 -func (j *Json) Int64() (int64, error) { +// CheckInt64 coerces into an int64 +func (j *Json) CheckInt64() (int64, error) { switch j.data.(type) { case float32, float64: return int64(reflect.ValueOf(j.data).Float()), nil @@ -61,8 +61,8 @@ func (j *Json) Int64() (int64, error) { return 0, errors.New("invalid value type") } -// Uint64 coerces into an uint64 -func (j *Json) Uint64() (uint64, error) { +// CheckUint64 coerces into an uint64 +func (j *Json) CheckUint64() (uint64, error) { switch j.data.(type) { case float32, float64: return uint64(reflect.ValueOf(j.data).Float()), nil diff --git a/simplejson_go10_test.go b/simplejson_go10_test.go index 28c3595..e925f8a 100644 --- a/simplejson_go10_test.go +++ b/simplejson_go10_test.go @@ -26,7 +26,7 @@ func TestNewFromReader(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -39,13 +39,13 @@ func TestNewFromReader(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000)) } func TestSimplejsonGo10(t *testing.T) { @@ -63,7 +63,7 @@ func TestSimplejsonGo10(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -76,11 +76,11 @@ func TestSimplejsonGo10(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000)) } diff --git a/simplejson_go11.go b/simplejson_go11.go index 1c47953..8adaf97 100644 --- a/simplejson_go11.go +++ b/simplejson_go11.go @@ -27,8 +27,8 @@ func NewFromReader(r io.Reader) (*Json, error) { return j, err } -// Float64 coerces into a float64 -func (j *Json) Float64() (float64, error) { +// CheckFloat64 coerces into a float64 +func (j *Json) CheckFloat64() (float64, error) { switch j.data.(type) { case json.Number: return j.data.(json.Number).Float64() @@ -42,8 +42,8 @@ func (j *Json) Float64() (float64, error) { return 0, errors.New("invalid value type") } -// Int coerces into an int -func (j *Json) Int() (int, error) { +// CheckInt coerces into an int +func (j *Json) CheckInt() (int, error) { switch j.data.(type) { case json.Number: i, err := j.data.(json.Number).Int64() @@ -58,8 +58,8 @@ func (j *Json) Int() (int, error) { return 0, errors.New("invalid value type") } -// Int64 coerces into an int64 -func (j *Json) Int64() (int64, error) { +// CheckInt64 coerces into an int64 +func (j *Json) CheckInt64() (int64, error) { switch j.data.(type) { case json.Number: return j.data.(json.Number).Int64() @@ -73,8 +73,8 @@ func (j *Json) Int64() (int64, error) { return 0, errors.New("invalid value type") } -// Uint64 coerces into an uint64 -func (j *Json) Uint64() (uint64, error) { +// CheckUint64 coerces into an uint64 +func (j *Json) CheckUint64() (uint64, error) { switch j.data.(type) { case json.Number: return strconv.ParseUint(j.data.(json.Number).String(), 10, 64) diff --git a/simplejson_go11_test.go b/simplejson_go11_test.go index 196d744..93e4de2 100644 --- a/simplejson_go11_test.go +++ b/simplejson_go11_test.go @@ -29,7 +29,7 @@ func TestNewFromReader(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -44,14 +44,14 @@ func TestNewFromReader(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) - assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(9223372036854775807)) + assert.Equal(t, js.Get("test").Get("uint64").Uint64(), uint64(18446744073709551615)) } func TestSimplejsonGo11(t *testing.T) { @@ -70,7 +70,7 @@ func TestSimplejsonGo11(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -85,12 +85,12 @@ func TestSimplejsonGo11(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) - assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(9223372036854775807)) + assert.Equal(t, js.Get("test").Get("uint64").Uint64(), uint64(18446744073709551615)) } diff --git a/simplejson_test.go b/simplejson_test.go index 7bfc539..6a1ae82 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -38,86 +38,86 @@ func TestSimplejson(t *testing.T) { aws := js.Get("test").Get("arraywithsubs") assert.NotEqual(t, nil, aws) var awsval int - awsval, _ = aws.Get(0).Get("subkeyone").Int() + awsval, _ = aws.Get(0).Get("subkeyone").CheckInt() assert.Equal(t, 1, awsval) - awsval, _ = aws.Get(1).Get("subkeytwo").Int() + awsval, _ = aws.Get(1).Get("subkeytwo").CheckInt() assert.Equal(t, 2, awsval) - awsval, _ = aws.Get(1).Get("subkeythree").Int() + awsval, _ = aws.Get(1).Get("subkeythree").CheckInt() assert.Equal(t, 3, awsval) - i, _ := js.Get("test").Get("int").Int() + i, _ := js.Get("test").Get("int").CheckInt() assert.Equal(t, 10, i) - f, _ := js.Get("test").Get("float").Float64() + f, _ := js.Get("test").Get("float").CheckFloat64() assert.Equal(t, 5.150, f) - s, _ := js.Get("test").Get("string").String() + s, _ := js.Get("test").Get("string").CheckString() assert.Equal(t, "simplejson", s) - b, _ := js.Get("test").Get("bool").Bool() + b, _ := js.Get("test").Get("bool").CheckBool() assert.Equal(t, true, b) - mi := js.Get("test").Get("int").MustInt() + mi := js.Get("test").Get("int").Int() assert.Equal(t, 10, mi) - mi2 := js.Get("test").Get("missing_int").MustInt(5150) + mi2 := js.Get("test").Get("missing_int").Int(5150) assert.Equal(t, 5150, mi2) - ms := js.Get("test").Get("string").MustString() + ms := js.Get("test").Get("string").String() assert.Equal(t, "simplejson", ms) - ms2 := js.Get("test").Get("missing_string").MustString("fyea") + ms2 := js.Get("test").Get("missing_string").String("fyea") assert.Equal(t, "fyea", ms2) - ma2 := js.Get("test").Get("missing_array").MustArray([]interface{}{"1", 2, "3"}) + ma2 := js.Get("test").Get("missing_array").Array([]interface{}{"1", 2, "3"}) assert.Equal(t, ma2, []interface{}{"1", 2, "3"}) - mm2 := js.Get("test").Get("missing_map").MustMap(map[string]interface{}{"found": false}) + mm2 := js.Get("test").Get("missing_map").Map(map[string]interface{}{"found": false}) assert.Equal(t, mm2, map[string]interface{}{"found": false}) - gp, _ := js.Get("test", "string").String() + gp, _ := js.Get("test", "string").CheckString() assert.Equal(t, "simplejson", gp) - gp2, _ := js.Get("test", "int").Int() + gp2, _ := js.Get("test", "int").CheckInt() assert.Equal(t, 10, gp2) - gpa, _ := js.Get("test", "string_array", 0).String() + gpa, _ := js.Get("test", "string_array", 0).CheckString() assert.Equal(t, "asdf", gpa) - gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").Int() + gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").CheckInt() assert.Equal(t, 3, gpa2) - jm, err := js.Get("test").JsonMap() + jm, err := js.Get("test").CheckJsonMap() assert.Equal(t, err, nil) - jmbool, _ := jm["bool"].Bool() + jmbool, _ := jm["bool"].CheckBool() assert.Equal(t, true, jmbool) - ja, err := js.Get("test", "string_array").JsonArray() + ja, err := js.Get("test", "string_array").CheckJsonArray() assert.Equal(t, err, nil) - jastr, _ := ja[0].String() + jastr, _ := ja[0].CheckString() assert.Equal(t, "asdf", jastr) - assert.Equal(t, js.Get("test").Get("bool").MustBool(), true) + assert.Equal(t, js.Get("test").Get("bool").Bool(), true) js.Set("float2", 300.0) - assert.Equal(t, js.Get("float2").MustFloat64(), 300.0) + assert.Equal(t, js.Get("float2").Float64(), 300.0) js.Set("test2", "setTest") - assert.Equal(t, "setTest", js.Get("test2").MustString()) + assert.Equal(t, "setTest", js.Get("test2").String()) js.Del("test2") - assert.NotEqual(t, "setTest", js.Get("test2").MustString()) + assert.NotEqual(t, "setTest", js.Get("test2").String()) js.Get("test").Get("sub_obj").Set("a", 2) - assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").MustInt()) + assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").Int()) js.Get("test", "sub_obj").Set("a", 3) - assert.Equal(t, 3, js.Get("test", "sub_obj", "a").MustInt()) + assert.Equal(t, 3, js.Get("test", "sub_obj", "a").Int()) - jmm := js.Get("missing_map").MustJsonMap(map[string]*Json{"js1": js}) + jmm := js.Get("missing_map").JsonMap(map[string]*Json{"js1": js}) assert.Equal(t, js, jmm["js1"]) - jma := js.Get("missing_array").MustJsonArray([]*Json{js}) + jma := js.Get("missing_array").JsonArray([]*Json{js}) assert.Equal(t, js, jma[0]) } @@ -137,7 +137,7 @@ func TestStdlibInterfaces(t *testing.T) { assert.Equal(t, "myobject", val.Name) assert.NotEqual(t, nil, val.Params.data) - s, _ := val.Params.Get("string").String() + s, _ := val.Params.Get("string").CheckString() assert.Equal(t, "simplejson", s) p, err := json.Marshal(val) @@ -152,7 +152,7 @@ func TestSet(t *testing.T) { js.Set("baz", "bing") - s, err := js.Get("baz").String() + s, err := js.Get("baz").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bing", s) } @@ -164,7 +164,7 @@ func TestReplace(t *testing.T) { err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`)) assert.Equal(t, nil, err) - s, err := js.Get("baz").String() + s, err := js.Get("baz").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bing", s) } @@ -175,7 +175,7 @@ func TestSetPath(t *testing.T) { js.SetPath([]string{"foo", "bar"}, "baz") - s, err := js.Get("foo", "bar").String() + s, err := js.Get("foo", "bar").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "baz", s) } @@ -184,16 +184,16 @@ func TestSetPathNoPath(t *testing.T) { js, err := NewJson([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`)) assert.Equal(t, nil, err) - f := js.Get("some_number").MustFloat64(99.0) + f := js.Get("some_number").Float64(99.0) assert.Equal(t, f, 1.0) js.SetPath([]string{}, map[string]interface{}{"foo": "bar"}) - s, err := js.Get("foo").String() + s, err := js.Get("foo").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bar", s) - f = js.Get("some_number").MustFloat64(99.0) + f = js.Get("some_number").Float64(99.0) assert.Equal(t, f, 99.0) } @@ -226,7 +226,7 @@ func TestPathWillAugmentExisting(t *testing.T) { } for _, tc := range cases { - s, err := js.Get(tc.path...).String() + s, err := js.Get(tc.path...).CheckString() assert.Equal(t, nil, err) assert.Equal(t, tc.outcome, s) } @@ -239,7 +239,7 @@ func TestPathWillOverwriteExisting(t *testing.T) { js.SetPath([]string{"this", "a", "foo"}, "bar") - s, err := js.Get("this", "a", "foo").String() + s, err := js.Get("this", "a", "foo").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bar", s) }