fromvalue.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/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. do: func(p Path) { s.Insert(p) },
  24. }
  25. w.walk()
  26. return s
  27. }
  28. type objectWalker struct {
  29. path Path
  30. value value.Value
  31. do func(Path)
  32. }
  33. func (w *objectWalker) walk() {
  34. switch {
  35. case w.value.Null:
  36. case w.value.FloatValue != nil:
  37. case w.value.IntValue != nil:
  38. case w.value.StringValue != nil:
  39. case w.value.BooleanValue != nil:
  40. // All leaf fields handled the same way (after the switch
  41. // statement).
  42. // Descend
  43. case w.value.ListValue != nil:
  44. // If the list were atomic, we'd break here, but we don't have
  45. // a schema, so we can't tell.
  46. for i, child := range w.value.ListValue.Items {
  47. w2 := *w
  48. w2.path = append(w.path, GuessBestListPathElement(i, child))
  49. w2.value = child
  50. w2.walk()
  51. }
  52. return
  53. case w.value.MapValue != nil:
  54. // If the map/struct were atomic, we'd break here, but we don't
  55. // have a schema, so we can't tell.
  56. for i := range w.value.MapValue.Items {
  57. child := w.value.MapValue.Items[i]
  58. w2 := *w
  59. w2.path = append(w.path, PathElement{FieldName: &child.Name})
  60. w2.value = child.Value
  61. w2.walk()
  62. }
  63. return
  64. }
  65. // Leaf fields get added to the set.
  66. if len(w.path) > 0 {
  67. w.do(w.path)
  68. }
  69. }
  70. // AssociativeListCandidateFieldNames lists the field names which are
  71. // considered keys if found in a list element.
  72. var AssociativeListCandidateFieldNames = []string{
  73. "key",
  74. "id",
  75. "name",
  76. }
  77. // GuessBestListPathElement guesses whether item is an associative list
  78. // element, which should be referenced by key(s), or if it is not and therefore
  79. // referencing by index is acceptable. Currently this is done by checking
  80. // whether item has any of the fields listed in
  81. // AssociativeListCandidateFieldNames which have scalar values.
  82. func GuessBestListPathElement(index int, item value.Value) PathElement {
  83. if item.MapValue == nil {
  84. // Non map items could be parts of sets or regular "atomic"
  85. // lists. We won't try to guess whether something should be a
  86. // set or not.
  87. return PathElement{Index: &index}
  88. }
  89. var keys []value.Field
  90. for _, name := range AssociativeListCandidateFieldNames {
  91. f, ok := item.MapValue.Get(name)
  92. if !ok {
  93. continue
  94. }
  95. // only accept primitive/scalar types as keys.
  96. if f.Value.Null || f.Value.MapValue != nil || f.Value.ListValue != nil {
  97. continue
  98. }
  99. keys = append(keys, *f)
  100. }
  101. if len(keys) > 0 {
  102. return PathElement{Key: keys}
  103. }
  104. return PathElement{Index: &index}
  105. }