1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00

lazy array get

This commit is contained in:
Tao Wen 2017-01-24 22:56:18 +08:00
parent fa165c684f
commit ee54218b0a
8 changed files with 22 additions and 27 deletions

View File

@ -18,6 +18,10 @@ type Any interface {
type baseAny struct {}
func (any *baseAny) Get(path ...interface{}) Any {
return &invalidAny{}
}
func (any *baseAny) Size() int {
return 0
}

View File

@ -117,8 +117,16 @@ func (any *arrayLazyAny) ToString() string {
}
func (any *arrayLazyAny) Get(path ...interface{}) Any {
idx := path[0].(int)
return any.fillCacheUntil(idx)
if len(path) == 0 {
return any
}
if len(path) == 1 {
idx := path[0].(int)
return any.fillCacheUntil(idx)
} else {
idx := path[0].(int)
return any.fillCacheUntil(idx).Get(path[1:]...)
}
}
func (any *arrayLazyAny) Size() int {

View File

@ -36,10 +36,6 @@ func (any *trueAny) ToString() string {
return "true"
}
func (any *trueAny) Get(path ...interface{}) Any {
return &invalidAny{}
}
type falseAny struct {
baseAny
}
@ -75,7 +71,3 @@ func (any *falseAny) ToFloat64() float64 {
func (any *falseAny) ToString() string {
return "false"
}
func (any *falseAny) Get(path ...interface{}) Any {
return &invalidAny{}
}

View File

@ -64,8 +64,4 @@ func (any *floatLazyAny) ToFloat64() float64 {
func (any *floatLazyAny) ToString() string {
return *(*string)(unsafe.Pointer(&any.buf))
}
func (any *floatLazyAny) Get(path ...interface{}) Any {
return &invalidAny{}
}

View File

@ -64,8 +64,4 @@ func (any *intLazyAny) ToFloat64() float64 {
func (any *intLazyAny) ToString() string {
return *(*string)(unsafe.Pointer(&any.buf))
}
func (any *intLazyAny) Get(path ...interface{}) Any {
return &invalidAny{}
}

View File

@ -34,8 +34,4 @@ func (any *nilAny) ToFloat64() float64 {
func (any *nilAny) ToString() string {
return ""
}
func (any *nilAny) Get(path ...interface{}) Any {
return &invalidAny{}
}
}

View File

@ -96,8 +96,4 @@ func (any *stringLazyAny) ToFloat64() float64 {
func (any *stringLazyAny) ToString() string {
any.fillCache()
return any.cache
}
func (any *stringLazyAny) Get(path ...interface{}) Any {
return &invalidAny{}
}

View File

@ -80,6 +80,13 @@ func Test_read_array_with_any_iterator(t *testing.T) {
should.Equal([]int{1, 2}, elements)
}
func Test_array_any_get(t *testing.T) {
should := require.New(t)
any, err := UnmarshalAnyFromString("[1,[2,3],4]")
should.Nil(err)
should.Equal(3, any.Get(1,1).ToInt())
}
func Test_invalid_array(t *testing.T) {
_, err := UnmarshalAnyFromString("[")
if err == nil || err == io.EOF {