iterator.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package ast
  17. import (
  18. "fmt"
  19. "github.com/bytedance/sonic/internal/caching"
  20. "github.com/bytedance/sonic/internal/native/types"
  21. )
  22. type Pair struct {
  23. hash uint64
  24. Key string
  25. Value Node
  26. }
  27. func NewPair(key string, val Node) Pair {
  28. return Pair{
  29. hash: caching.StrHash(key),
  30. Key: key,
  31. Value: val,
  32. }
  33. }
  34. // Values returns iterator for array's children traversal
  35. func (self *Node) Values() (ListIterator, error) {
  36. if err := self.should(types.V_ARRAY); err != nil {
  37. return ListIterator{}, err
  38. }
  39. return self.values(), nil
  40. }
  41. func (self *Node) values() ListIterator {
  42. return ListIterator{Iterator{p: self}}
  43. }
  44. // Properties returns iterator for object's children traversal
  45. func (self *Node) Properties() (ObjectIterator, error) {
  46. if err := self.should(types.V_OBJECT); err != nil {
  47. return ObjectIterator{}, err
  48. }
  49. return self.properties(), nil
  50. }
  51. func (self *Node) properties() ObjectIterator {
  52. return ObjectIterator{Iterator{p: self}}
  53. }
  54. type Iterator struct {
  55. i int
  56. p *Node
  57. }
  58. func (self *Iterator) Pos() int {
  59. return self.i
  60. }
  61. func (self *Iterator) Len() int {
  62. return self.p.len()
  63. }
  64. // HasNext reports if it is the end of iteration or has error.
  65. func (self *Iterator) HasNext() bool {
  66. if !self.p.isLazy() {
  67. return self.p.Valid() && self.i < self.p.len()
  68. } else if self.p.t == _V_ARRAY_LAZY {
  69. return self.p.skipNextNode().Valid()
  70. } else if self.p.t == _V_OBJECT_LAZY {
  71. pair := self.p.skipNextPair()
  72. if pair == nil {
  73. return false
  74. }
  75. return pair.Value.Valid()
  76. }
  77. return false
  78. }
  79. // ListIterator is specialized iterator for V_ARRAY
  80. type ListIterator struct {
  81. Iterator
  82. }
  83. // ObjectIterator is specialized iterator for V_ARRAY
  84. type ObjectIterator struct {
  85. Iterator
  86. }
  87. func (self *ListIterator) next() *Node {
  88. next_start:
  89. if !self.HasNext() {
  90. return nil
  91. } else {
  92. n := self.p.nodeAt(self.i)
  93. self.i++
  94. if !n.Exists() {
  95. goto next_start
  96. }
  97. return n
  98. }
  99. }
  100. // Next scans through children of underlying V_ARRAY,
  101. // copies each child to v, and returns .HasNext().
  102. func (self *ListIterator) Next(v *Node) bool {
  103. n := self.next()
  104. if n == nil {
  105. return false
  106. }
  107. *v = *n
  108. return true
  109. }
  110. func (self *ObjectIterator) next() *Pair {
  111. next_start:
  112. if !self.HasNext() {
  113. return nil
  114. } else {
  115. n := self.p.pairAt(self.i)
  116. self.i++
  117. if n == nil || !n.Value.Exists() {
  118. goto next_start
  119. }
  120. return n
  121. }
  122. }
  123. // Next scans through children of underlying V_OBJECT,
  124. // copies each child to v, and returns .HasNext().
  125. func (self *ObjectIterator) Next(p *Pair) bool {
  126. n := self.next()
  127. if n == nil {
  128. return false
  129. }
  130. *p = *n
  131. return true
  132. }
  133. // Sequence represents scanning path of single-layer nodes.
  134. // Index indicates the value's order in both V_ARRAY and V_OBJECT json.
  135. // Key is the value's key (for V_OBJECT json only, otherwise it will be nil).
  136. type Sequence struct {
  137. Index int
  138. Key *string
  139. // Level int
  140. }
  141. // String is string representation of one Sequence
  142. func (s Sequence) String() string {
  143. k := ""
  144. if s.Key != nil {
  145. k = *s.Key
  146. }
  147. return fmt.Sprintf("Sequence(%d, %q)", s.Index, k)
  148. }
  149. type Scanner func(path Sequence, node *Node) bool
  150. // ForEach scans one V_OBJECT node's children from JSON head to tail,
  151. // and pass the Sequence and Node of corresponding JSON value.
  152. //
  153. // Especially, if the node is not V_ARRAY or V_OBJECT,
  154. // the node itself will be returned and Sequence.Index == -1.
  155. //
  156. // NOTICE: A unsetted node WON'T trigger sc, but its index still counts into Path.Index
  157. func (self *Node) ForEach(sc Scanner) error {
  158. if err := self.checkRaw(); err != nil {
  159. return err
  160. }
  161. switch self.itype() {
  162. case types.V_ARRAY:
  163. iter, err := self.Values()
  164. if err != nil {
  165. return err
  166. }
  167. v := iter.next()
  168. for v != nil {
  169. if !sc(Sequence{iter.i-1, nil}, v) {
  170. return nil
  171. }
  172. v = iter.next()
  173. }
  174. case types.V_OBJECT:
  175. iter, err := self.Properties()
  176. if err != nil {
  177. return err
  178. }
  179. v := iter.next()
  180. for v != nil {
  181. if !sc(Sequence{iter.i-1, &v.Key}, &v.Value) {
  182. return nil
  183. }
  184. v = iter.next()
  185. }
  186. default:
  187. if self.Check() != nil {
  188. return self
  189. }
  190. sc(Sequence{-1, nil}, self)
  191. }
  192. return nil
  193. }