Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MapArray & MustMapArray #73

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/go-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xxx
1 change: 1 addition & 0 deletions .github/workflows/go-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

29 changes: 29 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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 ./...

46 changes: 46 additions & 0 deletions simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions simplejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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}],
Expand Down Expand Up @@ -85,6 +87,19 @@ 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()
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})

Expand All @@ -100,6 +115,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)

Expand Down