Skip to content

Commit

Permalink
Set default option values if not set in file mode
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Brumhard <tobias.brumhard@mail.schwarz>
  • Loading branch information
brumhard committed Oct 29, 2021
1 parent d09895c commit 0e6ee55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/gotemplate/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,17 @@ func (gt *GT) LoadConfigValuesFromFile(file string) (*OptionValues, error) {
}

for _, category := range gt.Options.Extensions {
if optionValues.Extensions == nil {
optionValues.Extensions = map[string]OptionNameToValue{}
}
for _, option := range category.Options {
val, ok := optionValues.Base[option.Name()]
if optionValues.Extensions[category.Name] == nil {
optionValues.Extensions[category.Name] = OptionNameToValue{}
}
val, ok := optionValues.Extensions[category.Name][option.Name()]
if !ok {
// set defaults for all unset optionValues, no need to validate
optionValues.Extensions[category.Name][option.Name()] = option.Default(&optionValues)
continue
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/gotemplate/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ base:
require.ErrorIs(t, err, gotemplate.ErrMalformedInput)
})

t.Run("sets default values for extensions", func(t *testing.T) {
gt.Options = &gotemplate.Options{
Extensions: []gotemplate.Category{
{
Name: "test",
Options: []gotemplate.Option{
gotemplate.NewOption(
"string",
gotemplate.StringValue("desc"),
gotemplate.StaticValue("default"),
),
},
},
},
}

optionValues, err := loadValueFromTestFile(t, gt, "")
require.NoError(t, err)
require.Equal(t, &gotemplate.OptionValues{
Extensions: map[string]gotemplate.OptionNameToValue{
"test": {
"string": "default",
},
},
}, optionValues)
})

t.Run("supports int, string, bool", func(t *testing.T) {
gt.Options = &gotemplate.Options{
Base: []gotemplate.Option{
Expand Down

0 comments on commit 0e6ee55

Please sign in to comment.