diff --git a/feature_any_array.go b/feature_any_array.go index b09cf18..e08d159 100644 --- a/feature_any_array.go +++ b/feature_any_array.go @@ -213,14 +213,28 @@ func (any *arrayLazyAny) Get(path ...interface{}) Any { return any } var element Any - idx, ok := path[0].(int) - if ok { - element = any.fillCacheUntil(idx) + switch firstPath := path[0].(type) { + case int: + element = any.fillCacheUntil(firstPath) if element == nil { - element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)} + element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)} } - } else { - element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)} + case int32: + if '*' == firstPath { + any.fillCache() + arr := make([]Any, 0, len(any.cache)) + for _, element := range any.cache { + found := element.Get(path[1:]...) + if found.ValueType() != Invalid { + arr = append(arr, found) + } + } + return wrapArray(arr) + } else { + element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)} + } + default: + element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)} } if len(path) == 1 { return element @@ -445,14 +459,28 @@ func (any *arrayAny) Get(path ...interface{}) Any { return any } var element Any - idx, ok := path[0].(int) - if ok { - element = any.fillCacheUntil(idx) + switch firstPath := path[0].(type) { + case int: + element = any.fillCacheUntil(firstPath) if element == nil { - element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)} + element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)} } - } else { - element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)} + case int32: + if '*' == firstPath { + any.fillCache() + arr := make([]Any, 0, len(any.cache)) + for _, element := range any.cache { + found := element.Get(path[1:]...) + if found.ValueType() != Invalid { + arr = append(arr, found) + } + } + return wrapArray(arr) + } else { + element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)} + } + default: + element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)} } if len(path) == 1 { return element diff --git a/jsoniter_array_test.go b/jsoniter_array_test.go index d2763ae..ddc9a15 100644 --- a/jsoniter_array_test.go +++ b/jsoniter_array_test.go @@ -114,6 +114,16 @@ func Test_array_lazy_any_get_all(t *testing.T) { should.Equal("[1,2,3]", any.Get('*',0).ToString()) } +func Test_array_wrapper_any_get_all(t *testing.T) { + should := require.New(t) + any := wrapArray([][]int{ + []int{1, 2}, + []int{3, 4}, + []int{5, 6}, + }) + should.Equal("[1,3,5]", any.Get('*',0).ToString()) +} + func Test_array_lazy_any_get_invalid(t *testing.T) { should := require.New(t) any, err := UnmarshalAnyFromString("[]")