element.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. Copyright 2018 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 fieldpath
  14. import (
  15. "fmt"
  16. "sort"
  17. "strings"
  18. "sigs.k8s.io/structured-merge-diff/v4/value"
  19. )
  20. // PathElement describes how to select a child field given a containing object.
  21. type PathElement struct {
  22. // Exactly one of the following fields should be non-nil.
  23. // FieldName selects a single field from a map (reminder: this is also
  24. // how structs are represented). The containing object must be a map.
  25. FieldName *string
  26. // Key selects the list element which has fields matching those given.
  27. // The containing object must be an associative list with map typed
  28. // elements. They are sorted alphabetically.
  29. Key *value.FieldList
  30. // Value selects the list element with the given value. The containing
  31. // object must be an associative list with a primitive typed element
  32. // (i.e., a set).
  33. Value *value.Value
  34. // Index selects a list element by its index number. The containing
  35. // object must be an atomic list.
  36. Index *int
  37. }
  38. // Less provides an order for path elements.
  39. func (e PathElement) Less(rhs PathElement) bool {
  40. return e.Compare(rhs) < 0
  41. }
  42. // Compare provides an order for path elements.
  43. func (e PathElement) Compare(rhs PathElement) int {
  44. if e.FieldName != nil {
  45. if rhs.FieldName == nil {
  46. return -1
  47. }
  48. return strings.Compare(*e.FieldName, *rhs.FieldName)
  49. } else if rhs.FieldName != nil {
  50. return 1
  51. }
  52. if e.Key != nil {
  53. if rhs.Key == nil {
  54. return -1
  55. }
  56. return e.Key.Compare(*rhs.Key)
  57. } else if rhs.Key != nil {
  58. return 1
  59. }
  60. if e.Value != nil {
  61. if rhs.Value == nil {
  62. return -1
  63. }
  64. return value.Compare(*e.Value, *rhs.Value)
  65. } else if rhs.Value != nil {
  66. return 1
  67. }
  68. if e.Index != nil {
  69. if rhs.Index == nil {
  70. return -1
  71. }
  72. if *e.Index < *rhs.Index {
  73. return -1
  74. } else if *e.Index == *rhs.Index {
  75. return 0
  76. }
  77. return 1
  78. } else if rhs.Index != nil {
  79. return 1
  80. }
  81. return 0
  82. }
  83. // Equals returns true if both path elements are equal.
  84. func (e PathElement) Equals(rhs PathElement) bool {
  85. if e.FieldName != nil {
  86. if rhs.FieldName == nil {
  87. return false
  88. }
  89. return *e.FieldName == *rhs.FieldName
  90. } else if rhs.FieldName != nil {
  91. return false
  92. }
  93. if e.Key != nil {
  94. if rhs.Key == nil {
  95. return false
  96. }
  97. return e.Key.Equals(*rhs.Key)
  98. } else if rhs.Key != nil {
  99. return false
  100. }
  101. if e.Value != nil {
  102. if rhs.Value == nil {
  103. return false
  104. }
  105. return value.Equals(*e.Value, *rhs.Value)
  106. } else if rhs.Value != nil {
  107. return false
  108. }
  109. if e.Index != nil {
  110. if rhs.Index == nil {
  111. return false
  112. }
  113. return *e.Index == *rhs.Index
  114. } else if rhs.Index != nil {
  115. return false
  116. }
  117. return true
  118. }
  119. // String presents the path element as a human-readable string.
  120. func (e PathElement) String() string {
  121. switch {
  122. case e.FieldName != nil:
  123. return "." + *e.FieldName
  124. case e.Key != nil:
  125. strs := make([]string, len(*e.Key))
  126. for i, k := range *e.Key {
  127. strs[i] = fmt.Sprintf("%v=%v", k.Name, value.ToString(k.Value))
  128. }
  129. // Keys are supposed to be sorted.
  130. return "[" + strings.Join(strs, ",") + "]"
  131. case e.Value != nil:
  132. return fmt.Sprintf("[=%v]", value.ToString(*e.Value))
  133. case e.Index != nil:
  134. return fmt.Sprintf("[%v]", *e.Index)
  135. default:
  136. return "{{invalid path element}}"
  137. }
  138. }
  139. // KeyByFields is a helper function which constructs a key for an associative
  140. // list type. `nameValues` must have an even number of entries, alternating
  141. // names (type must be string) with values (type must be value.Value). If these
  142. // conditions are not met, KeyByFields will panic--it's intended for static
  143. // construction and shouldn't have user-produced values passed to it.
  144. func KeyByFields(nameValues ...interface{}) *value.FieldList {
  145. if len(nameValues)%2 != 0 {
  146. panic("must have a value for every name")
  147. }
  148. out := value.FieldList{}
  149. for i := 0; i < len(nameValues)-1; i += 2 {
  150. out = append(out, value.Field{Name: nameValues[i].(string), Value: value.NewValueInterface(nameValues[i+1])})
  151. }
  152. out.Sort()
  153. return &out
  154. }
  155. // PathElementSet is a set of path elements.
  156. // TODO: serialize as a list.
  157. type PathElementSet struct {
  158. members sortedPathElements
  159. }
  160. func MakePathElementSet(size int) PathElementSet {
  161. return PathElementSet{
  162. members: make(sortedPathElements, 0, size),
  163. }
  164. }
  165. type sortedPathElements []PathElement
  166. // Implement the sort interface; this would permit bulk creation, which would
  167. // be faster than doing it one at a time via Insert.
  168. func (spe sortedPathElements) Len() int { return len(spe) }
  169. func (spe sortedPathElements) Less(i, j int) bool { return spe[i].Less(spe[j]) }
  170. func (spe sortedPathElements) Swap(i, j int) { spe[i], spe[j] = spe[j], spe[i] }
  171. // Insert adds pe to the set.
  172. func (s *PathElementSet) Insert(pe PathElement) {
  173. loc := sort.Search(len(s.members), func(i int) bool {
  174. return !s.members[i].Less(pe)
  175. })
  176. if loc == len(s.members) {
  177. s.members = append(s.members, pe)
  178. return
  179. }
  180. if s.members[loc].Equals(pe) {
  181. return
  182. }
  183. s.members = append(s.members, PathElement{})
  184. copy(s.members[loc+1:], s.members[loc:])
  185. s.members[loc] = pe
  186. }
  187. // Union returns a set containing elements that appear in either s or s2.
  188. func (s *PathElementSet) Union(s2 *PathElementSet) *PathElementSet {
  189. out := &PathElementSet{}
  190. i, j := 0, 0
  191. for i < len(s.members) && j < len(s2.members) {
  192. if s.members[i].Less(s2.members[j]) {
  193. out.members = append(out.members, s.members[i])
  194. i++
  195. } else {
  196. out.members = append(out.members, s2.members[j])
  197. if !s2.members[j].Less(s.members[i]) {
  198. i++
  199. }
  200. j++
  201. }
  202. }
  203. if i < len(s.members) {
  204. out.members = append(out.members, s.members[i:]...)
  205. }
  206. if j < len(s2.members) {
  207. out.members = append(out.members, s2.members[j:]...)
  208. }
  209. return out
  210. }
  211. // Intersection returns a set containing elements which appear in both s and s2.
  212. func (s *PathElementSet) Intersection(s2 *PathElementSet) *PathElementSet {
  213. out := &PathElementSet{}
  214. i, j := 0, 0
  215. for i < len(s.members) && j < len(s2.members) {
  216. if s.members[i].Less(s2.members[j]) {
  217. i++
  218. } else {
  219. if !s2.members[j].Less(s.members[i]) {
  220. out.members = append(out.members, s.members[i])
  221. i++
  222. }
  223. j++
  224. }
  225. }
  226. return out
  227. }
  228. // Difference returns a set containing elements which appear in s but not in s2.
  229. func (s *PathElementSet) Difference(s2 *PathElementSet) *PathElementSet {
  230. out := &PathElementSet{}
  231. i, j := 0, 0
  232. for i < len(s.members) && j < len(s2.members) {
  233. if s.members[i].Less(s2.members[j]) {
  234. out.members = append(out.members, s.members[i])
  235. i++
  236. } else {
  237. if !s2.members[j].Less(s.members[i]) {
  238. i++
  239. }
  240. j++
  241. }
  242. }
  243. if i < len(s.members) {
  244. out.members = append(out.members, s.members[i:]...)
  245. }
  246. return out
  247. }
  248. // Size retuns the number of elements in the set.
  249. func (s *PathElementSet) Size() int { return len(s.members) }
  250. // Has returns true if pe is a member of the set.
  251. func (s *PathElementSet) Has(pe PathElement) bool {
  252. loc := sort.Search(len(s.members), func(i int) bool {
  253. return !s.members[i].Less(pe)
  254. })
  255. if loc == len(s.members) {
  256. return false
  257. }
  258. if s.members[loc].Equals(pe) {
  259. return true
  260. }
  261. return false
  262. }
  263. // Equals returns true if s and s2 have exactly the same members.
  264. func (s *PathElementSet) Equals(s2 *PathElementSet) bool {
  265. if len(s.members) != len(s2.members) {
  266. return false
  267. }
  268. for k := range s.members {
  269. if !s.members[k].Equals(s2.members[k]) {
  270. return false
  271. }
  272. }
  273. return true
  274. }
  275. // Iterate calls f for each PathElement in the set. The order is deterministic.
  276. func (s *PathElementSet) Iterate(f func(PathElement)) {
  277. for _, pe := range s.members {
  278. f(pe)
  279. }
  280. }