From 8d176ea6229f3583d7fd7a940ffe48c9cb708c7c Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Mon, 5 Jun 2023 22:52:17 -0400 Subject: [PATCH 1/3] tests: rewrite to drop assert package --- go.mod | 10 +- go.sum | 17 ++- simplejson.go | 54 +++++--- simplejson_go10_test.go | 3 +- simplejson_go11_test.go | 77 ++++++++--- simplejson_test.go | 287 ++++++++++++++++++++++++++-------------- 6 files changed, 301 insertions(+), 147 deletions(-) diff --git a/go.mod b/go.mod index 15a43ed..9462032 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,11 @@ module github.com/bitly/go-simplejson -go 1.13 +go 1.17 -require github.com/stretchr/testify v1.4.0 +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index e863f51..479781e 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,17 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/simplejson.go b/simplejson.go index 581b45d..486fe49 100644 --- a/simplejson.go +++ b/simplejson.go @@ -115,7 +115,8 @@ func (j *Json) Del(key string) { // for `key` in its `map` representation // // useful for chaining operations (to traverse a nested JSON): -// js.Get("top_level").Get("dict").Get("value").Int() +// +// js.Get("top_level").Get("dict").Get("value").Int() func (j *Json) Get(key string) *Json { m, err := j.Map() if err == nil { @@ -129,7 +130,7 @@ func (j *Json) Get(key string) *Json { // GetPath searches for the item as specified by the branch // without the need to deep dive using Get()'s. // -// js.GetPath("top_level", "dict") +// js.GetPath("top_level", "dict") func (j *Json) GetPath(branch ...string) *Json { jin := j for _, p := range branch { @@ -143,7 +144,8 @@ func (j *Json) GetPath(branch ...string) *Json { // // this is the analog to Get when accessing elements of // a json array instead of a json object: -// js.Get("top_level").Get("array").GetIndex(1).Get("key").Int() +// +// js.Get("top_level").Get("array").GetIndex(1).Get("key").Int() func (j *Json) GetIndex(index int) *Json { a, err := j.Array() if err == nil { @@ -158,9 +160,10 @@ func (j *Json) GetIndex(index int) *Json { // a `bool` identifying success or failure // // useful for chained operations when success is important: -// if data, ok := js.Get("top_level").CheckGet("inner"); ok { -// log.Println(data) -// } +// +// if data, ok := js.Get("top_level").CheckGet("inner"); ok { +// log.Println(data) +// } func (j *Json) CheckGet(key string) (*Json, bool) { m, err := j.Map() if err == nil { @@ -235,9 +238,10 @@ func (j *Json) StringArray() ([]string, error) { // MustArray 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() { -// fmt.Println(i, v) -// } +// +// for i, v := range js.Get("results").MustArray() { +// fmt.Println(i, v) +// } func (j *Json) MustArray(args ...[]interface{}) []interface{} { var def []interface{} @@ -260,9 +264,10 @@ func (j *Json) MustArray(args ...[]interface{}) []interface{} { // MustMap 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() { -// fmt.Println(k, v) -// } +// +// for k, v := range js.Get("dictionary").MustMap() { +// fmt.Println(k, v) +// } func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { var def map[string]interface{} @@ -285,7 +290,8 @@ func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { // MustString 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")) +// +// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default")) func (j *Json) MustString(args ...string) string { var def string @@ -308,9 +314,10 @@ func (j *Json) MustString(args ...string) string { // MustStringArray guarantees the return of a `[]string` (with optional default) // // useful when you want to interate over array values in a succinct manner: -// for i, s := range js.Get("results").MustStringArray() { -// fmt.Println(i, s) -// } +// +// for i, s := range js.Get("results").MustStringArray() { +// fmt.Println(i, s) +// } func (j *Json) MustStringArray(args ...[]string) []string { var def []string @@ -333,7 +340,8 @@ func (j *Json) MustStringArray(args ...[]string) []string { // MustInt 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)) +// +// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150)) func (j *Json) MustInt(args ...int) int { var def int @@ -356,7 +364,8 @@ func (j *Json) MustInt(args ...int) int { // MustFloat64 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)) +// +// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150)) func (j *Json) MustFloat64(args ...float64) float64 { var def float64 @@ -379,7 +388,8 @@ func (j *Json) MustFloat64(args ...float64) float64 { // MustBool 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)) +// +// myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true)) func (j *Json) MustBool(args ...bool) bool { var def bool @@ -402,7 +412,8 @@ func (j *Json) MustBool(args ...bool) bool { // MustInt64 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)) +// +// myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150)) func (j *Json) MustInt64(args ...int64) int64 { var def int64 @@ -425,7 +436,8 @@ func (j *Json) MustInt64(args ...int64) int64 { // MustUInt64 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)) +// +// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150)) func (j *Json) MustUint64(args ...uint64) uint64 { var def uint64 diff --git a/simplejson_go10_test.go b/simplejson_go10_test.go index 0a684e6..0a68d2a 100644 --- a/simplejson_go10_test.go +++ b/simplejson_go10_test.go @@ -4,9 +4,10 @@ package simplejson import ( "bytes" - "github.com/stretchr/testify/assert" "strconv" "testing" + + "github.com/stretchr/testify/assert" ) func TestNewFromReader(t *testing.T) { diff --git a/simplejson_go11_test.go b/simplejson_go11_test.go index c9b074b..1582799 100644 --- a/simplejson_go11_test.go +++ b/simplejson_go11_test.go @@ -5,7 +5,7 @@ package simplejson import ( "bytes" "encoding/json" - "github.com/stretchr/testify/assert" + "reflect" "strconv" "testing" ) @@ -26,32 +26,49 @@ func TestNewFromReader(t *testing.T) { js, err := NewFromReader(buf) //Standard Test Case - assert.NotEqual(t, nil, js) - assert.Equal(t, nil, err) + if js == nil { + t.Fatal("got nil") + } + if err != nil { + t.Fatalf("got err %#v", err) + } arr, _ := js.Get("test").Get("array").Array() - assert.NotEqual(t, nil, arr) + if arr == nil { + t.Fatal("got nil") + } for i, v := range arr { var iv int switch v.(type) { case json.Number: i64, err := v.(json.Number).Int64() - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("got err %#v", err) + } iv = int(i64) case string: iv, _ = strconv.Atoi(v.(string)) } - assert.Equal(t, i+1, iv) + if iv != i+1 { + t.Errorf("got %#v expected %#v", iv, i+1) + } } - ma := js.Get("test").Get("array").MustArray() - assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) + if ma := js.Get("test").Get("array").MustArray(); !reflect.DeepEqual(ma, []interface{}{json.Number("1"), "2", json.Number("3")}) { + t.Errorf("got %#v", ma) + } mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() - assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) + if !reflect.DeepEqual(mm, map[string]interface{}{"subkeyone": json.Number("1")}) { + t.Errorf("got %#v", mm) + } - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) - assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615)) + if n := js.Get("test").Get("bignum").MustInt64(); n != int64(9223372036854775807) { + t.Errorf("got %#v", n) + } + if n := js.Get("test").Get("uint64").MustUint64(); n != uint64(18446744073709551615) { + t.Errorf("got %#v", n) + } } func TestSimplejsonGo11(t *testing.T) { @@ -67,30 +84,46 @@ func TestSimplejsonGo11(t *testing.T) { } }`)) - assert.NotEqual(t, nil, js) - assert.Equal(t, nil, err) + if js == nil { + t.Fatal("got nil") + } + if err != nil { + t.Fatalf("got err %#v", err) + } arr, _ := js.Get("test").Get("array").Array() - assert.NotEqual(t, nil, arr) + if arr == nil { + t.Fatal("got nil") + } for i, v := range arr { var iv int switch v.(type) { case json.Number: i64, err := v.(json.Number).Int64() - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("got err %#v", err) + } iv = int(i64) case string: iv, _ = strconv.Atoi(v.(string)) } - assert.Equal(t, i+1, iv) + if iv != i+1 { + t.Errorf("got %#v expected %#v", iv, i+1) + } } - ma := js.Get("test").Get("array").MustArray() - assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) + if ma := js.Get("test").Get("array").MustArray(); !reflect.DeepEqual(ma, []interface{}{json.Number("1"), "2", json.Number("3")}) { + t.Errorf("got %#v", ma) + } mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() - 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)) + if !reflect.DeepEqual(mm, map[string]interface{}{"subkeyone": json.Number("1")}) { + t.Errorf("got %#v", mm) + } + if n := js.Get("test").Get("bignum").MustInt64(); n != int64(9223372036854775807) { + t.Errorf("got %#v", n) + } + if n := js.Get("test").Get("uint64").MustUint64(); n != uint64(18446744073709551615) { + t.Errorf("got %#v", n) + } } diff --git a/simplejson_test.go b/simplejson_test.go index 477f1a4..2ddc89b 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -2,9 +2,8 @@ package simplejson import ( "encoding/json" + "reflect" "testing" - - "github.com/stretchr/testify/assert" ) func TestSimplejson(t *testing.T) { @@ -25,103 +24,147 @@ func TestSimplejson(t *testing.T) { "sub_obj": {"a": 1} } }`)) - - assert.NotEqual(t, nil, js) - assert.Equal(t, nil, err) + if js == nil { + t.Fatal("got nil") + } + if err != nil { + t.Fatalf("got err %#v", err) + } _, ok = js.CheckGet("test") - assert.Equal(t, true, ok) + if !ok { + t.Errorf("test: got %#v expected true", ok) + } _, ok = js.CheckGet("missing_key") - assert.Equal(t, false, ok) + if ok { + t.Errorf("missing_key: got %#v expected false", ok) + } aws := js.Get("test").Get("arraywithsubs") - assert.NotEqual(t, nil, aws) - var awsval int - awsval, _ = aws.GetIndex(0).Get("subkeyone").Int() - assert.Equal(t, 1, awsval) - awsval, _ = aws.GetIndex(1).Get("subkeytwo").Int() - assert.Equal(t, 2, awsval) - awsval, _ = aws.GetIndex(1).Get("subkeythree").Int() - assert.Equal(t, 3, awsval) + if aws == nil { + t.Fatal("got nil") + } - i, _ := js.Get("test").Get("int").Int() - assert.Equal(t, 10, i) + if got, _ := aws.GetIndex(0).Get("subkeyone").Int(); got != 1 { + t.Errorf("got %#v", got) + } + if got, _ := aws.GetIndex(1).Get("subkeytwo").Int(); got != 2 { + t.Errorf("got %#v", got) + } + if got, _ := aws.GetIndex(1).Get("subkeythree").Int(); got != 3 { + t.Errorf("got %#v", got) + } - f, _ := js.Get("test").Get("float").Float64() - assert.Equal(t, 5.150, f) + if i, _ := js.Get("test").Get("int").Int(); i != 10 { + t.Errorf("got %#v", i) + } - s, _ := js.Get("test").Get("string").String() - assert.Equal(t, "simplejson", s) + if f, _ := js.Get("test").Get("float").Float64(); f != 5.150 { + t.Errorf("got %#v", f) + } - b, _ := js.Get("test").Get("bool").Bool() - assert.Equal(t, true, b) + if s, _ := js.Get("test").Get("string").String(); s != "simplejson" { + t.Errorf("got %#v", s) + } - mi := js.Get("test").Get("int").MustInt() - assert.Equal(t, 10, mi) + if b, _ := js.Get("test").Get("bool").Bool(); b != true { + t.Errorf("got %#v", b) + } - mi2 := js.Get("test").Get("missing_int").MustInt(5150) - assert.Equal(t, 5150, mi2) + if mi := js.Get("test").Get("int").MustInt(); mi != 10 { + t.Errorf("got %#v", mi) + } - ms := js.Get("test").Get("string").MustString() - assert.Equal(t, "simplejson", ms) + if mi := js.Get("test").Get("missing_int").MustInt(5150); mi != 5150 { + t.Errorf("got %#v", mi) + } - ms2 := js.Get("test").Get("missing_string").MustString("fyea") - assert.Equal(t, "fyea", ms2) + if s := js.Get("test").Get("string").MustString(); s != "simplejson" { + t.Errorf("got %#v", s) + } - ma2 := js.Get("test").Get("missing_array").MustArray([]interface{}{"1", 2, "3"}) - assert.Equal(t, ma2, []interface{}{"1", 2, "3"}) + if s := js.Get("test").Get("missing_string").MustString("fyea"); s != "fyea" { + t.Errorf("got %#v", s) + } + + a := js.Get("test").Get("missing_array").MustArray([]interface{}{"1", 2, "3"}) + if !reflect.DeepEqual(a, []interface{}{"1", 2, "3"}) { + t.Errorf("got %#v", a) + } msa := js.Get("test").Get("string_array").MustStringArray() - assert.Equal(t, msa[0], "asdf") - assert.Equal(t, msa[1], "ghjk") - assert.Equal(t, msa[2], "zxcv") + if !reflect.DeepEqual(msa, []string{"asdf", "ghjk", "zxcv"}) { + t.Errorf("got %#v", msa) + } - msa2 := js.Get("test").Get("string_array").MustStringArray([]string{"1", "2", "3"}) - assert.Equal(t, msa2[0], "asdf") - assert.Equal(t, msa2[1], "ghjk") - assert.Equal(t, msa2[2], "zxcv") + msa = js.Get("test").Get("string_array").MustStringArray([]string{"1", "2", "3"}) + if !reflect.DeepEqual(msa, []string{"asdf", "ghjk", "zxcv"}) { + t.Errorf("got %#v", msa) + } - msa3 := js.Get("test").Get("missing_array").MustStringArray([]string{"1", "2", "3"}) - assert.Equal(t, msa3, []string{"1", "2", "3"}) + msa = js.Get("test").Get("missing_array").MustStringArray([]string{"1", "2", "3"}) + if !reflect.DeepEqual(msa, []string{"1", "2", "3"}) { + t.Errorf("got %#v", msa) + } - mm2 := js.Get("test").Get("missing_map").MustMap(map[string]interface{}{"found": false}) - assert.Equal(t, mm2, map[string]interface{}{"found": false}) + mm := js.Get("test").Get("missing_map").MustMap(map[string]interface{}{"found": false}) + if !reflect.DeepEqual(mm, map[string]interface{}{"found": false}) { + t.Errorf("got %#v", mm) + } - strs, err := js.Get("test").Get("string_array").StringArray() - assert.Equal(t, err, nil) - assert.Equal(t, strs[0], "asdf") - assert.Equal(t, strs[1], "ghjk") - assert.Equal(t, strs[2], "zxcv") + sa, err := js.Get("test").Get("string_array").StringArray() + if err != nil { + t.Fatalf("got err %#v", err) + } + if !reflect.DeepEqual(sa, []string{"asdf", "ghjk", "zxcv"}) { + t.Errorf("got %#v", sa) + } - strs2, err := js.Get("test").Get("string_array_null").StringArray() - assert.Equal(t, err, nil) - assert.Equal(t, strs2[0], "abc") - assert.Equal(t, strs2[1], "") - assert.Equal(t, strs2[2], "efg") + sa, err = js.Get("test").Get("string_array_null").StringArray() + if err != nil { + t.Fatalf("got err %#v", err) + } + if !reflect.DeepEqual(sa, []string{"abc", "", "efg"}) { + t.Errorf("got %#v", sa) + } - gp, _ := js.GetPath("test", "string").String() - assert.Equal(t, "simplejson", gp) + if s, _ := js.GetPath("test", "string").String(); s != "simplejson" { + t.Errorf("got %#v", s) + } - gp2, _ := js.GetPath("test", "int").Int() - assert.Equal(t, 10, gp2) + if i, _ := js.GetPath("test", "int").Int(); i != 10 { + t.Errorf("got %#v", i) + } - assert.Equal(t, js.Get("test").Get("bool").MustBool(), true) + if b := js.Get("test").Get("bool").MustBool(); b != true { + t.Errorf("got %#v", b) + } js.Set("float2", 300.0) - assert.Equal(t, js.Get("float2").MustFloat64(), 300.0) + if f := js.Get("float2").MustFloat64(); f != 300.0 { + t.Errorf("got %#v", f) + } js.Set("test2", "setTest") - assert.Equal(t, "setTest", js.Get("test2").MustString()) + if s := js.Get("test2").MustString(); s != "setTest" { + t.Errorf("got %#v", s) + } js.Del("test2") - assert.NotEqual(t, "setTest", js.Get("test2").MustString()) + if s := js.Get("test2").MustString(); s == "setTest" { + t.Errorf("got %#v", s) + } js.Get("test").Get("sub_obj").Set("a", 2) - assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").MustInt()) + if i := js.Get("test").Get("sub_obj").Get("a").MustInt(); i != 2 { + t.Errorf("got %#v", i) + } js.GetPath("test", "sub_obj").Set("a", 3) - assert.Equal(t, 3, js.GetPath("test", "sub_obj", "a").MustInt()) + if i := js.Get("test").Get("sub_obj").Get("a").MustInt(); i != 3 { + t.Errorf("got %#v", i) + } } func TestStdlibInterfaces(t *testing.T) { @@ -136,73 +179,115 @@ func TestStdlibInterfaces(t *testing.T) { raw := `{"name":"myobject","params":{"string":"simplejson"}}` - assert.Equal(t, nil, json.Unmarshal([]byte(raw), val)) - - assert.Equal(t, "myobject", val.Name) - assert.NotEqual(t, nil, val.Params.data) - s, _ := val.Params.Get("string").String() - assert.Equal(t, "simplejson", s) + if err := json.Unmarshal([]byte(raw), val); err != nil { + t.Fatalf("err %#v", err) + } + if val.Name != "myobject" { + t.Errorf("got %#v", val.Name) + } + if val.Params.data == nil { + t.Errorf("got %#v", val.Params.data) + } + if s, _ := val.Params.Get("string").String(); s != "simplejson" { + t.Errorf("got %#v", s) + } p, err := json.Marshal(val) - assert.Equal(t, nil, err) - assert.Equal(t, nil, json.Unmarshal(p, val2)) - assert.Equal(t, val, val2) // stable + if err != nil { + t.Fatalf("err %#v", err) + } + if err = json.Unmarshal(p, val2); err != nil { + t.Fatalf("err %#v", err) + } + if !reflect.DeepEqual(val, val2) { // stable + t.Errorf("got %#v expected %#v", val2, val) + } } func TestSet(t *testing.T) { js, err := NewJson([]byte(`{}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } js.Set("baz", "bing") s, err := js.GetPath("baz").String() - assert.Equal(t, nil, err) - assert.Equal(t, "bing", s) + if err != nil { + t.Fatalf("err %#v", err) + } + if s != "bing" { + t.Errorf("got %#v", s) + } } func TestReplace(t *testing.T) { js, err := NewJson([]byte(`{}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } s, err := js.GetPath("baz").String() - assert.Equal(t, nil, err) - assert.Equal(t, "bing", s) + if err != nil { + t.Fatalf("err %#v", err) + } + if s != "bing" { + t.Errorf("got %#v", s) + } } func TestSetPath(t *testing.T) { js, err := NewJson([]byte(`{}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } js.SetPath([]string{"foo", "bar"}, "baz") s, err := js.GetPath("foo", "bar").String() - assert.Equal(t, nil, err) - assert.Equal(t, "baz", s) + if err != nil { + t.Fatalf("err %#v", err) + } + if s != "baz" { + t.Errorf("got %#v", s) + } } func TestSetPathNoPath(t *testing.T) { js, err := NewJson([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } - f := js.GetPath("some_number").MustFloat64(99.0) - assert.Equal(t, f, 1.0) + if f := js.GetPath("some_number").MustFloat64(99.0); f != 1.0 { + t.Errorf("got %#v", f) + } js.SetPath([]string{}, map[string]interface{}{"foo": "bar"}) s, err := js.GetPath("foo").String() - assert.Equal(t, nil, err) - assert.Equal(t, "bar", s) + if err != nil { + t.Fatalf("err %#v", err) + } + if s != "bar" { + t.Errorf("got %#v", s) + } - f = js.GetPath("some_number").MustFloat64(99.0) - assert.Equal(t, f, 99.0) + if f := js.GetPath("some_number").MustFloat64(99.0); f != 99.0 { + t.Errorf("got %#v", f) + } } func TestPathWillAugmentExisting(t *testing.T) { js, err := NewJson([]byte(`{"this":{"a":"aa","b":"bb","c":"cc"}}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } js.SetPath([]string{"this", "d"}, "dd") @@ -230,19 +315,29 @@ func TestPathWillAugmentExisting(t *testing.T) { for _, tc := range cases { s, err := js.GetPath(tc.path...).String() - assert.Equal(t, nil, err) - assert.Equal(t, tc.outcome, s) + if err != nil { + t.Fatalf("err %#v", err) + } + if s != tc.outcome { + t.Errorf("got %#v expected %#v", s, tc.outcome) + } } } func TestPathWillOverwriteExisting(t *testing.T) { // notice how "a" is 0.1 - but then we'll try to set at path a, foo js, err := NewJson([]byte(`{"this":{"a":0.1,"b":"bb","c":"cc"}}`)) - assert.Equal(t, nil, err) + if err != nil { + t.Fatalf("err %#v", err) + } js.SetPath([]string{"this", "a", "foo"}, "bar") s, err := js.GetPath("this", "a", "foo").String() - assert.Equal(t, nil, err) - assert.Equal(t, "bar", s) + if err != nil { + t.Fatalf("err %#v", err) + } + if s != "bar" { + t.Errorf("got %#v", s) + } } From cfc0185bfee1a2bd9342e9c0cdca976c3b9bbcd2 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Tue, 6 Jun 2023 09:57:31 -0400 Subject: [PATCH 2/3] Drop Go 1.0 compatability --- go.mod | 8 -- go.sum | 17 ---- simplejson_go10.go | 75 ---------------- simplejson_go10_test.go | 87 ------------------- simplejson_go11.go => simplejson_reader.go | 2 - ..._go11_test.go => simplejson_reader_test.go | 2 - 6 files changed, 191 deletions(-) delete mode 100644 simplejson_go10.go delete mode 100644 simplejson_go10_test.go rename simplejson_go11.go => simplejson_reader.go (99%) rename simplejson_go11_test.go => simplejson_reader_test.go (99%) diff --git a/go.mod b/go.mod index 9462032..c8006e9 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,3 @@ module github.com/bitly/go-simplejson go 1.17 - -require github.com/stretchr/testify v1.8.4 - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/go.sum b/go.sum index 479781e..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/simplejson_go10.go b/simplejson_go10.go deleted file mode 100644 index c9151e9..0000000 --- a/simplejson_go10.go +++ /dev/null @@ -1,75 +0,0 @@ -// +build !go1.1 - -package simplejson - -import ( - "encoding/json" - "errors" - "io" - "reflect" -) - -// NewFromReader returns a *Json by decoding from an io.Reader -func NewFromReader(r io.Reader) (*Json, error) { - j := new(Json) - dec := json.NewDecoder(r) - err := dec.Decode(&j.data) - return j, err -} - -// Implements the json.Unmarshaler interface. -func (j *Json) UnmarshalJSON(p []byte) error { - return json.Unmarshal(p, &j.data) -} - -// Float64 coerces into a float64 -func (j *Json) Float64() (float64, error) { - switch j.data.(type) { - case float32, float64: - return reflect.ValueOf(j.data).Float(), nil - case int, int8, int16, int32, int64: - return float64(reflect.ValueOf(j.data).Int()), nil - case uint, uint8, uint16, uint32, uint64: - return float64(reflect.ValueOf(j.data).Uint()), nil - } - return 0, errors.New("invalid value type") -} - -// Int coerces into an int -func (j *Json) Int() (int, error) { - switch j.data.(type) { - case float32, float64: - return int(reflect.ValueOf(j.data).Float()), nil - case int, int8, int16, int32, int64: - return int(reflect.ValueOf(j.data).Int()), nil - case uint, uint8, uint16, uint32, uint64: - return int(reflect.ValueOf(j.data).Uint()), nil - } - return 0, errors.New("invalid value type") -} - -// Int64 coerces into an int64 -func (j *Json) Int64() (int64, error) { - switch j.data.(type) { - case float32, float64: - return int64(reflect.ValueOf(j.data).Float()), nil - case int, int8, int16, int32, int64: - return reflect.ValueOf(j.data).Int(), nil - case uint, uint8, uint16, uint32, uint64: - return int64(reflect.ValueOf(j.data).Uint()), nil - } - return 0, errors.New("invalid value type") -} - -// Uint64 coerces into an uint64 -func (j *Json) Uint64() (uint64, error) { - switch j.data.(type) { - case float32, float64: - return uint64(reflect.ValueOf(j.data).Float()), nil - case int, int8, int16, int32, int64: - return uint64(reflect.ValueOf(j.data).Int()), nil - case uint, uint8, uint16, uint32, uint64: - return reflect.ValueOf(j.data).Uint(), nil - } - return 0, errors.New("invalid value type") -} diff --git a/simplejson_go10_test.go b/simplejson_go10_test.go deleted file mode 100644 index 0a68d2a..0000000 --- a/simplejson_go10_test.go +++ /dev/null @@ -1,87 +0,0 @@ -// +build !go1.1 - -package simplejson - -import ( - "bytes" - "strconv" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewFromReader(t *testing.T) { - buf := bytes.NewBuffer([]byte(`{ - "test": { - "array": [1, "2", 3], - "arraywithsubs": [ - {"subkeyone": 1}, - {"subkeytwo": 2, "subkeythree": 3} - ], - "bignum": 8000000000 - } - }`)) - js, err := NewFromReader(buf) - - //Standard Test Case - assert.NotEqual(t, nil, js) - assert.Equal(t, nil, err) - - arr, _ := js.Get("test").Get("array").Array() - assert.NotEqual(t, nil, arr) - for i, v := range arr { - var iv int - switch v.(type) { - case float64: - iv = int(v.(float64)) - case string: - iv, _ = strconv.Atoi(v.(string)) - } - assert.Equal(t, i+1, iv) - } - - ma := js.Get("test").Get("array").MustArray() - assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - - mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() - assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) - - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) -} - -func TestSimplejsonGo10(t *testing.T) { - js, err := NewJson([]byte(`{ - "test": { - "array": [1, "2", 3], - "arraywithsubs": [ - {"subkeyone": 1}, - {"subkeytwo": 2, "subkeythree": 3} - ], - "bignum": 8000000000 - } - }`)) - - assert.NotEqual(t, nil, js) - assert.Equal(t, nil, err) - - arr, _ := js.Get("test").Get("array").Array() - assert.NotEqual(t, nil, arr) - for i, v := range arr { - var iv int - switch v.(type) { - case float64: - iv = int(v.(float64)) - case string: - iv, _ = strconv.Atoi(v.(string)) - } - assert.Equal(t, i+1, iv) - } - - ma := js.Get("test").Get("array").MustArray() - assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - - mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() - assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) - - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) -} diff --git a/simplejson_go11.go b/simplejson_reader.go similarity index 99% rename from simplejson_go11.go rename to simplejson_reader.go index 1c47953..a109013 100644 --- a/simplejson_go11.go +++ b/simplejson_reader.go @@ -1,5 +1,3 @@ -// +build go1.1 - package simplejson import ( diff --git a/simplejson_go11_test.go b/simplejson_reader_test.go similarity index 99% rename from simplejson_go11_test.go rename to simplejson_reader_test.go index 1582799..228b58b 100644 --- a/simplejson_go11_test.go +++ b/simplejson_reader_test.go @@ -1,5 +1,3 @@ -// +build go1.1 - package simplejson import ( From 2eea67a1a294c88fad48b4984aa1815177ba0423 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Tue, 6 Jun 2023 10:03:33 -0400 Subject: [PATCH 3/3] ci: travis -> GitHub Action --- .github/workflows/ci.yaml | 18 ++++++++++++++++++ .travis.yml | 8 -------- README.md | 4 +++- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/ci.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..4c4c361 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,18 @@ +name: Go package + +on: [push, pull_request] + +jobs: + build: + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.x' + + - name: Test + run: go test -v ./... diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e08d89d..0000000 --- a/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -sudo: false -language: go - -go: - - 1.x - -notifications: - email: false diff --git a/README.md b/README.md index 812d307..f1f0790 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ a Go package to interact with arbitrary JSON -[![Build Status](https://secure.travis-ci.org/bitly/go-simplejson.png?branch=master)](http://travis-ci.org/bitly/go-simplejson) [![GoDoc](https://godoc.org/github.com/bitly/go-simplejson?status.svg)](https://godoc.org/github.com/bitly/go-simplejson) [![GitHub release](https://img.shields.io/github/release/bitly/go-simplejson.svg)](https://github.com/bitly/go-simplejson/releases/latest) +[![Build Status](https://github.com/bitly/go-simplejson/actions/workflows/ci/badge.svg)](https://github.com/bitly/go-simplejson/actions) +[![GoDoc](https://pkg.go.dev/badge/github.com/bitly/go-simplejson)](https://pkg.go.dev/github.com/bitly/go-simplejson) +[![GitHub release](https://img.shields.io/github/release/bitly/go-simplejson.svg)](https://github.com/bitly/go-simplejson/releases/latest) ### Importing