k3s/vendor/sigs.k8s.io/structured-merge-diff/v3/fieldpath/fromvalue.go

135 lines
3.4 KiB
Go
Raw Normal View History

2019-08-30 18:33:25 +00:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fieldpath
import (
2020-03-26 21:07:15 +00:00
"sigs.k8s.io/structured-merge-diff/v3/value"
2019-08-30 18:33:25 +00:00
)
// SetFromValue creates a set containing every leaf field mentioned in v.
func SetFromValue(v value.Value) *Set {
s := NewSet()
w := objectWalker{
2020-03-26 21:07:15 +00:00
path: Path{},
value: v,
allocator: value.NewFreelistAllocator(),
do: func(p Path) { s.Insert(p) },
2019-08-30 18:33:25 +00:00
}
w.walk()
return s
}
type objectWalker struct {
2020-03-26 21:07:15 +00:00
path Path
value value.Value
allocator value.Allocator
2019-08-30 18:33:25 +00:00
do func(Path)
}
func (w *objectWalker) walk() {
switch {
2020-03-26 21:07:15 +00:00
case w.value.IsNull():
case w.value.IsFloat():
case w.value.IsInt():
case w.value.IsString():
case w.value.IsBool():
2019-08-30 18:33:25 +00:00
// All leaf fields handled the same way (after the switch
// statement).
// Descend
2020-03-26 21:07:15 +00:00
case w.value.IsList():
2019-08-30 18:33:25 +00:00
// If the list were atomic, we'd break here, but we don't have
// a schema, so we can't tell.
2020-03-26 21:07:15 +00:00
l := w.value.AsListUsing(w.allocator)
defer w.allocator.Free(l)
iter := l.RangeUsing(w.allocator)
defer w.allocator.Free(iter)
for iter.Next() {
i, value := iter.Item()
2019-08-30 18:33:25 +00:00
w2 := *w
2020-03-26 21:07:15 +00:00
w2.path = append(w.path, w.GuessBestListPathElement(i, value))
w2.value = value
2019-08-30 18:33:25 +00:00
w2.walk()
}
return
2020-03-26 21:07:15 +00:00
case w.value.IsMap():
2019-08-30 18:33:25 +00:00
// If the map/struct were atomic, we'd break here, but we don't
// have a schema, so we can't tell.
2020-03-26 21:07:15 +00:00
m := w.value.AsMapUsing(w.allocator)
defer w.allocator.Free(m)
m.IterateUsing(w.allocator, func(k string, val value.Value) bool {
2019-08-30 18:33:25 +00:00
w2 := *w
2020-03-26 21:07:15 +00:00
w2.path = append(w.path, PathElement{FieldName: &k})
w2.value = val
2019-08-30 18:33:25 +00:00
w2.walk()
2020-03-26 21:07:15 +00:00
return true
})
2019-08-30 18:33:25 +00:00
return
}
// Leaf fields get added to the set.
if len(w.path) > 0 {
w.do(w.path)
}
}
// AssociativeListCandidateFieldNames lists the field names which are
// considered keys if found in a list element.
var AssociativeListCandidateFieldNames = []string{
"key",
"id",
"name",
}
// GuessBestListPathElement guesses whether item is an associative list
// element, which should be referenced by key(s), or if it is not and therefore
// referencing by index is acceptable. Currently this is done by checking
// whether item has any of the fields listed in
// AssociativeListCandidateFieldNames which have scalar values.
2020-03-26 21:07:15 +00:00
func (w *objectWalker) GuessBestListPathElement(index int, item value.Value) PathElement {
if !item.IsMap() {
2019-08-30 18:33:25 +00:00
// Non map items could be parts of sets or regular "atomic"
// lists. We won't try to guess whether something should be a
// set or not.
return PathElement{Index: &index}
}
2020-03-26 21:07:15 +00:00
m := item.AsMapUsing(w.allocator)
defer w.allocator.Free(m)
2019-12-12 01:27:03 +00:00
var keys value.FieldList
2019-08-30 18:33:25 +00:00
for _, name := range AssociativeListCandidateFieldNames {
2020-03-26 21:07:15 +00:00
f, ok := m.Get(name)
2019-08-30 18:33:25 +00:00
if !ok {
continue
}
// only accept primitive/scalar types as keys.
2020-03-26 21:07:15 +00:00
if f.IsNull() || f.IsMap() || f.IsList() {
2019-08-30 18:33:25 +00:00
continue
}
2020-03-26 21:07:15 +00:00
keys = append(keys, value.Field{Name: name, Value: f})
2019-08-30 18:33:25 +00:00
}
if len(keys) > 0 {
2019-12-12 01:27:03 +00:00
keys.Sort()
return PathElement{Key: &keys}
2019-08-30 18:33:25 +00:00
}
return PathElement{Index: &index}
}