ops.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package ast
  2. // FilterOp is an enum that represents operations that can be performed
  3. // when filtering (equality, inequality, etc.)
  4. type FilterOp string
  5. // If you add a FilterOp, MAKE SURE TO UPDATE ALL FILTER IMPLEMENTATIONS! Go
  6. // does not enforce exhaustive pattern matching on "enum" types.
  7. const (
  8. // FilterOpEquals is the equality operator
  9. //
  10. // "kube-system" FilterOpEquals "kube-system" = true
  11. // "kube-syste" FilterOpEquals "kube-system" = false
  12. FilterOpEquals FilterOp = "equals"
  13. // FilterOpNotEquals is the inverse of equals.
  14. FilterOpNotEquals = "notequals"
  15. // FilterOpContains supports string fields, slice fields, and map fields.
  16. // For maps, this is equivalent to map.HasKey(x)
  17. //
  18. // "kube-system" FilterOpContains "e-s" = true
  19. // ["a", "b", "c"] FilterOpContains "a" = true
  20. // { "namespace": "kubecost", "cluster": "cluster-one" } FilterOpContains "namespace" = true
  21. FilterOpContains = "contains"
  22. // FilterOpNotContains is the inverse of contains.
  23. FilterOpNotContains = "notcontains"
  24. // FilterOpContainsPrefix is like FilterOpContains, but checks against the start of a string.
  25. // For maps, this checks to see if any of the keys start with the prefix
  26. //
  27. // "kube-system" ContainsPrefix "kube" = true
  28. // ["kube-system", "abc123"] ContainsPrefix "kube" = true
  29. // { "kube-label": "test", "abc": "123" } ContainsPrefix "ab" = true
  30. FilterOpContainsPrefix = "containsprefix"
  31. // FilterOpNotContainsPrefix is the inverse of FilterOpContainsPrefix
  32. FilterOpNotContainsPrefix = "notcontainsprefix"
  33. // FilterOpContainsSuffix is like FilterOpContains, but checks against the end of a string.
  34. // For maps, this checks to see if any of the keys end with the suffix
  35. //
  36. // "kube-system" ContainsSuffix "system" = true
  37. // ["kube-system", "abc123"] ContainsSuffix "system" = true
  38. // { "kube-label": "test", "abc": "123" } ContainsSuffix "-label" = true
  39. FilterOpContainsSuffix = "containssuffix"
  40. // FilterOpNotContainsSuffix is the inverse of FilterOpContainsSuffix
  41. FilterOpNotContainsSuffix = "notcontainssuffix"
  42. // FilterOpVoid is base-depth operator that is used for an empty filter
  43. FilterOpVoid = "void"
  44. // FilterOpContradiction is a base-depth operator that filters all data.
  45. FilterOpContradiction = "contradiction"
  46. // FilterOpAnd is an operator that succeeds if all parameters succeed.
  47. FilterOpAnd = "and"
  48. // FilterOpOr is an operator that succeeds if any parameter succeeds
  49. FilterOpOr = "or"
  50. // FilterOpNot is an operator that contains a single operand
  51. FilterOpNot = "not"
  52. )
  53. // VoidOp is base-depth operator that is used for an empty filter
  54. type VoidOp struct{}
  55. // Op returns the FilterOp enumeration value for the operator.
  56. func (_ *VoidOp) Op() FilterOp {
  57. return FilterOpVoid
  58. }
  59. // ContradictionOp is a base-depth operator that filters all data.
  60. type ContradictionOp struct{}
  61. // Op returns the FilterOp enumeration value for the operator.
  62. func (_ *ContradictionOp) Op() FilterOp {
  63. return FilterOpContradiction
  64. }
  65. // AndOp is a filter operation that contains a flat list of nodes which should all resolve
  66. // to true in order for the result to be true.
  67. type AndOp struct {
  68. Operands []FilterNode
  69. }
  70. // Op returns the FilterOp enumeration value for the operator.
  71. func (_ *AndOp) Op() FilterOp {
  72. return FilterOpAnd
  73. }
  74. // Add appends a filter node to the flat list of operands within the AND operator
  75. func (ao *AndOp) Add(node FilterNode) {
  76. ao.Operands = append(ao.Operands, node)
  77. }
  78. // OrOp is a filter operation that contains a flat list of nodes which at least one node
  79. // should resolve to true in order for the result to be true.
  80. type OrOp struct {
  81. Operands []FilterNode
  82. }
  83. // Op returns the FilterOp enumeration value for the operator.
  84. func (_ *OrOp) Op() FilterOp {
  85. return FilterOpOr
  86. }
  87. // Add appends a filter node to the flat list of operands within the OR operator
  88. func (oo *OrOp) Add(node FilterNode) {
  89. oo.Operands = append(oo.Operands, node)
  90. }
  91. // NotOp is a filter operation that logically inverts result of the child operand.
  92. type NotOp struct {
  93. Operand FilterNode
  94. }
  95. // Op returns the FilterOp enumeration value for the operator.
  96. func (_ *NotOp) Op() FilterOp {
  97. return FilterOpNot
  98. }
  99. // Add sets the not operand to the parameter
  100. func (no *NotOp) Add(node FilterNode) {
  101. no.Operand = node
  102. }
  103. // EqualOp is a filter operation that compares a resolvable identifier (Left) to a
  104. // string value (Right)
  105. type EqualOp struct {
  106. // Left contains a resolvable Identifier (property of an input type) which can be
  107. // used to compare against the Right value.
  108. Left Identifier
  109. // Right contains the value which we wish to compare the resolved identifier to.
  110. Right string
  111. }
  112. // Op returns the FilterOp enumeration value for the operator.
  113. func (_ *EqualOp) Op() FilterOp {
  114. return FilterOpEquals
  115. }
  116. // ContainsOp is a filter operation that checks to see if a resolvable identifier (Left) contains a
  117. // string value (Right)
  118. type ContainsOp struct {
  119. // Left contains a resolvable Identifier (property of an input type) which can be
  120. // used to query against using the Right value.
  121. Left Identifier
  122. // Right contains the value which we use to search the resolved Left identifier with.
  123. Right string
  124. }
  125. // Op returns the FilterOp enumeration value for the operator.
  126. func (_ *ContainsOp) Op() FilterOp {
  127. return FilterOpContains
  128. }
  129. // ContainsPrefixOp is a filter operation that checks to see if a resolvable identifier (Left) starts with a
  130. // string value (Right)
  131. type ContainsPrefixOp struct {
  132. // Left contains a resolvable Identifier (property of an input type) which can be
  133. // used to query against using the Right value.
  134. Left Identifier
  135. // Right contains the value which we use to search the resolved Left identifier with.
  136. Right string
  137. }
  138. // Op returns the FilterOp enumeration value for the operator.
  139. func (_ *ContainsPrefixOp) Op() FilterOp {
  140. return FilterOpContainsPrefix
  141. }
  142. // ContainsSuffixOp is a filter operation that checks to see if a resolvable identifier (Left) ends with a
  143. // string value (Right)
  144. type ContainsSuffixOp struct {
  145. // Left contains a resolvable Identifier (property of an input type) which can be
  146. // used to query against using the Right value.
  147. Left Identifier
  148. // Right contains the value which we use to search the resolved Left identifier with.
  149. Right string
  150. }
  151. // Op returns the FilterOp enumeration value for the operator.
  152. func (_ *ContainsSuffixOp) Op() FilterOp {
  153. return FilterOpContainsSuffix
  154. }
  155. func Not(fn FilterNode) FilterNode {
  156. return &NotOp{Operand: fn}
  157. }
  158. func IsVoid(fn FilterNode) bool {
  159. _, ok := fn.(*VoidOp)
  160. return ok
  161. }