Documentation ¶
Index ¶
- Constants
- Variables
- func FirstFile(result *string, info *fs.FileInfo, names ...string) func(string) error
- func MediaType(t Template) (mediaType string)
- func UpSearch(dir string, fns ...func(string) error) (err error)
- type InvalidMissingKeyError
- type KeyValues
- type Matcher
- type Matchers
- type MediaTyper
- type Model
- type Parser
- type ParserConfig
- type Selector
- type SelectorConfig
- type Template
Constants ¶
const ( MissingKeyInvalid = "invalid" MissingKeyDefault = "default" MissingKeyZero = "zero" MissingKeyError = "error" DefaultMissingKey = MissingKeyError )
const DefaultMediaType = "application/json"
DefaultMediaType is the MIME type assumed when either an object does not implement MediaTyper or returns the empty string from its MediaType method.
Variables ¶
var ( // ErrStopSearch is a sentinel indicating that UpSearch should halt and return. ErrStopSearch = errors.New("sentinel error indicating a search should end") )
Functions ¶
func FirstFile ¶
FirstFile returns a predicate for UpSearch that matches the first file with any of a set of names. Upon any match, the returned predicate returns ErrStopSearch.
The result pointer must be non-nil and receives the absolute path of the first file matched. Info is optional, and if non-nil it receives the fs.FileInfo associated with the result path.
func MediaType ¶
MediaType returns the MIME type associated with the given template. If no MIME type is associated with the template, DefaultMediaType is returned.
func UpSearch ¶
UpSearch walks up a directory applying predicates to each absolute path. The given directory is walked up to the root. If any of the predicates return the special value ErrStopSearch, then UpSearch returns immediately with a nil error. Otherwise, any error halts the search and that error is returned.
Types ¶
type InvalidMissingKeyError ¶
type InvalidMissingKeyError struct {
Value string
}
InvalidMissingKeyError indicates that an unrecognized value was used in configuration for MissingKey. The text/template and html/template packages will panic in this case, whereas this package uses this error.
func (*InvalidMissingKeyError) Error ¶
func (imke *InvalidMissingKeyError) Error() string
Error satisfies the error interface.
type KeyValues ¶
type KeyValues struct {
// contains filtered or unexported fields
}
KeyValues is an immutable, efficient list of key/value pairs for enriching Models prior to template execution.
func (KeyValues) Append ¶
Append adds key/value pairs to this list. If more is not empty, a new, distinct KeyValues is created and returned with the Model's key/value pairs added. If more is empty, this KeyValues is returned.
Duplicate keys are allowed. The final value of that key is simply the value last added to this list.
This method does not modify this instance.
func (KeyValues) ApplyDefaults ¶
ApplyDefaults uses the key/value pairs in this list as defaults for the given model. Any key present in this list but not present in the model will be set to the value in this list. Existing keys in the model that are not in this list are left untouched.
func (KeyValues) ApplyOverrides ¶
ApplyOverrides unconditionally sets each key in this list on the given model. Existing keys in the model that are in this list are overridden.
func (KeyValues) Extend ¶
Extend adds the key/value pairs in another list to this list. If more is empty, this instance is returned. If this instance is empty, then more is returned. Otherwise, a new, distinct KeyValues is returned that is the merge of the two.
Duplicate keys are allowed. The final value of that key is simply the value last added to this list.
This method does not modify this instance.
type Matcher ¶
Matcher is a simple strategy for matching values, such as names and paths.
func ParsePatterns ¶
ParsePatterns parses a sequence of globs for matching values. The returned Matcher will match values if at least one of the globs matched. If patterns is empty, then the returned Matcher won't match anything.
type Matchers ¶
type Matchers []Matcher
Matchers is an aggregate Matcher. A value will match if any of the sequence of Matchers returns true.
type MediaTyper ¶
type MediaTyper interface { // MediaType returns the MIME type associated with this object. If // this method returns the empty string, then DefaultMediaType is assumed. MediaType() string }
MediaTyper is an optional interface that associates a MIME type with an object. Typically, this interface is implemented by templates produced by this package to indicate what the MIME type of the rendered template is.
type Parser ¶
type Parser interface { // Parse produces a Template from some parsed content. The name is // optional, and can be the empty string. // // If a common Model was defined in ParserConfig, the returned Template // will also implement ModelTemplate. // // For golang templates, an internal prototype template is first cloned // and then used as the containing template for each parsed template. This // ensures that every template is unaffected by global definitions in other templates. Parse(name, content string) (Template, error) }
Parser is a template parser. A ParserConfig defines all the options for tailoring a Parser as desired.
func NewParser ¶
func NewParser(c ParserConfig) (Parser, error)
NewParser creates a Parser from a set of configuration options. Templates returned by the parser created by this function will also implement MediaTyper, which will associated each Template with the media type specified in the config.
type ParserConfig ¶
type ParserConfig struct { // HTML indicates which template package to use. If this field is false, // which is the default, text/template is used by the returned Parser. // If this field is true, html/template is used instead. HTML bool `json:"html" yaml:"html"` // MissingKey is the "missingkey=..." option. If unset, error is used. // If this field is set to an unrecognized value, an error is raised. MissingKey string `json:"missingKey" yaml:"missingKey"` // LeftDelim is the left delimiter for pipelines. If unset, the default "{{" is used. LeftDelim string `json:"leftDelim" yaml:"leftDelim"` // RightDelim is the right delimiter for pipelines. If unset, the default "}}" is used. RightDelim string `json:"rightDelim" yaml:"rightDelim"` // FuncMap is the function map for all templates returned by the Parser. FuncMap map[string]interface{} `json:"-" yaml:"-"` // MediaType is the media type associated with all rendered templates produced // by this parser configuration. If unset, DefaultMediaType is assumed. MediaType string `json:"mediaType" yaml:"mediaType"` }
ParserConfig is the set of configurable options for building a Parser.
type Selector ¶
type Selector interface { // Select chooses a parser based on the template name. If no parser // was found for the template, this method returns (nil, false). Select(name string) (Parser, bool) }
Selector is a strategy for determining how to parse a template based on that template's name. The name is typically a relative path, which may be matched via globbing.
func NewSelector ¶
func NewSelector(configs ...SelectorConfig) (Selector, error)
NewSelector constructs a Selector based on the given configurations. If an empty configs is passed, the returned Selector won't match any template names.
type SelectorConfig ¶
type SelectorConfig struct { // Patterns are the globs which must match a template's name in order // to use this configured parser. Patterns []string `json:"patterns" yaml:"patterns"` // Parser is the configuration for parsing templates that match any // of the configured patterns. Parser ParserConfig `json:"parser" yaml:"parser"` }
SelectorConfig represents a set of templates whose relative paths match one or more patterns.
type Template ¶
type Template interface { // Name returns the name of this template supplied at parse time. This will // typically be the relative path of this template. Name() string // Execute renders this template using the supplied data. The type of // data is unspecified and is dependent on the underlying template. Execute(output io.Writer, data interface{}) error }
Template represents a parsed template. The golang text and HTML templates implement this interface.
func MediaTemplate ¶
MediaTemplate associates a Template, typically a raw golang template, with a MIME type. The returned Template will also implement MediaTyper.