Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / util / jsonpath / node.go
1 /*
2 Copyright 2015 The Kubernetes Authors.
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
17 package jsonpath
18
19 import "fmt"
20
21 // NodeType identifies the type of a parse tree node.
22 type NodeType int
23
24 // Type returns itself and provides an easy default implementation
25 func (t NodeType) Type() NodeType {
26         return t
27 }
28
29 func (t NodeType) String() string {
30         return NodeTypeName[t]
31 }
32
33 const (
34         NodeText NodeType = iota
35         NodeArray
36         NodeList
37         NodeField
38         NodeIdentifier
39         NodeFilter
40         NodeInt
41         NodeFloat
42         NodeWildcard
43         NodeRecursive
44         NodeUnion
45         NodeBool
46 )
47
48 var NodeTypeName = map[NodeType]string{
49         NodeText:       "NodeText",
50         NodeArray:      "NodeArray",
51         NodeList:       "NodeList",
52         NodeField:      "NodeField",
53         NodeIdentifier: "NodeIdentifier",
54         NodeFilter:     "NodeFilter",
55         NodeInt:        "NodeInt",
56         NodeFloat:      "NodeFloat",
57         NodeWildcard:   "NodeWildcard",
58         NodeRecursive:  "NodeRecursive",
59         NodeUnion:      "NodeUnion",
60         NodeBool:       "NodeBool",
61 }
62
63 type Node interface {
64         Type() NodeType
65         String() string
66 }
67
68 // ListNode holds a sequence of nodes.
69 type ListNode struct {
70         NodeType
71         Nodes []Node // The element nodes in lexical order.
72 }
73
74 func newList() *ListNode {
75         return &ListNode{NodeType: NodeList}
76 }
77
78 func (l *ListNode) append(n Node) {
79         l.Nodes = append(l.Nodes, n)
80 }
81
82 func (l *ListNode) String() string {
83         return l.Type().String()
84 }
85
86 // TextNode holds plain text.
87 type TextNode struct {
88         NodeType
89         Text string // The text; may span newlines.
90 }
91
92 func newText(text string) *TextNode {
93         return &TextNode{NodeType: NodeText, Text: text}
94 }
95
96 func (t *TextNode) String() string {
97         return fmt.Sprintf("%s: %s", t.Type(), t.Text)
98 }
99
100 // FieldNode holds field of struct
101 type FieldNode struct {
102         NodeType
103         Value string
104 }
105
106 func newField(value string) *FieldNode {
107         return &FieldNode{NodeType: NodeField, Value: value}
108 }
109
110 func (f *FieldNode) String() string {
111         return fmt.Sprintf("%s: %s", f.Type(), f.Value)
112 }
113
114 // IdentifierNode holds an identifier
115 type IdentifierNode struct {
116         NodeType
117         Name string
118 }
119
120 func newIdentifier(value string) *IdentifierNode {
121         return &IdentifierNode{
122                 NodeType: NodeIdentifier,
123                 Name:     value,
124         }
125 }
126
127 func (f *IdentifierNode) String() string {
128         return fmt.Sprintf("%s: %s", f.Type(), f.Name)
129 }
130
131 // ParamsEntry holds param information for ArrayNode
132 type ParamsEntry struct {
133         Value int
134         Known bool // whether the value is known when parse it
135 }
136
137 // ArrayNode holds start, end, step information for array index selection
138 type ArrayNode struct {
139         NodeType
140         Params [3]ParamsEntry // start, end, step
141 }
142
143 func newArray(params [3]ParamsEntry) *ArrayNode {
144         return &ArrayNode{
145                 NodeType: NodeArray,
146                 Params:   params,
147         }
148 }
149
150 func (a *ArrayNode) String() string {
151         return fmt.Sprintf("%s: %v", a.Type(), a.Params)
152 }
153
154 // FilterNode holds operand and operator information for filter
155 type FilterNode struct {
156         NodeType
157         Left     *ListNode
158         Right    *ListNode
159         Operator string
160 }
161
162 func newFilter(left, right *ListNode, operator string) *FilterNode {
163         return &FilterNode{
164                 NodeType: NodeFilter,
165                 Left:     left,
166                 Right:    right,
167                 Operator: operator,
168         }
169 }
170
171 func (f *FilterNode) String() string {
172         return fmt.Sprintf("%s: %s %s %s", f.Type(), f.Left, f.Operator, f.Right)
173 }
174
175 // IntNode holds integer value
176 type IntNode struct {
177         NodeType
178         Value int
179 }
180
181 func newInt(num int) *IntNode {
182         return &IntNode{NodeType: NodeInt, Value: num}
183 }
184
185 func (i *IntNode) String() string {
186         return fmt.Sprintf("%s: %d", i.Type(), i.Value)
187 }
188
189 // FloatNode holds float value
190 type FloatNode struct {
191         NodeType
192         Value float64
193 }
194
195 func newFloat(num float64) *FloatNode {
196         return &FloatNode{NodeType: NodeFloat, Value: num}
197 }
198
199 func (i *FloatNode) String() string {
200         return fmt.Sprintf("%s: %f", i.Type(), i.Value)
201 }
202
203 // WildcardNode means a wildcard
204 type WildcardNode struct {
205         NodeType
206 }
207
208 func newWildcard() *WildcardNode {
209         return &WildcardNode{NodeType: NodeWildcard}
210 }
211
212 func (i *WildcardNode) String() string {
213         return i.Type().String()
214 }
215
216 // RecursiveNode means a recursive descent operator
217 type RecursiveNode struct {
218         NodeType
219 }
220
221 func newRecursive() *RecursiveNode {
222         return &RecursiveNode{NodeType: NodeRecursive}
223 }
224
225 func (r *RecursiveNode) String() string {
226         return r.Type().String()
227 }
228
229 // UnionNode is union of ListNode
230 type UnionNode struct {
231         NodeType
232         Nodes []*ListNode
233 }
234
235 func newUnion(nodes []*ListNode) *UnionNode {
236         return &UnionNode{NodeType: NodeUnion, Nodes: nodes}
237 }
238
239 func (u *UnionNode) String() string {
240         return u.Type().String()
241 }
242
243 // BoolNode holds bool value
244 type BoolNode struct {
245         NodeType
246         Value bool
247 }
248
249 func newBool(value bool) *BoolNode {
250         return &BoolNode{NodeType: NodeBool, Value: value}
251 }
252
253 func (b *BoolNode) String() string {
254         return fmt.Sprintf("%s: %t", b.Type(), b.Value)
255 }