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

Any Get will never return nil

This commit is contained in:
Tao Wen
2017-01-26 16:24:01 +08:00
parent 9abc2f52b0
commit 85edb698c8
12 changed files with 106 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package jsoniter
import (
"unsafe"
"fmt"
)
type arrayLazyAny struct {
@ -13,6 +14,10 @@ type arrayLazyAny struct {
remaining []byte
}
func (any *arrayLazyAny) ValueType() ValueType {
return Array
}
func (any *arrayLazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
@ -176,12 +181,20 @@ func (any *arrayLazyAny) Get(path ...interface{}) Any {
if len(path) == 0 {
return any
}
if len(path) == 1 {
idx := path[0].(int)
return any.fillCacheUntil(idx)
var element Any
idx, ok := path[0].(int)
if ok {
element = any.fillCacheUntil(idx)
if element == nil {
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)}
}
} else {
idx := path[0].(int)
return any.fillCacheUntil(idx).Get(path[1:]...)
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)}
}
if len(path) == 1 {
return element
} else {
return element.Get(path[1:]...)
}
}