From 0521617964ca38da3bfa5955c2de90341a40e459 Mon Sep 17 00:00:00 2001 From: brendesp Date: Mon, 13 May 2019 16:22:20 -0600 Subject: [PATCH 1/2] Allow Set() to accept arrays. --- simplejson.go | 17 +++++++++++++++++ simplejson_test.go | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/simplejson.go b/simplejson.go index 95e73fd..813a054 100644 --- a/simplejson.go +++ b/simplejson.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "log" + "reflect" ) // returns the current implementation version @@ -60,6 +61,22 @@ func (j *Json) Set(key string, val interface{}) { if err != nil { return } + + if val != nil { + // If val is an array convert to []interface{} + typ := reflect.TypeOf(val) + kind := typ.Kind() + if kind == reflect.Array || kind == reflect.Slice { + + v := reflect.ValueOf(val) + a := make([]interface{}, v.Len()) + for i := 0; i < v.Len(); i++ { + a[i] = v.Index(i).Interface() + } + m[key] = a + return + } + } m[key] = val } diff --git a/simplejson_test.go b/simplejson_test.go index 477f1a4..73cf323 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -122,6 +122,24 @@ func TestSimplejson(t *testing.T) { js.GetPath("test", "sub_obj").Set("a", 3) assert.Equal(t, 3, js.GetPath("test", "sub_obj", "a").MustInt()) + + a := [3]string{"one", "two", "three"} + js = New() + js.Set("array", a) + a2, err := js.Get("array").Array() + assert.Equal(t, nil, err) + assert.NotEqual(t, nil, a2) + assert.Equal(t, a2[0], "one") + assert.Equal(t, a2[1], "two") + assert.Equal(t, a2[2], "three") + + a3, err := js.Get("array").StringArray() + assert.Equal(t, nil, err) + assert.NotEqual(t, nil, a3) + assert.Equal(t, a3[0], "one") + assert.Equal(t, a3[1], "two") + assert.Equal(t, a3[2], "three") + } func TestStdlibInterfaces(t *testing.T) { From ea5b96b695de2c2fa505489ad6d0c7194fb689ce Mon Sep 17 00:00:00 2001 From: brendesp Date: Mon, 13 May 2019 16:33:23 -0600 Subject: [PATCH 2/2] Refactor and add handleArray() --- simplejson.go | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/simplejson.go b/simplejson.go index 813a054..5a65e57 100644 --- a/simplejson.go +++ b/simplejson.go @@ -62,29 +62,14 @@ func (j *Json) Set(key string, val interface{}) { return } - if val != nil { - // If val is an array convert to []interface{} - typ := reflect.TypeOf(val) - kind := typ.Kind() - if kind == reflect.Array || kind == reflect.Slice { - - v := reflect.ValueOf(val) - a := make([]interface{}, v.Len()) - for i := 0; i < v.Len(); i++ { - a[i] = v.Index(i).Interface() - } - m[key] = a - return - } - } - m[key] = val + m[key] = handleArray(val) } // SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, // and then finally writing in the value func (j *Json) SetPath(branch []string, val interface{}) { if len(branch) == 0 { - j.data = val + j.data = handleArray(val) return } @@ -116,7 +101,7 @@ func (j *Json) SetPath(branch []string, val interface{}) { } // add remaining k/v - curr[branch[len(branch)-1]] = val + curr[branch[len(branch)-1]] = handleArray(val) } // Del modifies `Json` map by deleting `key` if it is present. @@ -461,3 +446,21 @@ func (j *Json) MustUint64(args ...uint64) uint64 { return def } + +func handleArray(val interface{}) interface{} { + if val != nil { + // If val is an array convert to []interface{} + typ := reflect.TypeOf(val) + kind := typ.Kind() + if kind == reflect.Array || kind == reflect.Slice { + + v := reflect.ValueOf(val) + arr := make([]interface{}, v.Len()) + for i := 0; i < v.Len(); i++ { + arr[i] = v.Index(i).Interface() + } + val = arr + } + } + return val +}