-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
text/template: provide a mechanism for options
Add one option, which is the motivating example, a way to control what happens when a map is indexed with a key that is not in the map. Rather than do something specific for that case, we provide a simple general option mechanism to avoid adding API if something else comes up. This general approach also makes it easy for html/template to track (and adapt, should that become important). New method: Option(option string...). The option strings are key=value pairs or just simple strings (no =). New option: 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. Fixes golang#6288. Change-Id: Id811e2b99dc05aff324d517faac113ef3c25293a Reviewed-on: https://go-review.googlesource.com/8462 Reviewed-by: Robert Griesemer <[email protected]>
- Loading branch information
Showing
6 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 { | ||
for _, s := range opt { | ||
t.setOption(s) | ||
} | ||
return t | ||
} | ||
|
||
func (t *Template) setOption(opt string) { | ||
if opt == "" { | ||
panic("empty option string") | ||
} | ||
elems := strings.Split(opt, "=") | ||
switch len(elems) { | ||
case 2: | ||
// key=value | ||
switch elems[0] { | ||
case "missingkey": | ||
switch elems[1] { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters