iterator.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/native/types`
  20. )
  21. type Pair struct {
  22. Key string
  23. Value Node
  24. }
  25. // Values returns iterator for array's children traversal
  26. func (self *Node) Values() (ListIterator, error) {
  27. if err := self.should(types.V_ARRAY, "an array"); err != nil {
  28. return ListIterator{}, err
  29. }
  30. return self.values(), nil
  31. }
  32. func (self *Node) values() ListIterator {
  33. return ListIterator{Iterator{p: self}}
  34. }
  35. // Properties returns iterator for object's children traversal
  36. func (self *Node) Properties() (ObjectIterator, error) {
  37. if err := self.should(types.V_OBJECT, "an object"); err != nil {
  38. return ObjectIterator{}, err
  39. }
  40. return self.properties(), nil
  41. }
  42. func (self *Node) properties() ObjectIterator {
  43. return ObjectIterator{Iterator{p: self}}
  44. }
  45. type Iterator struct {
  46. i int
  47. p *Node
  48. }
  49. func (self *Iterator) Pos() int {
  50. return self.i
  51. }
  52. func (self *Iterator) Len() int {
  53. return self.p.len()
  54. }
  55. // HasNext reports if it is the end of iteration or has error.
  56. func (self *Iterator) HasNext() bool {
  57. if !self.p.isLazy() {
  58. return self.p.Valid() && self.i < self.p.len()
  59. } else if self.p.t == _V_ARRAY_LAZY {
  60. return self.p.skipNextNode().Valid()
  61. } else if self.p.t == _V_OBJECT_LAZY {
  62. pair := self.p.skipNextPair()
  63. if pair == nil {
  64. return false
  65. }
  66. return pair.Value.Valid()
  67. }
  68. return false
  69. }
  70. // ListIterator is specialized iterator for V_ARRAY
  71. type ListIterator struct {
  72. Iterator
  73. }
  74. // ObjectIterator is specialized iterator for V_ARRAY
  75. type ObjectIterator struct {
  76. Iterator
  77. }
  78. func (self *ListIterator) next() *Node {
  79. next_start:
  80. if !self.HasNext() {
  81. return nil
  82. } else {
  83. n := self.p.nodeAt(self.i)
  84. self.i++
  85. if !n.Exists() {
  86. goto next_start
  87. }
  88. return n
  89. }
  90. }
  91. // Next scans through children of underlying V_ARRAY,
  92. // copies each child to v, and returns .HasNext().
  93. func (self *ListIterator) Next(v *Node) bool {
  94. n := self.next()
  95. if n == nil {
  96. return false
  97. }
  98. *v = *n
  99. return true
  100. }
  101. func (self *ObjectIterator) next() *Pair {
  102. next_start:
  103. if !self.HasNext() {
  104. return nil
  105. } else {
  106. n := self.p.pairAt(self.i)
  107. self.i++
  108. if n == nil || !n.Value.Exists() {
  109. goto next_start
  110. }
  111. return n
  112. }
  113. }
  114. // Next scans through children of underlying V_OBJECT,
  115. // copies each child to v, and returns .HasNext().
  116. func (self *ObjectIterator) Next(p *Pair) bool {
  117. n := self.next()
  118. if n == nil {
  119. return false
  120. }
  121. *p = *n
  122. return true
  123. }
  124. // Sequence represents scanning path of single-layer nodes.
  125. // Index indicates the value's order in both V_ARRAY and V_OBJECT json.
  126. // Key is the value's key (for V_OBJECT json only, otherwise it will be nil).
  127. type Sequence struct {
  128. Index int
  129. Key *string
  130. // Level int
  131. }
  132. // String is string representation of one Sequence
  133. func (s Sequence) String() string {
  134. k := ""
  135. if s.Key != nil {
  136. k = *s.Key
  137. }
  138. return fmt.Sprintf("Sequence(%d, %q)", s.Index, k)
  139. }
  140. type Scanner func(path Sequence, node *Node) bool
  141. // ForEach scans one V_OBJECT node's children from JSON head to tail,
  142. // and pass the Sequence and Node of corresponding JSON value.
  143. //
  144. // Especailly, if the node is not V_ARRAY or V_OBJECT,
  145. // the node itself will be returned and Sequence.Index == -1.
  146. //
  147. // NOTICE: A unsetted node WON'T trigger sc, but its index still counts into Path.Index
  148. func (self *Node) ForEach(sc Scanner) error {
  149. switch self.itype() {
  150. case types.V_ARRAY:
  151. iter, err := self.Values()
  152. if err != nil {
  153. return err
  154. }
  155. v := iter.next()
  156. for v != nil {
  157. if !sc(Sequence{iter.i-1, nil}, v) {
  158. return nil
  159. }
  160. v = iter.next()
  161. }
  162. case types.V_OBJECT:
  163. iter, err := self.Properties()
  164. if err != nil {
  165. return err
  166. }
  167. v := iter.next()
  168. for v != nil {
  169. if !sc(Sequence{iter.i-1, &v.Key}, &v.Value) {
  170. return nil
  171. }
  172. v = iter.next()
  173. }
  174. default:
  175. if self.Check() != nil {
  176. return self
  177. }
  178. sc(Sequence{-1, nil}, self)
  179. }
  180. return nil
  181. }