node.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package jsonpath
  14. import "fmt"
  15. // NodeType identifies the type of a parse tree node.
  16. type NodeType int
  17. // Type returns itself and provides an easy default implementation
  18. func (t NodeType) Type() NodeType {
  19. return t
  20. }
  21. func (t NodeType) String() string {
  22. return NodeTypeName[t]
  23. }
  24. const (
  25. NodeText NodeType = iota
  26. NodeArray
  27. NodeList
  28. NodeField
  29. NodeIdentifier
  30. NodeFilter
  31. NodeInt
  32. NodeFloat
  33. NodeWildcard
  34. NodeRecursive
  35. NodeUnion
  36. )
  37. var NodeTypeName = map[NodeType]string{
  38. NodeText: "NodeText",
  39. NodeArray: "NodeArray",
  40. NodeList: "NodeList",
  41. NodeField: "NodeField",
  42. NodeIdentifier: "NodeIdentifier",
  43. NodeFilter: "NodeFilter",
  44. NodeInt: "NodeInt",
  45. NodeFloat: "NodeFloat",
  46. NodeWildcard: "NodeWildcard",
  47. NodeRecursive: "NodeRecursive",
  48. NodeUnion: "NodeUnion",
  49. }
  50. type Node interface {
  51. Type() NodeType
  52. String() string
  53. }
  54. // ListNode holds a sequence of nodes.
  55. type ListNode struct {
  56. NodeType
  57. Nodes []Node // The element nodes in lexical order.
  58. }
  59. func newList() *ListNode {
  60. return &ListNode{NodeType: NodeList}
  61. }
  62. func (l *ListNode) append(n Node) {
  63. l.Nodes = append(l.Nodes, n)
  64. }
  65. func (l *ListNode) String() string {
  66. return fmt.Sprintf("%s", l.Type())
  67. }
  68. // TextNode holds plain text.
  69. type TextNode struct {
  70. NodeType
  71. Text string // The text; may span newlines.
  72. }
  73. func newText(text string) *TextNode {
  74. return &TextNode{NodeType: NodeText, Text: text}
  75. }
  76. func (t *TextNode) String() string {
  77. return fmt.Sprintf("%s: %s", t.Type(), t.Text)
  78. }
  79. // FieldNode holds field of struct
  80. type FieldNode struct {
  81. NodeType
  82. Value string
  83. }
  84. func newField(value string) *FieldNode {
  85. return &FieldNode{NodeType: NodeField, Value: value}
  86. }
  87. func (f *FieldNode) String() string {
  88. return fmt.Sprintf("%s: %s", f.Type(), f.Value)
  89. }
  90. // IdentifierNode holds an identifier
  91. type IdentifierNode struct {
  92. NodeType
  93. Name string
  94. }
  95. func newIdentifier(value string) *IdentifierNode {
  96. return &IdentifierNode{
  97. NodeType: NodeIdentifier,
  98. Name: value,
  99. }
  100. }
  101. func (f *IdentifierNode) String() string {
  102. return fmt.Sprintf("%s: %s", f.Type(), f.Name)
  103. }
  104. // ParamsEntry holds param information for ArrayNode
  105. type ParamsEntry struct {
  106. Value int
  107. Known bool //whether the value is known when parse it
  108. }
  109. // ArrayNode holds start, end, step information for array index selection
  110. type ArrayNode struct {
  111. NodeType
  112. Params [3]ParamsEntry //start, end, step
  113. }
  114. func newArray(params [3]ParamsEntry) *ArrayNode {
  115. return &ArrayNode{
  116. NodeType: NodeArray,
  117. Params: params,
  118. }
  119. }
  120. func (a *ArrayNode) String() string {
  121. return fmt.Sprintf("%s: %v", a.Type(), a.Params)
  122. }
  123. // FilterNode holds operand and operator information for filter
  124. type FilterNode struct {
  125. NodeType
  126. Left *ListNode
  127. Right *ListNode
  128. Operator string
  129. }
  130. func newFilter(left, right *ListNode, operator string) *FilterNode {
  131. return &FilterNode{
  132. NodeType: NodeFilter,
  133. Left: left,
  134. Right: right,
  135. Operator: operator,
  136. }
  137. }
  138. func (f *FilterNode) String() string {
  139. return fmt.Sprintf("%s: %s %s %s", f.Type(), f.Left, f.Operator, f.Right)
  140. }
  141. // IntNode holds integer value
  142. type IntNode struct {
  143. NodeType
  144. Value int
  145. }
  146. func newInt(num int) *IntNode {
  147. return &IntNode{NodeType: NodeInt, Value: num}
  148. }
  149. func (i *IntNode) String() string {
  150. return fmt.Sprintf("%s: %d", i.Type(), i.Value)
  151. }
  152. // FloatNode holds float value
  153. type FloatNode struct {
  154. NodeType
  155. Value float64
  156. }
  157. func newFloat(num float64) *FloatNode {
  158. return &FloatNode{NodeType: NodeFloat, Value: num}
  159. }
  160. func (i *FloatNode) String() string {
  161. return fmt.Sprintf("%s: %f", i.Type(), i.Value)
  162. }
  163. // WildcardNode means a wildcard
  164. type WildcardNode struct {
  165. NodeType
  166. }
  167. func newWildcard() *WildcardNode {
  168. return &WildcardNode{NodeType: NodeWildcard}
  169. }
  170. func (i *WildcardNode) String() string {
  171. return fmt.Sprintf("%s", i.Type())
  172. }
  173. // RecursiveNode means a recursive descent operator
  174. type RecursiveNode struct {
  175. NodeType
  176. }
  177. func newRecursive() *RecursiveNode {
  178. return &RecursiveNode{NodeType: NodeRecursive}
  179. }
  180. func (r *RecursiveNode) String() string {
  181. return fmt.Sprintf("%s", r.Type())
  182. }
  183. // UnionNode is union of ListNode
  184. type UnionNode struct {
  185. NodeType
  186. Nodes []*ListNode
  187. }
  188. func newUnion(nodes []*ListNode) *UnionNode {
  189. return &UnionNode{NodeType: NodeUnion, Nodes: nodes}
  190. }
  191. func (u *UnionNode) String() string {
  192. return fmt.Sprintf("%s", u.Type())
  193. }