1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

map any support get all

This commit is contained in:
Tao Wen
2017-01-31 23:16:40 +08:00
parent bf459b9a49
commit 1e91dbbf58
3 changed files with 56 additions and 13 deletions

View File

@ -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