any_array.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package jsoniter
  2. import (
  3. "reflect"
  4. "unsafe"
  5. )
  6. type arrayLazyAny struct {
  7. baseAny
  8. cfg *frozenConfig
  9. buf []byte
  10. err error
  11. }
  12. func (any *arrayLazyAny) ValueType() ValueType {
  13. return ArrayValue
  14. }
  15. func (any *arrayLazyAny) MustBeValid() Any {
  16. return any
  17. }
  18. func (any *arrayLazyAny) LastError() error {
  19. return any.err
  20. }
  21. func (any *arrayLazyAny) ToBool() bool {
  22. iter := any.cfg.BorrowIterator(any.buf)
  23. defer any.cfg.ReturnIterator(iter)
  24. return iter.ReadArray()
  25. }
  26. func (any *arrayLazyAny) ToInt() int {
  27. if any.ToBool() {
  28. return 1
  29. }
  30. return 0
  31. }
  32. func (any *arrayLazyAny) ToInt32() int32 {
  33. if any.ToBool() {
  34. return 1
  35. }
  36. return 0
  37. }
  38. func (any *arrayLazyAny) ToInt64() int64 {
  39. if any.ToBool() {
  40. return 1
  41. }
  42. return 0
  43. }
  44. func (any *arrayLazyAny) ToUint() uint {
  45. if any.ToBool() {
  46. return 1
  47. }
  48. return 0
  49. }
  50. func (any *arrayLazyAny) ToUint32() uint32 {
  51. if any.ToBool() {
  52. return 1
  53. }
  54. return 0
  55. }
  56. func (any *arrayLazyAny) ToUint64() uint64 {
  57. if any.ToBool() {
  58. return 1
  59. }
  60. return 0
  61. }
  62. func (any *arrayLazyAny) ToFloat32() float32 {
  63. if any.ToBool() {
  64. return 1
  65. }
  66. return 0
  67. }
  68. func (any *arrayLazyAny) ToFloat64() float64 {
  69. if any.ToBool() {
  70. return 1
  71. }
  72. return 0
  73. }
  74. func (any *arrayLazyAny) ToString() string {
  75. return *(*string)(unsafe.Pointer(&any.buf))
  76. }
  77. func (any *arrayLazyAny) ToVal(val interface{}) {
  78. iter := any.cfg.BorrowIterator(any.buf)
  79. defer any.cfg.ReturnIterator(iter)
  80. iter.ReadVal(val)
  81. }
  82. func (any *arrayLazyAny) Get(path ...interface{}) Any {
  83. if len(path) == 0 {
  84. return any
  85. }
  86. switch firstPath := path[0].(type) {
  87. case int:
  88. iter := any.cfg.BorrowIterator(any.buf)
  89. defer any.cfg.ReturnIterator(iter)
  90. valueBytes := locateArrayElement(iter, firstPath)
  91. if valueBytes == nil {
  92. return newInvalidAny(path)
  93. }
  94. iter.ResetBytes(valueBytes)
  95. return locatePath(iter, path[1:])
  96. case int32:
  97. if '*' == firstPath {
  98. iter := any.cfg.BorrowIterator(any.buf)
  99. defer any.cfg.ReturnIterator(iter)
  100. arr := make([]Any, 0)
  101. iter.ReadArrayCB(func(iter *Iterator) bool {
  102. found := iter.readAny().Get(path[1:]...)
  103. if found.ValueType() != InvalidValue {
  104. arr = append(arr, found)
  105. }
  106. return true
  107. })
  108. return wrapArray(arr)
  109. }
  110. return newInvalidAny(path)
  111. default:
  112. return newInvalidAny(path)
  113. }
  114. }
  115. func (any *arrayLazyAny) Size() int {
  116. size := 0
  117. iter := any.cfg.BorrowIterator(any.buf)
  118. defer any.cfg.ReturnIterator(iter)
  119. iter.ReadArrayCB(func(iter *Iterator) bool {
  120. size++
  121. iter.Skip()
  122. return true
  123. })
  124. return size
  125. }
  126. func (any *arrayLazyAny) WriteTo(stream *Stream) {
  127. stream.Write(any.buf)
  128. }
  129. func (any *arrayLazyAny) GetInterface() interface{} {
  130. iter := any.cfg.BorrowIterator(any.buf)
  131. defer any.cfg.ReturnIterator(iter)
  132. return iter.Read()
  133. }
  134. type arrayAny struct {
  135. baseAny
  136. val reflect.Value
  137. }
  138. func wrapArray(val interface{}) *arrayAny {
  139. return &arrayAny{baseAny{}, reflect.ValueOf(val)}
  140. }
  141. func (any *arrayAny) ValueType() ValueType {
  142. return ArrayValue
  143. }
  144. func (any *arrayAny) MustBeValid() Any {
  145. return any
  146. }
  147. func (any *arrayAny) LastError() error {
  148. return nil
  149. }
  150. func (any *arrayAny) ToBool() bool {
  151. return any.val.Len() != 0
  152. }
  153. func (any *arrayAny) ToInt() int {
  154. if any.val.Len() == 0 {
  155. return 0
  156. }
  157. return 1
  158. }
  159. func (any *arrayAny) ToInt32() int32 {
  160. if any.val.Len() == 0 {
  161. return 0
  162. }
  163. return 1
  164. }
  165. func (any *arrayAny) ToInt64() int64 {
  166. if any.val.Len() == 0 {
  167. return 0
  168. }
  169. return 1
  170. }
  171. func (any *arrayAny) ToUint() uint {
  172. if any.val.Len() == 0 {
  173. return 0
  174. }
  175. return 1
  176. }
  177. func (any *arrayAny) ToUint32() uint32 {
  178. if any.val.Len() == 0 {
  179. return 0
  180. }
  181. return 1
  182. }
  183. func (any *arrayAny) ToUint64() uint64 {
  184. if any.val.Len() == 0 {
  185. return 0
  186. }
  187. return 1
  188. }
  189. func (any *arrayAny) ToFloat32() float32 {
  190. if any.val.Len() == 0 {
  191. return 0
  192. }
  193. return 1
  194. }
  195. func (any *arrayAny) ToFloat64() float64 {
  196. if any.val.Len() == 0 {
  197. return 0
  198. }
  199. return 1
  200. }
  201. func (any *arrayAny) ToString() string {
  202. str, _ := MarshalToString(any.val.Interface())
  203. return str
  204. }
  205. func (any *arrayAny) Get(path ...interface{}) Any {
  206. if len(path) == 0 {
  207. return any
  208. }
  209. switch firstPath := path[0].(type) {
  210. case int:
  211. if firstPath < 0 || firstPath >= any.val.Len() {
  212. return newInvalidAny(path)
  213. }
  214. return Wrap(any.val.Index(firstPath).Interface())
  215. case int32:
  216. if '*' == firstPath {
  217. mappedAll := make([]Any, 0)
  218. for i := 0; i < any.val.Len(); i++ {
  219. mapped := Wrap(any.val.Index(i).Interface()).Get(path[1:]...)
  220. if mapped.ValueType() != InvalidValue {
  221. mappedAll = append(mappedAll, mapped)
  222. }
  223. }
  224. return wrapArray(mappedAll)
  225. }
  226. return newInvalidAny(path)
  227. default:
  228. return newInvalidAny(path)
  229. }
  230. }
  231. func (any *arrayAny) Size() int {
  232. return any.val.Len()
  233. }
  234. func (any *arrayAny) WriteTo(stream *Stream) {
  235. stream.WriteVal(any.val)
  236. }
  237. func (any *arrayAny) GetInterface() interface{} {
  238. return any.val.Interface()
  239. }