element.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/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.
  29. Key []value.Field
  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. // String presents the path element as a human-readable string.
  39. func (e PathElement) String() string {
  40. switch {
  41. case e.FieldName != nil:
  42. return "." + *e.FieldName
  43. case len(e.Key) > 0:
  44. strs := make([]string, len(e.Key))
  45. for i, k := range e.Key {
  46. strs[i] = fmt.Sprintf("%v=%v", k.Name, k.Value)
  47. }
  48. // The order must be canonical, since we use the string value
  49. // in a set structure.
  50. sort.Strings(strs)
  51. return "[" + strings.Join(strs, ",") + "]"
  52. case e.Value != nil:
  53. return fmt.Sprintf("[=%v]", e.Value)
  54. case e.Index != nil:
  55. return fmt.Sprintf("[%v]", *e.Index)
  56. default:
  57. return "{{invalid path element}}"
  58. }
  59. }
  60. // KeyByFields is a helper function which constructs a key for an associative
  61. // list type. `nameValues` must have an even number of entries, alternating
  62. // names (type must be string) with values (type must be value.Value). If these
  63. // conditions are not met, KeyByFields will panic--it's intended for static
  64. // construction and shouldn't have user-produced values passed to it.
  65. func KeyByFields(nameValues ...interface{}) []value.Field {
  66. if len(nameValues)%2 != 0 {
  67. panic("must have a value for every name")
  68. }
  69. out := []value.Field{}
  70. for i := 0; i < len(nameValues)-1; i += 2 {
  71. out = append(out, value.Field{
  72. Name: nameValues[i].(string),
  73. Value: nameValues[i+1].(value.Value),
  74. })
  75. }
  76. return out
  77. }
  78. // PathElementSet is a set of path elements.
  79. // TODO: serialize as a list.
  80. type PathElementSet struct {
  81. // The strange construction is because there's no way to test
  82. // PathElements for equality (it can't be used as a key for a map).
  83. members map[string]PathElement
  84. }
  85. // Insert adds pe to the set.
  86. func (s *PathElementSet) Insert(pe PathElement) {
  87. serialized := pe.String()
  88. if s.members == nil {
  89. s.members = map[string]PathElement{
  90. serialized: pe,
  91. }
  92. return
  93. }
  94. if _, ok := s.members[serialized]; !ok {
  95. s.members[serialized] = pe
  96. }
  97. }
  98. // Union returns a set containing elements that appear in either s or s2.
  99. func (s *PathElementSet) Union(s2 *PathElementSet) *PathElementSet {
  100. out := &PathElementSet{
  101. members: map[string]PathElement{},
  102. }
  103. for k, v := range s.members {
  104. out.members[k] = v
  105. }
  106. for k, v := range s2.members {
  107. out.members[k] = v
  108. }
  109. return out
  110. }
  111. // Intersection returns a set containing elements which appear in both s and s2.
  112. func (s *PathElementSet) Intersection(s2 *PathElementSet) *PathElementSet {
  113. out := &PathElementSet{
  114. members: map[string]PathElement{},
  115. }
  116. for k, v := range s.members {
  117. if _, ok := s2.members[k]; ok {
  118. out.members[k] = v
  119. }
  120. }
  121. return out
  122. }
  123. // Difference returns a set containing elements which appear in s but not in s2.
  124. func (s *PathElementSet) Difference(s2 *PathElementSet) *PathElementSet {
  125. out := &PathElementSet{
  126. members: map[string]PathElement{},
  127. }
  128. for k, v := range s.members {
  129. if _, ok := s2.members[k]; !ok {
  130. out.members[k] = v
  131. }
  132. }
  133. return out
  134. }
  135. // Size retuns the number of elements in the set.
  136. func (s *PathElementSet) Size() int { return len(s.members) }
  137. // Has returns true if pe is a member of the set.
  138. func (s *PathElementSet) Has(pe PathElement) bool {
  139. if s.members == nil {
  140. return false
  141. }
  142. _, ok := s.members[pe.String()]
  143. return ok
  144. }
  145. // Equals returns true if s and s2 have exactly the same members.
  146. func (s *PathElementSet) Equals(s2 *PathElementSet) bool {
  147. if len(s.members) != len(s2.members) {
  148. return false
  149. }
  150. for k := range s.members {
  151. if _, ok := s2.members[k]; !ok {
  152. return false
  153. }
  154. }
  155. return true
  156. }
  157. // Iterate calls f for each PathElement in the set.
  158. func (s *PathElementSet) Iterate(f func(PathElement)) {
  159. for _, pe := range s.members {
  160. f(pe)
  161. }
  162. }