Skip to content

Commit

Permalink
增加个获取数组元素的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 25, 2024
1 parent ce80c33 commit 94c3ac1
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 deletions.
9 changes: 9 additions & 0 deletions resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ func Resolve[T any](simpleJson *simplejson.Json) (T, error) {
return zero, errors.Errorf("unsupported generic type: %T. unable to resolve JSON value.", zero)
}
}

// GetList retrieves a slice of simplejson.Json objects from the key's list (a[key]).
func GetList(simpleJson *simplejson.Json, key string) (simpleJsons []*simplejson.Json, err error) {
elements, err := simpleJson.Get(key).Array()
if err != nil {
return simpleJsons, errors.WithMessage(err, "unable to get list")
}
return List(elements), nil
}
33 changes: 33 additions & 0 deletions resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,36 @@ func TestResolve_Bytes(t *testing.T) {
t.Log(res)
require.Equal(t, "abc", string(res))
}

func TestGetList(t *testing.T) {
jsonData := `{
"key": [
{"name": "item1"},
{"name": "item2"},
{"name": "item3"}
]
}`

simpleJson, err := simplejsonx.Load([]byte(jsonData))
require.NoError(t, err)

{
simpleJsons, err := simplejsonx.GetList(simpleJson, "key")
require.NoError(t, err)
require.Len(t, simpleJsons, 3)

var names = make([]string, 0, len(simpleJsons))
for _, item := range simpleJsons {
name, err := simplejsonx.Extract[string](item, "name")
require.NoError(t, err)
names = append(names, name)
}
require.Equal(t, []string{"item1", "item2", "item3"}, names)
}

{
simpleJsons, err := simplejsonx.GetList(simpleJson, "invalidKey")
require.Error(t, err)
require.Len(t, simpleJsons, 0)
}
}
6 changes: 6 additions & 0 deletions sure/simplejson_must/resolve_must.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ func Resolve[T any](simpleJson *simplejson.Json) T {
sure.Must(err)
return res0
}

func GetList(simpleJson *simplejson.Json, key string) (simpleJsons []*simplejson.Json) {
simpleJsons, err := simplejsonx.GetList(simpleJson, key)
sure.Must(err)
return simpleJsons
}
6 changes: 3 additions & 3 deletions sure/simplejson_must/wrap_must.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
return simpleJson
}

func List(items []interface{}) (elements []*simplejson.Json) {
elements = simplejsonx.List(items)
return elements
func List(elements []interface{}) (simpleJsons []*simplejson.Json) {
simpleJsons = simplejsonx.List(elements)
return simpleJsons
}
6 changes: 6 additions & 0 deletions sure/simplejson_omit/resolve_omit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ func Resolve[T any](simpleJson *simplejson.Json) T {
sure.Omit(err)
return res0
}

func GetList(simpleJson *simplejson.Json, key string) (simpleJsons []*simplejson.Json) {
simpleJsons, err := simplejsonx.GetList(simpleJson, key)
sure.Omit(err)
return simpleJsons
}
6 changes: 3 additions & 3 deletions sure/simplejson_omit/wrap_omit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
return simpleJson
}

func List(items []interface{}) (elements []*simplejson.Json) {
elements = simplejsonx.List(items)
return elements
func List(elements []interface{}) (simpleJsons []*simplejson.Json) {
simpleJsons = simplejsonx.List(elements)
return simpleJsons
}
6 changes: 6 additions & 0 deletions sure/simplejson_soft/resolve_soft.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ func Resolve[T any](simpleJson *simplejson.Json) T {
sure.Soft(err)
return res0
}

func GetList(simpleJson *simplejson.Json, key string) (simpleJsons []*simplejson.Json) {
simpleJsons, err := simplejsonx.GetList(simpleJson, key)
sure.Soft(err)
return simpleJsons
}
6 changes: 3 additions & 3 deletions sure/simplejson_soft/wrap_soft.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
return simpleJson
}

func List(items []interface{}) (elements []*simplejson.Json) {
elements = simplejsonx.List(items)
return elements
func List(elements []interface{}) (simpleJsons []*simplejson.Json) {
simpleJsons = simplejsonx.List(elements)
return simpleJsons
}
10 changes: 5 additions & 5 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
}

// List converts a slice of items into a slice of simplejson.Json objects.
func List(items []interface{}) (elements []*simplejson.Json) {
elements = make([]*simplejson.Json, 0, len(items))
for _, item := range items {
elements = append(elements, Wrap(item))
func List(elements []interface{}) (simpleJsons []*simplejson.Json) {
simpleJsons = make([]*simplejson.Json, 0, len(elements))
for _, elem := range elements {
simpleJsons = append(simpleJsons, Wrap(elem))
}
return elements
return simpleJsons
}

0 comments on commit 94c3ac1

Please sign in to comment.