Bumps the minor-and-patch group with 3 updates in the / directory: [github.com/gabriel-vasile/mimetype](https://github.com/gabriel-vasile/mimetype), [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) and [github.com/rogpeppe/go-internal](https://github.com/rogpeppe/go-internal). Updates `github.com/gabriel-vasile/mimetype` from 1.4.13 to 1.4.15 - [Release notes](https://github.com/gabriel-vasile/mimetype/releases) - [Commits](https://github.com/gabriel-vasile/mimetype/compare/v1.4.13...v1.4.15) Updates `github.com/open-policy-agent/opa` from 1.18.2 to 1.19.0 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.18.2...v1.19.0) Updates `github.com/rogpeppe/go-internal` from 1.15.0 to 1.16.0 - [Release notes](https://github.com/rogpeppe/go-internal/releases) - [Commits](https://github.com/rogpeppe/go-internal/compare/v1.15.0...v1.16.0) --- updated-dependencies: - dependency-name: github.com/gabriel-vasile/mimetype dependency-version: 1.4.15 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.19.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/rogpeppe/go-internal dependency-version: 1.16.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This file contains the code to handle template options.
|
|
|
|
package template
|
|
|
|
import "strings"
|
|
|
|
// missingKeyAction defines how to respond to indexing a map with a key that is not present.
|
|
type missingKeyAction int
|
|
|
|
const (
|
|
mapInvalid missingKeyAction = iota // Return an invalid reflect.Value.
|
|
mapZeroValue // Return the zero value for the map element.
|
|
mapError // Error out
|
|
)
|
|
|
|
type option struct {
|
|
missingKey missingKeyAction
|
|
}
|
|
|
|
// Option sets options for the template. Options are described by
|
|
// strings, either a simple string or "key=value". There can be at
|
|
// most one equals sign in an option string. If the option string
|
|
// is unrecognized or otherwise invalid, Option panics.
|
|
//
|
|
// Known options:
|
|
//
|
|
// missingkey: Control the behavior during execution if a map is
|
|
// indexed with a key that is not present in the map.
|
|
//
|
|
// "missingkey=default" or "missingkey=invalid"
|
|
// The default behavior: Do nothing and continue execution.
|
|
// If printed, the result of the index operation is the string
|
|
// "<no value>".
|
|
// "missingkey=zero"
|
|
// The operation returns the zero value for the map type's element.
|
|
// "missingkey=error"
|
|
// Execution stops immediately with an error.
|
|
func (t *Template) Option(opt ...string) *Template {
|
|
t.init()
|
|
for _, s := range opt {
|
|
t.setOption(s)
|
|
}
|
|
return t
|
|
}
|
|
|
|
func (t *Template) setOption(opt string) {
|
|
if opt == "" {
|
|
panic("empty option string")
|
|
}
|
|
// key=value
|
|
if key, value, ok := strings.Cut(opt, "="); ok {
|
|
switch key {
|
|
case "missingkey":
|
|
switch value {
|
|
case "invalid", "default":
|
|
t.option.missingKey = mapInvalid
|
|
return
|
|
case "zero":
|
|
t.option.missingKey = mapZeroValue
|
|
return
|
|
case "error":
|
|
t.option.missingKey = mapError
|
|
return
|
|
}
|
|
}
|
|
}
|
|
panic("unrecognized option: " + opt)
|
|
}
|