From 4072553dfcfd0744ad640783f0b430bc06902e87 Mon Sep 17 00:00:00 2001 From: xiaoy Date: Thu, 13 Dec 2018 18:38:15 +0800 Subject: [PATCH 1/7] Update simplejson.go --- simplejson.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/simplejson.go b/simplejson.go index 95e73fd..aa5591a 100644 --- a/simplejson.go +++ b/simplejson.go @@ -232,6 +232,27 @@ func (j *Json) StringArray() ([]string, error) { return retArr, nil } + +// StringArray type asserts to an `array` of `map[string]interface{}` +func (j *Json) MapArray() ([]map[string]interface{}, error) { + arr, err := j.Array() + if err != nil { + return nil, err + } + retArr := make([]map[string]interface{}, 0, len(arr)) + for _, a := range arr { + if a == nil { + retArr = append(retArr, nil) + continue + } + s, ok := a.(map[string]interface{}) + if !ok { + return nil, errors.New("type assertion to []map[string]interface{} failed") + } + retArr = append(retArr, s) + } + return retArr, nil +} // MustArray guarantees the return of a `[]interface{}` (with optional default) // // useful when you want to interate over array values in a succinct manner: @@ -330,6 +351,31 @@ func (j *Json) MustStringArray(args ...[]string) []string { return def } +// MustMapArray guarantees the return of a `[]map[string]interface{}` (with optional default) +// +// useful when you want to interate over array values in a succinct manner: +// for i, s := range js.Get("results").MustMaPArray() { +// fmt.Println(i, s) +// } + +func (j *Json) MustMapArray(args ...[]map[string]interface{}) []map[string]interface{} { + var def []map[string]interface{} + + switch len(args) { + case 0: + case 1: + def = args[0] + default: + log.Panicf("MustStringArray() received too many arguments %d", len(args)) + } + + a, err := j.MapArray() + if err == nil { + return a + } + + return def +} // MustInt guarantees the return of an `int` (with optional default) // // useful when you explicitly want an `int` in a single value return context: From f99ed6f3a5a77ba38420cd7c433b4fd66da4fbf2 Mon Sep 17 00:00:00 2001 From: xiaoy Date: Fri, 14 Dec 2018 14:58:58 +0800 Subject: [PATCH 2/7] Update simplejson_test.go add MustMapArray & MapArray test --- simplejson_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/simplejson_test.go b/simplejson_test.go index 477f1a4..aaa8e3a 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "fmt" ) func TestSimplejson(t *testing.T) { @@ -15,6 +16,8 @@ func TestSimplejson(t *testing.T) { "test": { "string_array": ["asdf", "ghjk", "zxcv"], "string_array_null": ["abc", null, "efg"], + "map_array": [{"asdf":123}, {"ghjk":456}, {"zxcv":789}], + "map_array_null": [{"asdf":123}, null, {"zxcv":789}], "array": [1, "2", 3], "arraywithsubs": [{"subkeyone": 1}, {"subkeytwo": 2, "subkeythree": 3}], @@ -85,6 +88,20 @@ func TestSimplejson(t *testing.T) { msa3 := js.Get("test").Get("missing_array").MustStringArray([]string{"1", "2", "3"}) assert.Equal(t, msa3, []string{"1", "2", "3"}) + mma := js.Get("test").Get("map_array").MustMapArray() + fmt.Println(json.Number(123)) + assert.Equal(t, mma[0], map[string]interface{}{"asdf":json.Number("123")}) + assert.Equal(t, mma[1], map[string]interface{}{"ghjk":json.Number("456")}) + assert.Equal(t, mma[2], map[string]interface{}{"zxcv":json.Number("789")}) + + mma2 := js.Get("test").Get("map_array").MustMapArray([]map[string]interface{}{{"a":"1", "b":"2", "c":"3"}}) + assert.Equal(t, mma2[0], map[string]interface{}{"asdf":json.Number("123")}) + assert.Equal(t, mma2[1], map[string]interface{}{"ghjk":json.Number("456")}) + assert.Equal(t, mma2[2], map[string]interface{}{"zxcv":json.Number("789")}) + + mma3 := js.Get("test").Get("missing_map_array").MustMapArray([]map[string]interface{}{{"a":"1", "b":"2", "c":"3"}}) + assert.Equal(t, mma3, []map[string]interface{}{{"a":"1", "b":"2", "c":"3"}}) + mm2 := js.Get("test").Get("missing_map").MustMap(map[string]interface{}{"found": false}) assert.Equal(t, mm2, map[string]interface{}{"found": false}) @@ -100,6 +117,18 @@ func TestSimplejson(t *testing.T) { assert.Equal(t, strs2[1], "") assert.Equal(t, strs2[2], "efg") + maps, err := js.Get("test").Get("map_array").MapArray() + assert.Equal(t, err, nil) + assert.Equal(t, maps[0], map[string]interface{}{"asdf":json.Number("123")}) + assert.Equal(t, maps[1], map[string]interface{}{"ghjk":json.Number("456")}) + assert.Equal(t, maps[2], map[string]interface{}{"zxcv":json.Number("789")}) + + maps2, err := js.Get("test").Get("map_array_null").MapArray() + assert.Equal(t, err, nil) + assert.Equal(t, maps2[0], map[string]interface{}{"asdf":json.Number("123")}) + assert.Equal(t, maps2[1], map[string]interface{}(nil)) + assert.Equal(t, maps2[2], map[string]interface{}{"zxcv":json.Number("789")}) + gp, _ := js.GetPath("test", "string").String() assert.Equal(t, "simplejson", gp) From 5668b1979ae58fbc95f203a9afc01674c03fe7da Mon Sep 17 00:00:00 2001 From: xiaoy Date: Fri, 14 Dec 2018 15:00:01 +0800 Subject: [PATCH 3/7] Update simplejson_test.go fix --- simplejson_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/simplejson_test.go b/simplejson_test.go index aaa8e3a..a0fd5ce 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "fmt" ) func TestSimplejson(t *testing.T) { @@ -89,7 +88,6 @@ func TestSimplejson(t *testing.T) { assert.Equal(t, msa3, []string{"1", "2", "3"}) mma := js.Get("test").Get("map_array").MustMapArray() - fmt.Println(json.Number(123)) assert.Equal(t, mma[0], map[string]interface{}{"asdf":json.Number("123")}) assert.Equal(t, mma[1], map[string]interface{}{"ghjk":json.Number("456")}) assert.Equal(t, mma[2], map[string]interface{}{"zxcv":json.Number("789")}) From 34d4806f948ffcbb7e98e524a4cbee8b6bb0da78 Mon Sep 17 00:00:00 2001 From: xiaoy Date: Mon, 23 Oct 2023 16:11:32 +0800 Subject: [PATCH 4/7] Create go.yml --- .github/workflows/go.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..0065213 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,28 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... From 30bbb476eb869edb013b423454a622f9d5723e3e Mon Sep 17 00:00:00 2001 From: xiaoy Date: Mon, 26 Feb 2024 15:04:27 +0800 Subject: [PATCH 5/7] Update go.yml --- .github/workflows/go.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0065213..649a9cf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -26,3 +26,4 @@ jobs: - name: Test run: go test -v ./... + From 6db5f28b34615ab7f0530c61f4c37c6016518773 Mon Sep 17 00:00:00 2001 From: xiaoy Date: Mon, 8 Apr 2024 17:37:14 +0800 Subject: [PATCH 6/7] Create go-2.yaml --- .github/workflows/go-2.yaml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/workflows/go-2.yaml diff --git a/.github/workflows/go-2.yaml b/.github/workflows/go-2.yaml new file mode 100644 index 0000000..d6459e0 --- /dev/null +++ b/.github/workflows/go-2.yaml @@ -0,0 +1 @@ +xxx From 8ddea2c5509e10c84ed4968ab7a956b272fe3328 Mon Sep 17 00:00:00 2001 From: xiaoy Date: Mon, 8 Apr 2024 17:37:28 +0800 Subject: [PATCH 7/7] Create go-3.yaml --- .github/workflows/go-3.yaml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/workflows/go-3.yaml diff --git a/.github/workflows/go-3.yaml b/.github/workflows/go-3.yaml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.github/workflows/go-3.yaml @@ -0,0 +1 @@ +