Skip to content

Commit

Permalink
mark all data values coming from cmd as "@overlay/match missing_ok=True"
Browse files Browse the repository at this point in the history
removes + suffix feature for cmdline data values that was used to explicitly mark key as missing_ok=True
  • Loading branch information
Dmitriy Kalinin committed May 28, 2021
1 parent 7ede94c commit 1f89148
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
29 changes: 20 additions & 9 deletions pkg/cmd/template/cmd_data_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,34 @@ another:
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataValuesWithFlagsMarkedMissingOk(t *testing.T) {
func TestDataValuesWithFlagsWithoutDataValuesOverlay(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
data_int: #@ data.values.int
data_str: #@ data.values.str
values: #@ data.values`)

expectedYAMLTplData := `values:
expectedYAMLTplData := `data_int: 124
data_str: str
values:
int: 124
another:
nested:
map: 567
str: str
boolean: true
nested:
value: str
another_nested:
other_value: str2
`

// Only some values are prespecified by the overlay
yamlData := []byte(`
#@data/values
---
nested:
value: str
`)
int: 123
another:
nested:
map: {"a": 123}`)

filesToProcess := files.NewSortedFiles([]*files.File{
files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)),
Expand All @@ -130,8 +140,9 @@ nested:
opts := cmdtpl.NewOptions()

opts.DataValuesFlags = cmdtpl.DataValuesFlags{
// TODO add nested.value2*=str2 since replace with 0 nodes does not do anything
KVsFromYAML: []string{"another_nested+.other_value=str2"},
EnvFromStrings: []string{"DVS"},
EnvironFunc: func() []string { return []string{"DVS_str=str"} },
KVsFromYAML: []string{"int=124", "boolean=true", "nested.value=\"str\"", "another.nested.map=567"},
}

out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui)
Expand Down
36 changes: 16 additions & 20 deletions pkg/cmd/template/data_values_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ func (DataValuesFlags) libraryRefAndKey(key string) (string, string, error) {
}

func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, desc string) *yamlmeta.Document {
const (
missingOkSuffix = "+"
)

resultMap := &yamlmeta.Map{}
currMap := resultMap
var lastMapItem *yamlmeta.MapItem
Expand All @@ -301,22 +297,17 @@ func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, de

for _, piece := range keyPieces {
newMap := &yamlmeta.Map{}
nodeAnns := template.NodeAnnotations{}

if strings.HasSuffix(piece, missingOkSuffix) {
piece = piece[:len(piece)-1]
nodeAnns = template.NodeAnnotations{
yttoverlay.AnnotationMatch: template.NodeAnnotation{
Kwargs: []starlark.Tuple{{
starlark.String(yttoverlay.MatchAnnotationKwargMissingOK),
starlark.Bool(true),
}},
},
}
}

lastMapItem = &yamlmeta.MapItem{Key: piece, Value: newMap, Position: pos}
lastMapItem.SetAnnotations(nodeAnns)

// Data values schemas should be enough to provide key checking/validations.
lastMapItem.SetAnnotations(template.NodeAnnotations{
yttoverlay.AnnotationMatch: template.NodeAnnotation{
Kwargs: []starlark.Tuple{{
starlark.String(yttoverlay.MatchAnnotationKwargMissingOK),
starlark.Bool(true),
}},
},
})

currMap.Items = append(currMap.Items, lastMapItem)
currMap = newMap
Expand All @@ -327,7 +318,12 @@ func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, de
// Explicitly replace entire value at given key
// (this allows to specify non-scalar data values)
existingAnns := template.NewAnnotations(lastMapItem)
existingAnns[yttoverlay.AnnotationReplace] = template.NodeAnnotation{}
existingAnns[yttoverlay.AnnotationReplace] = template.NodeAnnotation{
Kwargs: []starlark.Tuple{{
starlark.String(yttoverlay.ReplaceAnnotationKwargOrAdd),
starlark.Bool(true),
}},
}
lastMapItem.SetAnnotations(existingAnns)

return &yamlmeta.Document{Value: resultMap, Position: pos}
Expand Down

0 comments on commit 1f89148

Please sign in to comment.