khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | package jsoniter |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type numberLazyAny struct { |
| 9 | baseAny |
| 10 | cfg *frozenConfig |
| 11 | buf []byte |
| 12 | err error |
| 13 | } |
| 14 | |
| 15 | func (any *numberLazyAny) ValueType() ValueType { |
| 16 | return NumberValue |
| 17 | } |
| 18 | |
| 19 | func (any *numberLazyAny) MustBeValid() Any { |
| 20 | return any |
| 21 | } |
| 22 | |
| 23 | func (any *numberLazyAny) LastError() error { |
| 24 | return any.err |
| 25 | } |
| 26 | |
| 27 | func (any *numberLazyAny) ToBool() bool { |
| 28 | return any.ToFloat64() != 0 |
| 29 | } |
| 30 | |
| 31 | func (any *numberLazyAny) ToInt() int { |
| 32 | iter := any.cfg.BorrowIterator(any.buf) |
| 33 | defer any.cfg.ReturnIterator(iter) |
| 34 | val := iter.ReadInt() |
| 35 | if iter.Error != nil && iter.Error != io.EOF { |
| 36 | any.err = iter.Error |
| 37 | } |
| 38 | return val |
| 39 | } |
| 40 | |
| 41 | func (any *numberLazyAny) ToInt32() int32 { |
| 42 | iter := any.cfg.BorrowIterator(any.buf) |
| 43 | defer any.cfg.ReturnIterator(iter) |
| 44 | val := iter.ReadInt32() |
| 45 | if iter.Error != nil && iter.Error != io.EOF { |
| 46 | any.err = iter.Error |
| 47 | } |
| 48 | return val |
| 49 | } |
| 50 | |
| 51 | func (any *numberLazyAny) ToInt64() int64 { |
| 52 | iter := any.cfg.BorrowIterator(any.buf) |
| 53 | defer any.cfg.ReturnIterator(iter) |
| 54 | val := iter.ReadInt64() |
| 55 | if iter.Error != nil && iter.Error != io.EOF { |
| 56 | any.err = iter.Error |
| 57 | } |
| 58 | return val |
| 59 | } |
| 60 | |
| 61 | func (any *numberLazyAny) ToUint() uint { |
| 62 | iter := any.cfg.BorrowIterator(any.buf) |
| 63 | defer any.cfg.ReturnIterator(iter) |
| 64 | val := iter.ReadUint() |
| 65 | if iter.Error != nil && iter.Error != io.EOF { |
| 66 | any.err = iter.Error |
| 67 | } |
| 68 | return val |
| 69 | } |
| 70 | |
| 71 | func (any *numberLazyAny) ToUint32() uint32 { |
| 72 | iter := any.cfg.BorrowIterator(any.buf) |
| 73 | defer any.cfg.ReturnIterator(iter) |
| 74 | val := iter.ReadUint32() |
| 75 | if iter.Error != nil && iter.Error != io.EOF { |
| 76 | any.err = iter.Error |
| 77 | } |
| 78 | return val |
| 79 | } |
| 80 | |
| 81 | func (any *numberLazyAny) ToUint64() uint64 { |
| 82 | iter := any.cfg.BorrowIterator(any.buf) |
| 83 | defer any.cfg.ReturnIterator(iter) |
| 84 | val := iter.ReadUint64() |
| 85 | if iter.Error != nil && iter.Error != io.EOF { |
| 86 | any.err = iter.Error |
| 87 | } |
| 88 | return val |
| 89 | } |
| 90 | |
| 91 | func (any *numberLazyAny) ToFloat32() float32 { |
| 92 | iter := any.cfg.BorrowIterator(any.buf) |
| 93 | defer any.cfg.ReturnIterator(iter) |
| 94 | val := iter.ReadFloat32() |
| 95 | if iter.Error != nil && iter.Error != io.EOF { |
| 96 | any.err = iter.Error |
| 97 | } |
| 98 | return val |
| 99 | } |
| 100 | |
| 101 | func (any *numberLazyAny) ToFloat64() float64 { |
| 102 | iter := any.cfg.BorrowIterator(any.buf) |
| 103 | defer any.cfg.ReturnIterator(iter) |
| 104 | val := iter.ReadFloat64() |
| 105 | if iter.Error != nil && iter.Error != io.EOF { |
| 106 | any.err = iter.Error |
| 107 | } |
| 108 | return val |
| 109 | } |
| 110 | |
| 111 | func (any *numberLazyAny) ToString() string { |
| 112 | return *(*string)(unsafe.Pointer(&any.buf)) |
| 113 | } |
| 114 | |
| 115 | func (any *numberLazyAny) WriteTo(stream *Stream) { |
| 116 | stream.Write(any.buf) |
| 117 | } |
| 118 | |
| 119 | func (any *numberLazyAny) GetInterface() interface{} { |
| 120 | iter := any.cfg.BorrowIterator(any.buf) |
| 121 | defer any.cfg.ReturnIterator(iter) |
| 122 | return iter.Read() |
| 123 | } |