Skip to content

Commit ff40460

Browse files
authored
Merge pull request #4424 from koba1t/feature/allow_setting_every_array_element_in_replacements
Allow setting every array element in replacements
2 parents a7f4db7 + 5ed96a3 commit ff40460

File tree

5 files changed

+342
-32
lines changed

5 files changed

+342
-32
lines changed

api/filters/replacement/replacement.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,41 @@ func applyToNode(node *yaml.RNode, value *yaml.RNode, target *types.TargetSelect
119119
if target.Options != nil && target.Options.Create {
120120
t, err = node.Pipe(yaml.LookupCreate(value.YNode().Kind, fieldPath...))
121121
} else {
122-
t, err = node.Pipe(yaml.Lookup(fieldPath...))
122+
t, err = node.Pipe(&yaml.PathMatcher{Path: fieldPath})
123123
}
124124
if err != nil {
125125
return err
126126
}
127127
if t != nil {
128-
if err = setTargetValue(target.Options, t, value); err != nil {
128+
if err = applyToOneNode(target.Options, t, value); err != nil {
129129
return err
130130
}
131131
}
132132
}
133133
return nil
134134
}
135135

136+
func applyToOneNode(options *types.FieldOptions, t *yaml.RNode, value *yaml.RNode) error {
137+
if len(t.YNode().Content) == 0 {
138+
if err := setTargetValue(options, t, value); err != nil {
139+
return err
140+
}
141+
return nil
142+
}
143+
144+
for _, scalarNode := range t.YNode().Content {
145+
if options != nil && options.Create {
146+
return fmt.Errorf("cannot use create option in a multi-value target")
147+
}
148+
rn := yaml.NewRNode(scalarNode)
149+
if err := setTargetValue(options, rn, value); err != nil {
150+
return err
151+
}
152+
}
153+
154+
return nil
155+
}
156+
136157
func setTargetValue(options *types.FieldOptions, t *yaml.RNode, value *yaml.RNode) error {
137158
value = value.Copy()
138159
if options != nil && options.Delimiter != "" {
@@ -152,7 +173,9 @@ func setTargetValue(options *types.FieldOptions, t *yaml.RNode, value *yaml.RNod
152173
}
153174
value.YNode().Value = strings.Join(tv, options.Delimiter)
154175
}
176+
155177
t.SetYNode(value.YNode())
178+
156179
return nil
157180
}
158181

0 commit comments

Comments
 (0)