ops.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // FilterOpAnd is an operator that succeeds if all parameters succeed.
  45. FilterOpAnd = "and"
  46. // FilterOpOr is an operator that succeeds if any parameter succeeds
  47. FilterOpOr = "or"
  48. // FilterOpNot is an operator that contains a single operand
  49. FilterOpNot = "not"
  50. )
  51. // VoidOp is base-depth operator that is used for an empty filter
  52. type VoidOp struct{}
  53. // Op returns the FilterOp enumeration value for the operator.
  54. func (_ *VoidOp) Op() FilterOp {
  55. return FilterOpVoid
  56. }
  57. // AndOp is a filter operation that contains a flat list of nodes which should all resolve
  58. // to true in order for the result to be true.
  59. type AndOp struct {
  60. Operands []FilterNode
  61. }
  62. // Op returns the FilterOp enumeration value for the operator.
  63. func (_ *AndOp) Op() FilterOp {
  64. return FilterOpAnd
  65. }
  66. // Add appends a filter node to the flat list of operands within the AND operator
  67. func (ao *AndOp) Add(node FilterNode) {
  68. ao.Operands = append(ao.Operands, node)
  69. }
  70. // OrOp is a filter operation that contains a flat list of nodes which at least one node
  71. // should resolve to true in order for the result to be true.
  72. type OrOp struct {
  73. Operands []FilterNode
  74. }
  75. // Op returns the FilterOp enumeration value for the operator.
  76. func (_ *OrOp) Op() FilterOp {
  77. return FilterOpOr
  78. }
  79. // Add appends a filter node to the flat list of operands within the OR operator
  80. func (oo *OrOp) Add(node FilterNode) {
  81. oo.Operands = append(oo.Operands, node)
  82. }
  83. // NotOp is a filter operation that logically inverts result of the child operand.
  84. type NotOp struct {
  85. Operand FilterNode
  86. }
  87. // Op returns the FilterOp enumeration value for the operator.
  88. func (_ *NotOp) Op() FilterOp {
  89. return FilterOpNot
  90. }
  91. // Add sets the not operand to the parameter
  92. func (no *NotOp) Add(node FilterNode) {
  93. no.Operand = node
  94. }
  95. // EqualOp is a filter operation that compares a resolvable identifier (Left) to a
  96. // string value (Right)
  97. type EqualOp struct {
  98. // Left contains a resolvable Identifier (property of an input type) which can be
  99. // used to compare against the Right value.
  100. Left Identifier
  101. // Right contains the value which we wish to compare the resolved identifier to.
  102. Right string
  103. }
  104. // Op returns the FilterOp enumeration value for the operator.
  105. func (_ *EqualOp) Op() FilterOp {
  106. return FilterOpEquals
  107. }
  108. // ContainsOp is a filter operation that checks to see if a resolvable identifier (Left) contains a
  109. // string value (Right)
  110. type ContainsOp struct {
  111. // Left contains a resolvable Identifier (property of an input type) which can be
  112. // used to query against using the Right value.
  113. Left Identifier
  114. // Right contains the value which we use to search the resolved Left identifier with.
  115. Right string
  116. }
  117. // Op returns the FilterOp enumeration value for the operator.
  118. func (_ *ContainsOp) Op() FilterOp {
  119. return FilterOpContains
  120. }
  121. // ContainsPrefixOp is a filter operation that checks to see if a resolvable identifier (Left) starts with a
  122. // string value (Right)
  123. type ContainsPrefixOp struct {
  124. // Left contains a resolvable Identifier (property of an input type) which can be
  125. // used to query against using the Right value.
  126. Left Identifier
  127. // Right contains the value which we use to search the resolved Left identifier with.
  128. Right string
  129. }
  130. // Op returns the FilterOp enumeration value for the operator.
  131. func (_ *ContainsPrefixOp) Op() FilterOp {
  132. return FilterOpContainsPrefix
  133. }
  134. // ContainsSuffixOp is a filter operation that checks to see if a resolvable identifier (Left) ends with a
  135. // string value (Right)
  136. type ContainsSuffixOp struct {
  137. // Left contains a resolvable Identifier (property of an input type) which can be
  138. // used to query against using the Right value.
  139. Left Identifier
  140. // Right contains the value which we use to search the resolved Left identifier with.
  141. Right string
  142. }
  143. // Op returns the FilterOp enumeration value for the operator.
  144. func (_ *ContainsSuffixOp) Op() FilterOp {
  145. return FilterOpContainsSuffix
  146. }