feature_any_array.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "reflect"
  5. "unsafe"
  6. )
  7. type arrayLazyAny struct {
  8. baseAny
  9. buf []byte
  10. iter *Iterator
  11. err error
  12. cache []Any
  13. remaining []byte
  14. }
  15. func (any *arrayLazyAny) ValueType() ValueType {
  16. return Array
  17. }
  18. func (any *arrayLazyAny) Parse() *Iterator {
  19. iter := any.iter
  20. if iter == nil {
  21. iter = NewIterator()
  22. any.iter = iter
  23. }
  24. iter.ResetBytes(any.remaining)
  25. return iter
  26. }
  27. func (any *arrayLazyAny) fillCacheUntil(target int) Any {
  28. if any.remaining == nil {
  29. if target >= len(any.cache) {
  30. return nil
  31. }
  32. return any.cache[target]
  33. }
  34. if any.cache == nil {
  35. any.cache = make([]Any, 0, 8)
  36. }
  37. i := len(any.cache)
  38. if target < i {
  39. return any.cache[target]
  40. }
  41. iter := any.Parse()
  42. if len(any.remaining) == len(any.buf) {
  43. iter.head++
  44. c := iter.nextToken()
  45. if c != ']' {
  46. iter.unreadByte()
  47. element := iter.readAny(iter)
  48. any.cache = append(any.cache, element)
  49. if target == 0 {
  50. any.remaining = iter.buf[iter.head:]
  51. any.err = iter.Error
  52. return element
  53. }
  54. i = 1
  55. } else {
  56. any.remaining = nil
  57. any.err = iter.Error
  58. return nil
  59. }
  60. }
  61. for iter.nextToken() == ',' {
  62. element := iter.readAny(iter)
  63. any.cache = append(any.cache, element)
  64. if i == target {
  65. any.remaining = iter.buf[iter.head:]
  66. any.err = iter.Error
  67. return element
  68. }
  69. i++
  70. }
  71. any.remaining = nil
  72. any.err = iter.Error
  73. return nil
  74. }
  75. func (any *arrayLazyAny) fillCache() {
  76. if any.remaining == nil {
  77. return
  78. }
  79. if any.cache == nil {
  80. any.cache = make([]Any, 0, 8)
  81. }
  82. iter := any.Parse()
  83. if len(any.remaining) == len(any.buf) {
  84. iter.head++
  85. c := iter.nextToken()
  86. if c != ']' {
  87. iter.unreadByte()
  88. any.cache = append(any.cache, iter.readAny(iter))
  89. } else {
  90. any.remaining = nil
  91. any.err = iter.Error
  92. return
  93. }
  94. }
  95. for iter.nextToken() == ',' {
  96. any.cache = append(any.cache, iter.readAny(iter))
  97. }
  98. any.remaining = nil
  99. any.err = iter.Error
  100. }
  101. func (any *arrayLazyAny) LastError() error {
  102. return any.err
  103. }
  104. func (any *arrayLazyAny) ToBool() bool {
  105. if any.cache == nil {
  106. any.IterateArray() // trigger first element read
  107. }
  108. return len(any.cache) != 0
  109. }
  110. func (any *arrayLazyAny) ToInt() int {
  111. if any.cache == nil {
  112. any.IterateArray() // trigger first element read
  113. }
  114. if len(any.cache) == 0 {
  115. return 0
  116. }
  117. return 1
  118. }
  119. func (any *arrayLazyAny) ToInt32() int32 {
  120. if any.cache == nil {
  121. any.IterateArray() // trigger first element read
  122. }
  123. if len(any.cache) == 0 {
  124. return 0
  125. }
  126. return 1
  127. }
  128. func (any *arrayLazyAny) ToInt64() int64 {
  129. if any.cache == nil {
  130. any.IterateArray() // trigger first element read
  131. }
  132. if len(any.cache) == 0 {
  133. return 0
  134. }
  135. return 1
  136. }
  137. func (any *arrayLazyAny) ToUint() uint {
  138. if any.cache == nil {
  139. any.IterateArray() // trigger first element read
  140. }
  141. if len(any.cache) == 0 {
  142. return 0
  143. }
  144. return 1
  145. }
  146. func (any *arrayLazyAny) ToUint32() uint32 {
  147. if any.cache == nil {
  148. any.IterateArray() // trigger first element read
  149. }
  150. if len(any.cache) == 0 {
  151. return 0
  152. }
  153. return 1
  154. }
  155. func (any *arrayLazyAny) ToUint64() uint64 {
  156. if any.cache == nil {
  157. any.IterateArray() // trigger first element read
  158. }
  159. if len(any.cache) == 0 {
  160. return 0
  161. }
  162. return 1
  163. }
  164. func (any *arrayLazyAny) ToFloat32() float32 {
  165. if any.cache == nil {
  166. any.IterateArray() // trigger first element read
  167. }
  168. if len(any.cache) == 0 {
  169. return 0
  170. }
  171. return 1
  172. }
  173. func (any *arrayLazyAny) ToFloat64() float64 {
  174. if any.cache == nil {
  175. any.IterateArray() // trigger first element read
  176. }
  177. if len(any.cache) == 0 {
  178. return 0
  179. }
  180. return 1
  181. }
  182. func (any *arrayLazyAny) ToString() string {
  183. if len(any.remaining) == len(any.buf) {
  184. // nothing has been parsed yet
  185. return *(*string)(unsafe.Pointer(&any.buf))
  186. } else {
  187. any.fillCache()
  188. str, err := MarshalToString(any.cache)
  189. any.err = err
  190. return str
  191. }
  192. }
  193. func (any *arrayLazyAny) Get(path ...interface{}) Any {
  194. if len(path) == 0 {
  195. return any
  196. }
  197. var element Any
  198. switch firstPath := path[0].(type) {
  199. case int:
  200. element = any.fillCacheUntil(firstPath)
  201. if element == nil {
  202. element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
  203. }
  204. case int32:
  205. if '*' == firstPath {
  206. any.fillCache()
  207. arr := make([]Any, 0, len(any.cache))
  208. for _, element := range any.cache {
  209. found := element.Get(path[1:]...)
  210. if found.ValueType() != Invalid {
  211. arr = append(arr, found)
  212. }
  213. }
  214. return wrapArray(arr)
  215. } else {
  216. element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
  217. }
  218. default:
  219. element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
  220. }
  221. if len(path) == 1 {
  222. return element
  223. } else {
  224. return element.Get(path[1:]...)
  225. }
  226. }
  227. func (any *arrayLazyAny) Size() int {
  228. any.fillCache()
  229. return len(any.cache)
  230. }
  231. func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
  232. if any.cache == nil {
  233. any.cache = make([]Any, 0, 8)
  234. }
  235. remaining := any.remaining
  236. if len(remaining) == len(any.buf) {
  237. iter := any.Parse()
  238. iter.head++
  239. c := iter.nextToken()
  240. if c != ']' {
  241. iter.unreadByte()
  242. v := iter.readAny(iter)
  243. any.cache = append(any.cache, v)
  244. remaining = iter.buf[iter.head:]
  245. any.remaining = remaining
  246. } else {
  247. remaining = nil
  248. any.remaining = nil
  249. any.err = iter.Error
  250. return nil, false
  251. }
  252. }
  253. if len(any.cache) == 0 {
  254. return nil, false
  255. }
  256. arr := any.cache
  257. nextValue := arr[0]
  258. i := 1
  259. return func() (Any, bool) {
  260. value := nextValue
  261. if i < len(arr) {
  262. // read from cache
  263. nextValue = arr[i]
  264. i++
  265. return value, true
  266. } else {
  267. // read from buffer
  268. iter := any.iter
  269. if iter == nil {
  270. iter = NewIterator()
  271. any.iter = iter
  272. }
  273. iter.ResetBytes(remaining)
  274. c := iter.nextToken()
  275. if c == ',' {
  276. nextValue = iter.readAny(iter)
  277. any.cache = append(any.cache, nextValue)
  278. remaining = iter.buf[iter.head:]
  279. any.remaining = remaining
  280. any.err = iter.Error
  281. return value, true
  282. } else {
  283. remaining = nil
  284. any.remaining = nil
  285. any.err = iter.Error
  286. return value, false
  287. }
  288. }
  289. }, true
  290. }
  291. func (any *arrayLazyAny) GetArray() []Any {
  292. any.fillCache()
  293. return any.cache
  294. }
  295. func (any *arrayLazyAny) SetArray(newList []Any) bool {
  296. any.fillCache()
  297. any.cache = newList
  298. return true
  299. }
  300. func (any *arrayLazyAny) WriteTo(stream *Stream) {
  301. if len(any.remaining) == len(any.buf) {
  302. // nothing has been parsed yet
  303. stream.Write(any.buf)
  304. } else {
  305. any.fillCache()
  306. stream.WriteVal(any.cache)
  307. }
  308. }
  309. func (any *arrayLazyAny) GetInterface() interface{} {
  310. any.fillCache()
  311. return any.cache
  312. }
  313. type arrayAny struct {
  314. baseAny
  315. err error
  316. cache []Any
  317. val reflect.Value
  318. }
  319. func wrapArray(val interface{}) *arrayAny {
  320. return &arrayAny{baseAny{}, nil, nil, reflect.ValueOf(val)}
  321. }
  322. func (any *arrayAny) ValueType() ValueType {
  323. return Array
  324. }
  325. func (any *arrayAny) Parse() *Iterator {
  326. return nil
  327. }
  328. func (any *arrayAny) LastError() error {
  329. return any.err
  330. }
  331. func (any *arrayAny) ToBool() bool {
  332. return any.val.Len() != 0
  333. }
  334. func (any *arrayAny) ToInt() int {
  335. if any.val.Len() == 0 {
  336. return 0
  337. }
  338. return 1
  339. }
  340. func (any *arrayAny) ToInt32() int32 {
  341. if any.val.Len() == 0 {
  342. return 0
  343. }
  344. return 1
  345. }
  346. func (any *arrayAny) ToInt64() int64 {
  347. if any.val.Len() == 0 {
  348. return 0
  349. }
  350. return 1
  351. }
  352. func (any *arrayAny) ToUint() uint {
  353. if any.val.Len() == 0 {
  354. return 0
  355. }
  356. return 1
  357. }
  358. func (any *arrayAny) ToUint32() uint32 {
  359. if any.val.Len() == 0 {
  360. return 0
  361. }
  362. return 1
  363. }
  364. func (any *arrayAny) ToUint64() uint64 {
  365. if any.val.Len() == 0 {
  366. return 0
  367. }
  368. return 1
  369. }
  370. func (any *arrayAny) ToFloat32() float32 {
  371. if any.val.Len() == 0 {
  372. return 0
  373. }
  374. return 1
  375. }
  376. func (any *arrayAny) ToFloat64() float64 {
  377. if any.val.Len() == 0 {
  378. return 0
  379. }
  380. return 1
  381. }
  382. func (any *arrayAny) ToString() string {
  383. if len(any.cache) == 0 {
  384. // nothing has been parsed yet
  385. str, err := MarshalToString(any.val.Interface())
  386. any.err = err
  387. return str
  388. } else {
  389. any.fillCache()
  390. str, err := MarshalToString(any.cache)
  391. any.err = err
  392. return str
  393. }
  394. }
  395. func (any *arrayAny) fillCacheUntil(idx int) Any {
  396. if idx < len(any.cache) {
  397. return any.cache[idx]
  398. } else {
  399. for i := len(any.cache); i < any.val.Len(); i++ {
  400. element := Wrap(any.val.Index(i).Interface())
  401. any.cache = append(any.cache, element)
  402. if idx == i {
  403. return element
  404. }
  405. }
  406. return nil
  407. }
  408. }
  409. func (any *arrayAny) fillCache() {
  410. any.cache = make([]Any, any.val.Len())
  411. for i := 0; i < any.val.Len(); i++ {
  412. any.cache[i] = Wrap(any.val.Index(i).Interface())
  413. }
  414. }
  415. func (any *arrayAny) Get(path ...interface{}) Any {
  416. if len(path) == 0 {
  417. return any
  418. }
  419. var element Any
  420. switch firstPath := path[0].(type) {
  421. case int:
  422. element = any.fillCacheUntil(firstPath)
  423. if element == nil {
  424. element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
  425. }
  426. case int32:
  427. if '*' == firstPath {
  428. any.fillCache()
  429. mappedAll := make([]Any, 0, len(any.cache))
  430. for _, element := range any.cache {
  431. mapped := element.Get(path[1:]...)
  432. if mapped.ValueType() != Invalid {
  433. mappedAll = append(mappedAll, mapped)
  434. }
  435. }
  436. return wrapArray(mappedAll)
  437. } else {
  438. element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
  439. }
  440. default:
  441. element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
  442. }
  443. if len(path) == 1 {
  444. return element
  445. } else {
  446. return element.Get(path[1:]...)
  447. }
  448. }
  449. func (any *arrayAny) Size() int {
  450. any.fillCache()
  451. return len(any.cache)
  452. }
  453. func (any *arrayAny) IterateArray() (func() (Any, bool), bool) {
  454. if any.val.Len() == 0 {
  455. return nil, false
  456. }
  457. i := 0
  458. return func() (Any, bool) {
  459. if i == any.val.Len() {
  460. return nil, false
  461. }
  462. if i == len(any.cache) {
  463. any.cache = append(any.cache, Wrap(any.val.Index(i).Interface()))
  464. }
  465. val := any.cache[i]
  466. i++
  467. return val, i != any.val.Len()
  468. }, true
  469. }
  470. func (any *arrayAny) GetArray() []Any {
  471. any.fillCache()
  472. return any.cache
  473. }
  474. func (any *arrayAny) SetArray(newList []Any) bool {
  475. any.fillCache()
  476. any.cache = newList
  477. return true
  478. }
  479. func (any *arrayAny) WriteTo(stream *Stream) {
  480. if len(any.cache) == 0 {
  481. // nothing has been parsed yet
  482. stream.WriteVal(any.val)
  483. } else {
  484. any.fillCache()
  485. stream.WriteVal(any.cache)
  486. }
  487. }
  488. func (any *arrayAny) GetInterface() interface{} {
  489. any.fillCache()
  490. return any.cache
  491. }