Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Empty data values is struct instead of None #431

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/cmd/template/cmd_data_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ import (
"github.com/stretchr/testify/require"
)

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

expectedYAMLTplData := `data_int: {}
`
filesToProcess := files.NewSortedFiles([]*files.File{
files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)),
})

ui := ui.NewTTY(false)
opts := cmdtpl.NewOptions()

out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui)
require.NoError(t, out.Err)
require.Len(t, out.Files, 1, "unexpected number of output files")

file := out.Files[0]

assert.Equal(t, "tpl.yml", file.RelativePath())
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataValues(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/template/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ foo:
bar: 3
ree: set from root
---
root_data_values: null
root_data_values: {}
`

filesToProcess := files.NewSortedFiles([]*files.File{
Expand Down Expand Up @@ -1692,7 +1692,7 @@ foo:
bar: 3
ree: set from root
---
root_data_values: null
root_data_values: {}
`

filesToProcess := files.NewSortedFiles([]*files.File{
Expand Down Expand Up @@ -2032,7 +2032,7 @@ system_domain: #@ data.values.system_domain
files.MustNewFileFromSource(files.NewBytesSource("template.yml", []byte(templateYAML))),
})

expectedErr := "NoneType has no .system_domain field or method"
expectedErr := "struct has no .system_domain field or method"
assertFails(t, filesToProcess, expectedErr, opts)
})
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/workspace/data_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/k14s/ytt/pkg/filepos"
"github.com/k14s/ytt/pkg/template"
"github.com/k14s/ytt/pkg/template/core"
"github.com/k14s/ytt/pkg/workspace/ref"
Expand Down Expand Up @@ -38,7 +39,14 @@ func NewDataValues(doc *yamlmeta.Document) (*DataValues, error) {
}

func NewEmptyDataValues() *DataValues {
return &DataValues{Doc: &yamlmeta.Document{}}
return &DataValues{Doc: newEmptyDataValuesDocument()}
}

func newEmptyDataValuesDocument() *yamlmeta.Document {
return &yamlmeta.Document{
Value: &yamlmeta.Map{},
Position: filepos.NewUnknownPosition(),
}
}

type ExtractLibRefs interface {
Expand Down
10 changes: 1 addition & 9 deletions pkg/workspace/data_values_pre_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/k14s/starlark-go/starlark"
"github.com/k14s/ytt/pkg/filepos"
"github.com/k14s/ytt/pkg/schema"
"github.com/k14s/ytt/pkg/yamlmeta"
yttoverlay "github.com/k14s/ytt/pkg/yttlibrary/overlay"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (o DataValuesPreProcessing) apply(files []*FileInLibrary) (*DataValues, []*
}

if resultDVsDoc == nil {
resultDVsDoc = o.newEmptyDataValuesDocument()
resultDVsDoc = newEmptyDataValuesDocument()
}
dataValues, err := NewDataValues(resultDVsDoc)
if err != nil {
Expand Down Expand Up @@ -159,13 +158,6 @@ func (o DataValuesPreProcessing) templateFile(fileInLib *FileInLibrary) ([]*yaml
return valuesDocs, nil
}

func (o DataValuesPreProcessing) newEmptyDataValuesDocument() *yamlmeta.Document {
return &yamlmeta.Document{
Value: nil,
Position: filepos.NewUnknownPosition(),
}
}

func (o DataValuesPreProcessing) overlay(dataValues, overlay *yamlmeta.Document) (*yamlmeta.Document, error) {
op := yttoverlay.Op{
Left: &yamlmeta.DocumentSet{Items: []*yamlmeta.Document{dataValues}},
Expand Down