mirror of
https://github.com/json-iterator/go.git
synced 2025-02-07 19:30:06 +02:00
map any support get all
This commit is contained in:
parent
bf459b9a49
commit
1e91dbbf58
@ -503,14 +503,28 @@ func (any *objectAny) Get(path ...interface{}) Any {
|
||||
return any
|
||||
}
|
||||
var element Any
|
||||
key, ok := path[0].(string)
|
||||
if ok {
|
||||
element = any.fillCacheUntil(key)
|
||||
switch firstPath := path[0].(type) {
|
||||
case string:
|
||||
element = any.fillCacheUntil(firstPath)
|
||||
if element == nil {
|
||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", key, 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", key, any.cache)}
|
||||
case int32:
|
||||
if '*' == firstPath {
|
||||
any.fillCache()
|
||||
mappedAll := map[string]Any{}
|
||||
for key, value := range any.cache {
|
||||
mapped := value.Get(path[1:]...)
|
||||
if mapped.ValueType() != Invalid {
|
||||
mappedAll[key] = mapped
|
||||
}
|
||||
}
|
||||
return wrapMap(mappedAll)
|
||||
} else {
|
||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
||||
}
|
||||
default:
|
||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
||||
}
|
||||
if len(path) == 1 {
|
||||
return element
|
||||
@ -738,14 +752,28 @@ func (any *mapAny) Get(path ...interface{}) Any {
|
||||
return any
|
||||
}
|
||||
var element Any
|
||||
key, ok := path[0].(string)
|
||||
if ok {
|
||||
element = any.fillCacheUntil(key)
|
||||
switch firstPath := path[0].(type) {
|
||||
case string:
|
||||
element = any.fillCacheUntil(firstPath)
|
||||
if element == nil {
|
||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", key, 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", key, any.cache)}
|
||||
case int32:
|
||||
if '*' == firstPath {
|
||||
any.fillCache()
|
||||
mappedAll := map[string]Any{}
|
||||
for key, value := range any.cache {
|
||||
mapped := value.Get(path[1:]...)
|
||||
if mapped.ValueType() != Invalid {
|
||||
mappedAll[key] = mapped
|
||||
}
|
||||
}
|
||||
return wrapMap(mappedAll)
|
||||
} else {
|
||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
||||
}
|
||||
default:
|
||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
||||
}
|
||||
if len(path) == 1 {
|
||||
return element
|
||||
|
@ -43,6 +43,12 @@ func Test_wrap_map(t *testing.T) {
|
||||
should.Equal(map[string]string{"Field1":"hello"}, vals)
|
||||
}
|
||||
|
||||
func Test_map_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Wrap(map[string][]int{"Field1": []int{1, 2}})
|
||||
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
|
||||
}
|
||||
|
||||
func Test_write_val_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := map[string]string{"1": "2"}
|
||||
|
@ -108,7 +108,6 @@ func Test_object_any_lazy_iterator(t *testing.T) {
|
||||
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
||||
}
|
||||
|
||||
|
||||
func Test_object_any_with_two_lazy_iterators(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d","e":"f"}`)
|
||||
@ -195,6 +194,16 @@ func Test_wrap_object(t *testing.T) {
|
||||
should.Equal(map[string]string{"Field1":"hello"}, vals)
|
||||
}
|
||||
|
||||
func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 []int
|
||||
Field2 []int
|
||||
}
|
||||
any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
|
||||
should.Equal(`{"Field2":3,"Field1":1}`, any.Get('*', 0).ToString())
|
||||
}
|
||||
|
||||
func Test_write_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
|
Loading…
x
Reference in New Issue
Block a user