github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/jsonpath/node.go (about)

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