fix errror message using replacement wildcard and create option

This commit is contained in:
koba1t
2022-04-12 05:14:24 +09:00
parent e5041bae6f
commit 5e84de2a89
4 changed files with 51 additions and 5 deletions

View File

@@ -142,9 +142,6 @@ func applyToOneNode(options *types.FieldOptions, t *yaml.RNode, value *yaml.RNod
}
for _, scalarNode := range t.YNode().Content {
if options != nil && options.Create {
return fmt.Errorf("cannot use create option in a multi-value target")
}
rn := yaml.NewRNode(scalarNode)
if err := setTargetValue(options, rn, value); err != nil {
return err

View File

@@ -1634,6 +1634,45 @@ spec:
- name: deployment-name
value: sample-deploy`,
},
"index contains '*' character and create options": {
input: `apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: sample-deploy
name: sample-deploy
spec:
replicas: 1
selector:
matchLabels:
app: sample-deploy
template:
metadata:
labels:
app: sample-deploy
spec:
containers:
- image: nginx
name: main
env:
- name: other-env
value: YYYYY
`,
replacements: `replacements:
- source:
kind: Deployment
name: sample-deploy
fieldPath: metadata.name
targets:
- select:
kind: Deployment
fieldPaths:
- spec.template.spec.containers.*.env.[name=deployment-name].value
options:
create: true
`,
expectedErr: "cannot support create option in a multi-value target now",
},
"multiple field paths in target": {
input: `apiVersion: v1
kind: ConfigMap

View File

@@ -541,6 +541,9 @@ func (l PathGetter) getFilter(part, nextPart string, fieldPath *[]string) (Filte
case part == "-":
// part is a hyphen
return GetElementByIndex(-1), nil
case part == "*":
// part is a hyphen
return nil, fmt.Errorf("cannot support create option in a multi-value target now")
case IsListIndex(part):
// part is surrounded by brackets
return l.elemFilter(part)

View File

@@ -142,7 +142,7 @@ func TestElementSetter(t *testing.T) {
node = MustParse(`
- a: b
- c: d
- c: d
`)
// If given a key and no values, ElementSetter will
// change node to be an empty list
@@ -154,7 +154,7 @@ func TestElementSetter(t *testing.T) {
node = MustParse(`
- a: b
- c: d
- c: d
`)
// Return error because ElementSetter will assume all elements are scalar when
// there is only value provided.
@@ -580,6 +580,13 @@ a: {}
assert.Equal(t, "h\n", assertNoErrorString(t)(rn.String()))
}
func TestLookup_Fn_create_with_wildcard_error(t *testing.T) {
node, err := Parse(s)
assert.NoError(t, err)
_, err = node.Pipe(LookupCreate(yaml.MappingNode, "a", "b", "*", "t"))
assert.Error(t, err, "cannot support create option in a multi-value target now")
}
func TestLookup(t *testing.T) {
s := `n: o
a: