iterator.go 4.7 KB

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