mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Walk: opt-in to visiting map keys when walking map entries
This commit is contained in:
@@ -40,6 +40,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) {
|
|||||||
}
|
}
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
val, err := Walker{
|
val, err := Walker{
|
||||||
|
VisitKeysAsScalars: l.VisitKeysAsScalars,
|
||||||
InferAssociativeLists: l.InferAssociativeLists,
|
InferAssociativeLists: l.InferAssociativeLists,
|
||||||
Visitor: l,
|
Visitor: l,
|
||||||
Schema: s,
|
Schema: s,
|
||||||
|
|||||||
@@ -27,6 +27,32 @@ func (l Walker) walkMap() (*yaml.RNode, error) {
|
|||||||
|
|
||||||
// recursively set the field values on the map
|
// recursively set the field values on the map
|
||||||
for _, key := range l.fieldNames() {
|
for _, key := range l.fieldNames() {
|
||||||
|
if l.VisitKeysAsScalars {
|
||||||
|
// visit the map keys as if they were scalars,
|
||||||
|
// this is necessary if doing things such as copying
|
||||||
|
// comments
|
||||||
|
var keys []*yaml.RNode
|
||||||
|
for i := range l.Sources {
|
||||||
|
// construct the sources from the keys
|
||||||
|
if l.Sources[i] == nil {
|
||||||
|
keys = append(keys, nil)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
field := l.Sources[i].Field(key)
|
||||||
|
if field == nil || yaml.IsEmpty(field.Key) {
|
||||||
|
keys = append(keys, nil)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keys = append(keys, field.Key)
|
||||||
|
}
|
||||||
|
// visit the sources as a scalar
|
||||||
|
// keys don't have any schema --pass in nil
|
||||||
|
_, err := l.Visitor.VisitScalar(keys, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var s *openapi.ResourceSchema
|
var s *openapi.ResourceSchema
|
||||||
if l.Schema != nil {
|
if l.Schema != nil {
|
||||||
s = l.Schema.Field(key)
|
s = l.Schema.Field(key)
|
||||||
@@ -36,6 +62,7 @@ func (l Walker) walkMap() (*yaml.RNode, error) {
|
|||||||
s = commentSch
|
s = commentSch
|
||||||
}
|
}
|
||||||
val, err := Walker{
|
val, err := Walker{
|
||||||
|
VisitKeysAsScalars: l.VisitKeysAsScalars,
|
||||||
InferAssociativeLists: l.InferAssociativeLists,
|
InferAssociativeLists: l.InferAssociativeLists,
|
||||||
Visitor: l,
|
Visitor: l,
|
||||||
Schema: s,
|
Schema: s,
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ type Walker struct {
|
|||||||
// fields which it doesn't have the schema based on the fields in the
|
// fields which it doesn't have the schema based on the fields in the
|
||||||
// list elements.
|
// list elements.
|
||||||
InferAssociativeLists bool
|
InferAssociativeLists bool
|
||||||
|
|
||||||
|
// VisitKeysAsScalars if true will call VisitScalar on map entry keys,
|
||||||
|
// providing nil as the OpenAPI schema.
|
||||||
|
VisitKeysAsScalars bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Walker) Kind() yaml.Kind {
|
func (l Walker) Kind() yaml.Kind {
|
||||||
|
|||||||
Reference in New Issue
Block a user