From 1f8914874f8f4423f45101ef63edfbe93f733514 Mon Sep 17 00:00:00 2001 From: Dmitriy Kalinin Date: Tue, 25 May 2021 13:17:15 -0400 Subject: [PATCH] mark all data values coming from cmd as "@overlay/match missing_ok=True" removes + suffix feature for cmdline data values that was used to explicitly mark key as missing_ok=True --- pkg/cmd/template/cmd_data_values_test.go | 29 +++++++++++++------ pkg/cmd/template/data_values_flags.go | 36 +++++++++++------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/pkg/cmd/template/cmd_data_values_test.go b/pkg/cmd/template/cmd_data_values_test.go index c62bb2c2..188b356f 100644 --- a/pkg/cmd/template/cmd_data_values_test.go +++ b/pkg/cmd/template/cmd_data_values_test.go @@ -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)), @@ -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) diff --git a/pkg/cmd/template/data_values_flags.go b/pkg/cmd/template/data_values_flags.go index 3683f80c..5474f512 100644 --- a/pkg/cmd/template/data_values_flags.go +++ b/pkg/cmd/template/data_values_flags.go @@ -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 @@ -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 @@ -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}