Skip to content
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
37 changes: 30 additions & 7 deletions templates-repo/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ type (
// options holds the settings of a repository, the sources it is yet to read, and the first
// option that rejected its arguments.
options struct {
sources []source
funcs template.FuncMap
extensions []string
roots []string
coverPrefix string
err error
coverage bool
sources []source
funcs template.FuncMap
extensions []string
roots []string
coverPrefix string
err error
coverage bool
templateOption string
}
)

Expand Down Expand Up @@ -258,3 +259,25 @@ func WithCoverage(prefix string) Option {
return o
}
}

// MissingKeyBehavior exposes the options provided by [template.Template].
type MissingKeyBehavior string

const (
MissingKeyBehaviorDefault MissingKeyBehavior = "missingkey=default"
MissingKeyBehaviorZero MissingKeyBehavior = "missingkey=zero"
MissingKeyBehaviorError MissingKeyBehavior = "missingkey=error"
)

// WithMissingKey instructs templates to be built with the options provided by [template.Template]:
//
// - [MissingKeyBehaviorDefault]: execution continues on missing keys on maps, the value is set to "<no value>".
// - [MissingKeyBehaviorZero]: execution continues with the value set to the zero value of the element type.
// - [MissingKeyBehaviorError]: execution stops with an error
func WithMissingKey(onMissingKey MissingKeyBehavior) Option {
return func(o options) options {
o.templateOption = string(onMissingKey)

return o
}
}
4 changes: 4 additions & 0 deletions templates-repo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ func parseAssets(assets []asset, settings options) (parsedAssets, error) {
fmt.Errorf("could not parse template %q from asset %q: %w: %w", owner, item.path, err, ErrTemplateRepo)
}

if settings.templateOption != "" {
tpl = tpl.Option(settings.templateOption)
}

declaredHere := make(map[string]*declared, len(tpl.Templates()))
for _, found := range tpl.Templates() {
if found.Tree == nil {
Expand Down
Loading