fromvalue.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "sigs.k8s.io/structured-merge-diff/v4/value"
  16. )
  17. // SetFromValue creates a set containing every leaf field mentioned in v.
  18. func SetFromValue(v value.Value) *Set {
  19. s := NewSet()
  20. w := objectWalker{
  21. path: Path{},
  22. value: v,
  23. allocator: value.NewFreelistAllocator(),
  24. do: func(p Path) { s.Insert(p) },
  25. }
  26. w.walk()
  27. return s
  28. }
  29. type objectWalker struct {
  30. path Path
  31. value value.Value
  32. allocator value.Allocator
  33. do func(Path)
  34. }
  35. func (w *objectWalker) walk() {
  36. switch {
  37. case w.value.IsNull():
  38. case w.value.IsFloat():
  39. case w.value.IsInt():
  40. case w.value.IsString():
  41. case w.value.IsBool():
  42. // All leaf fields handled the same way (after the switch
  43. // statement).
  44. // Descend
  45. case w.value.IsList():
  46. // If the list were atomic, we'd break here, but we don't have
  47. // a schema, so we can't tell.
  48. l := w.value.AsListUsing(w.allocator)
  49. defer w.allocator.Free(l)
  50. iter := l.RangeUsing(w.allocator)
  51. defer w.allocator.Free(iter)
  52. for iter.Next() {
  53. i, value := iter.Item()
  54. w2 := *w
  55. w2.path = append(w.path, w.GuessBestListPathElement(i, value))
  56. w2.value = value
  57. w2.walk()
  58. }
  59. return
  60. case w.value.IsMap():
  61. // If the map/struct were atomic, we'd break here, but we don't
  62. // have a schema, so we can't tell.
  63. m := w.value.AsMapUsing(w.allocator)
  64. defer w.allocator.Free(m)
  65. m.IterateUsing(w.allocator, func(k string, val value.Value) bool {
  66. w2 := *w
  67. w2.path = append(w.path, PathElement{FieldName: &k})
  68. w2.value = val
  69. w2.walk()
  70. return true
  71. })
  72. return
  73. }
  74. // Leaf fields get added to the set.
  75. if len(w.path) > 0 {
  76. w.do(w.path)
  77. }
  78. }
  79. // AssociativeListCandidateFieldNames lists the field names which are
  80. // considered keys if found in a list element.
  81. var AssociativeListCandidateFieldNames = []string{
  82. "key",
  83. "id",
  84. "name",
  85. }
  86. // GuessBestListPathElement guesses whether item is an associative list
  87. // element, which should be referenced by key(s), or if it is not and therefore
  88. // referencing by index is acceptable. Currently this is done by checking
  89. // whether item has any of the fields listed in
  90. // AssociativeListCandidateFieldNames which have scalar values.
  91. func (w *objectWalker) GuessBestListPathElement(index int, item value.Value) PathElement {
  92. if !item.IsMap() {
  93. // Non map items could be parts of sets or regular "atomic"
  94. // lists. We won't try to guess whether something should be a
  95. // set or not.
  96. return PathElement{Index: &index}
  97. }
  98. m := item.AsMapUsing(w.allocator)
  99. defer w.allocator.Free(m)
  100. var keys value.FieldList
  101. for _, name := range AssociativeListCandidateFieldNames {
  102. f, ok := m.Get(name)
  103. if !ok {
  104. continue
  105. }
  106. // only accept primitive/scalar types as keys.
  107. if f.IsNull() || f.IsMap() || f.IsList() {
  108. continue
  109. }
  110. keys = append(keys, value.Field{Name: name, Value: f})
  111. }
  112. if len(keys) > 0 {
  113. keys.Sort()
  114. return PathElement{Key: &keys}
  115. }
  116. return PathElement{Index: &index}
  117. }