any_object.go raw

   1  package jsoniter
   2  
   3  import (
   4  	"reflect"
   5  	"unsafe"
   6  )
   7  
   8  type objectLazyAny struct {
   9  	baseAny
  10  	cfg *frozenConfig
  11  	buf []byte
  12  	err error
  13  }
  14  
  15  func (any *objectLazyAny) ValueType() ValueType {
  16  	return ObjectValue
  17  }
  18  
  19  func (any *objectLazyAny) MustBeValid() Any {
  20  	return any
  21  }
  22  
  23  func (any *objectLazyAny) LastError() error {
  24  	return any.err
  25  }
  26  
  27  func (any *objectLazyAny) ToBool() bool {
  28  	return true
  29  }
  30  
  31  func (any *objectLazyAny) ToInt() int {
  32  	return 0
  33  }
  34  
  35  func (any *objectLazyAny) ToInt32() int32 {
  36  	return 0
  37  }
  38  
  39  func (any *objectLazyAny) ToInt64() int64 {
  40  	return 0
  41  }
  42  
  43  func (any *objectLazyAny) ToUint() uint {
  44  	return 0
  45  }
  46  
  47  func (any *objectLazyAny) ToUint32() uint32 {
  48  	return 0
  49  }
  50  
  51  func (any *objectLazyAny) ToUint64() uint64 {
  52  	return 0
  53  }
  54  
  55  func (any *objectLazyAny) ToFloat32() float32 {
  56  	return 0
  57  }
  58  
  59  func (any *objectLazyAny) ToFloat64() float64 {
  60  	return 0
  61  }
  62  
  63  func (any *objectLazyAny) ToString() string {
  64  	return *(*string)(unsafe.Pointer(&any.buf))
  65  }
  66  
  67  func (any *objectLazyAny) ToVal(obj interface{}) {
  68  	iter := any.cfg.BorrowIterator(any.buf)
  69  	defer any.cfg.ReturnIterator(iter)
  70  	iter.ReadVal(obj)
  71  }
  72  
  73  func (any *objectLazyAny) Get(path ...interface{}) Any {
  74  	if len(path) == 0 {
  75  		return any
  76  	}
  77  	switch firstPath := path[0].(type) {
  78  	case string:
  79  		iter := any.cfg.BorrowIterator(any.buf)
  80  		defer any.cfg.ReturnIterator(iter)
  81  		valueBytes := locateObjectField(iter, firstPath)
  82  		if valueBytes == nil {
  83  			return newInvalidAny(path)
  84  		}
  85  		iter.ResetBytes(valueBytes)
  86  		return locatePath(iter, path[1:])
  87  	case int32:
  88  		if '*' == firstPath {
  89  			mappedAll := map[string]Any{}
  90  			iter := any.cfg.BorrowIterator(any.buf)
  91  			defer any.cfg.ReturnIterator(iter)
  92  			iter.ReadMapCB(func(iter *Iterator, field string) bool {
  93  				mapped := locatePath(iter, path[1:])
  94  				if mapped.ValueType() != InvalidValue {
  95  					mappedAll[field] = mapped
  96  				}
  97  				return true
  98  			})
  99  			return wrapMap(mappedAll)
 100  		}
 101  		return newInvalidAny(path)
 102  	default:
 103  		return newInvalidAny(path)
 104  	}
 105  }
 106  
 107  func (any *objectLazyAny) Keys() []string {
 108  	keys := []string{}
 109  	iter := any.cfg.BorrowIterator(any.buf)
 110  	defer any.cfg.ReturnIterator(iter)
 111  	iter.ReadMapCB(func(iter *Iterator, field string) bool {
 112  		iter.Skip()
 113  		keys = append(keys, field)
 114  		return true
 115  	})
 116  	return keys
 117  }
 118  
 119  func (any *objectLazyAny) Size() int {
 120  	size := 0
 121  	iter := any.cfg.BorrowIterator(any.buf)
 122  	defer any.cfg.ReturnIterator(iter)
 123  	iter.ReadObjectCB(func(iter *Iterator, field string) bool {
 124  		iter.Skip()
 125  		size++
 126  		return true
 127  	})
 128  	return size
 129  }
 130  
 131  func (any *objectLazyAny) WriteTo(stream *Stream) {
 132  	stream.Write(any.buf)
 133  }
 134  
 135  func (any *objectLazyAny) GetInterface() interface{} {
 136  	iter := any.cfg.BorrowIterator(any.buf)
 137  	defer any.cfg.ReturnIterator(iter)
 138  	return iter.Read()
 139  }
 140  
 141  type objectAny struct {
 142  	baseAny
 143  	err error
 144  	val reflect.Value
 145  }
 146  
 147  func wrapStruct(val interface{}) *objectAny {
 148  	return &objectAny{baseAny{}, nil, reflect.ValueOf(val)}
 149  }
 150  
 151  func (any *objectAny) ValueType() ValueType {
 152  	return ObjectValue
 153  }
 154  
 155  func (any *objectAny) MustBeValid() Any {
 156  	return any
 157  }
 158  
 159  func (any *objectAny) Parse() *Iterator {
 160  	return nil
 161  }
 162  
 163  func (any *objectAny) LastError() error {
 164  	return any.err
 165  }
 166  
 167  func (any *objectAny) ToBool() bool {
 168  	return any.val.NumField() != 0
 169  }
 170  
 171  func (any *objectAny) ToInt() int {
 172  	return 0
 173  }
 174  
 175  func (any *objectAny) ToInt32() int32 {
 176  	return 0
 177  }
 178  
 179  func (any *objectAny) ToInt64() int64 {
 180  	return 0
 181  }
 182  
 183  func (any *objectAny) ToUint() uint {
 184  	return 0
 185  }
 186  
 187  func (any *objectAny) ToUint32() uint32 {
 188  	return 0
 189  }
 190  
 191  func (any *objectAny) ToUint64() uint64 {
 192  	return 0
 193  }
 194  
 195  func (any *objectAny) ToFloat32() float32 {
 196  	return 0
 197  }
 198  
 199  func (any *objectAny) ToFloat64() float64 {
 200  	return 0
 201  }
 202  
 203  func (any *objectAny) ToString() string {
 204  	str, err := MarshalToString(any.val.Interface())
 205  	any.err = err
 206  	return str
 207  }
 208  
 209  func (any *objectAny) Get(path ...interface{}) Any {
 210  	if len(path) == 0 {
 211  		return any
 212  	}
 213  	switch firstPath := path[0].(type) {
 214  	case string:
 215  		field := any.val.FieldByName(firstPath)
 216  		if !field.IsValid() {
 217  			return newInvalidAny(path)
 218  		}
 219  		return Wrap(field.Interface())
 220  	case int32:
 221  		if '*' == firstPath {
 222  			mappedAll := map[string]Any{}
 223  			for i := 0; i < any.val.NumField(); i++ {
 224  				field := any.val.Field(i)
 225  				if field.CanInterface() {
 226  					mapped := Wrap(field.Interface()).Get(path[1:]...)
 227  					if mapped.ValueType() != InvalidValue {
 228  						mappedAll[any.val.Type().Field(i).Name] = mapped
 229  					}
 230  				}
 231  			}
 232  			return wrapMap(mappedAll)
 233  		}
 234  		return newInvalidAny(path)
 235  	default:
 236  		return newInvalidAny(path)
 237  	}
 238  }
 239  
 240  func (any *objectAny) Keys() []string {
 241  	keys := make([]string, 0, any.val.NumField())
 242  	for i := 0; i < any.val.NumField(); i++ {
 243  		keys = append(keys, any.val.Type().Field(i).Name)
 244  	}
 245  	return keys
 246  }
 247  
 248  func (any *objectAny) Size() int {
 249  	return any.val.NumField()
 250  }
 251  
 252  func (any *objectAny) WriteTo(stream *Stream) {
 253  	stream.WriteVal(any.val)
 254  }
 255  
 256  func (any *objectAny) GetInterface() interface{} {
 257  	return any.val.Interface()
 258  }
 259  
 260  type mapAny struct {
 261  	baseAny
 262  	err error
 263  	val reflect.Value
 264  }
 265  
 266  func wrapMap(val interface{}) *mapAny {
 267  	return &mapAny{baseAny{}, nil, reflect.ValueOf(val)}
 268  }
 269  
 270  func (any *mapAny) ValueType() ValueType {
 271  	return ObjectValue
 272  }
 273  
 274  func (any *mapAny) MustBeValid() Any {
 275  	return any
 276  }
 277  
 278  func (any *mapAny) Parse() *Iterator {
 279  	return nil
 280  }
 281  
 282  func (any *mapAny) LastError() error {
 283  	return any.err
 284  }
 285  
 286  func (any *mapAny) ToBool() bool {
 287  	return true
 288  }
 289  
 290  func (any *mapAny) ToInt() int {
 291  	return 0
 292  }
 293  
 294  func (any *mapAny) ToInt32() int32 {
 295  	return 0
 296  }
 297  
 298  func (any *mapAny) ToInt64() int64 {
 299  	return 0
 300  }
 301  
 302  func (any *mapAny) ToUint() uint {
 303  	return 0
 304  }
 305  
 306  func (any *mapAny) ToUint32() uint32 {
 307  	return 0
 308  }
 309  
 310  func (any *mapAny) ToUint64() uint64 {
 311  	return 0
 312  }
 313  
 314  func (any *mapAny) ToFloat32() float32 {
 315  	return 0
 316  }
 317  
 318  func (any *mapAny) ToFloat64() float64 {
 319  	return 0
 320  }
 321  
 322  func (any *mapAny) ToString() string {
 323  	str, err := MarshalToString(any.val.Interface())
 324  	any.err = err
 325  	return str
 326  }
 327  
 328  func (any *mapAny) Get(path ...interface{}) Any {
 329  	if len(path) == 0 {
 330  		return any
 331  	}
 332  	switch firstPath := path[0].(type) {
 333  	case int32:
 334  		if '*' == firstPath {
 335  			mappedAll := map[string]Any{}
 336  			for _, key := range any.val.MapKeys() {
 337  				keyAsStr := key.String()
 338  				element := Wrap(any.val.MapIndex(key).Interface())
 339  				mapped := element.Get(path[1:]...)
 340  				if mapped.ValueType() != InvalidValue {
 341  					mappedAll[keyAsStr] = mapped
 342  				}
 343  			}
 344  			return wrapMap(mappedAll)
 345  		}
 346  		return newInvalidAny(path)
 347  	default:
 348  		value := any.val.MapIndex(reflect.ValueOf(firstPath))
 349  		if !value.IsValid() {
 350  			return newInvalidAny(path)
 351  		}
 352  		return Wrap(value.Interface())
 353  	}
 354  }
 355  
 356  func (any *mapAny) Keys() []string {
 357  	keys := make([]string, 0, any.val.Len())
 358  	for _, key := range any.val.MapKeys() {
 359  		keys = append(keys, key.String())
 360  	}
 361  	return keys
 362  }
 363  
 364  func (any *mapAny) Size() int {
 365  	return any.val.Len()
 366  }
 367  
 368  func (any *mapAny) WriteTo(stream *Stream) {
 369  	stream.WriteVal(any.val)
 370  }
 371  
 372  func (any *mapAny) GetInterface() interface{} {
 373  	return any.val.Interface()
 374  }
 375