extensions

package
v0.0.0-...-02c76fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2020 License: Apache-2.0 Imports: 10 Imported by: 6

Documentation

Overview

Package extensions defines custom extensions to the go text templating language. This package serves primarily as a lightweight wrapper on top of text/template to ensure a consistent set of extensions across the application by providing some validate functions to ensure that a minimal set of keywords are supported.

Index

Constants

View Source
const RefFuncName string = "lookup"

RefFuncName is the name of the function to reference exported values from other nodes. It is a const defined here to make it easily changeable to avoid bikeshedding

Variables

This section is empty.

Functions

func DefaultEnv

func DefaultEnv(env string) string

DefaultEnv provides a default implementation for the env function in text templates. It operates by determining whether an environment variable exists; if so, returns its value, otherwise returns an empty string.

func DefaultJoin

func DefaultJoin(sep string, src interface{}) (string, error)

DefaultJoin provides a default implementation for the join function in text templates. It operates by simply reversing the arguments to split so that it works in a reasonable manner when dealing with piped output.

func DefaultJsonify

func DefaultJsonify(val interface{}) (string, error)

DefaultJsonify just marshals a value to string

func DefaultSplit

func DefaultSplit(sep, str string) []string

DefaultSplit provides a default implementation for the split function in text templates. It operates by simply reversing the arguments to split so that it works in a reasonable manner when dealing with piped input.

func RememberCalls

func RememberCalls(list *[]string, returnvalue interface{}) interface{}

RememberCalls is a utility function to instert calls into a list. RememberCalls takes a pointer to a list of strings, and a default. It returns a variadic function that when called from gotemplate will take the indexed argument and append it to the provided list.

func StubTemplateFunc

func StubTemplateFunc(...string) (string, error)

StubTemplateFunc is the NOP function for template parsing

func Unimplemented

func Unimplemented(name string) interface{}

Unimplemented returns a function that will raise an error with the fact that the keyword is unimplemented.

Types

type LanguageExtension

type LanguageExtension struct {
	Funcs template.FuncMap
	// contains filtered or unexported fields
}

LanguageExtension is a type wrapper around a template.FuncMap to allow us to encapsulate more context in the future.

func DefaultLanguage

func DefaultLanguage() *LanguageExtension

DefaultLanguage provides a default language extension. It creates default implementations of context-free and non-dependency-generating functions (e.g. split) and provides a unimplemented function for functions that must be supplied with context or which may register dependencies.

func EmptyLanguage

func EmptyLanguage() *LanguageExtension

EmptyLanguage will create an empty language

func MakeLanguage

func MakeLanguage() *LanguageExtension

MakeLanguage provides an empty language that implements a nil operation for all known keywords.

func MinimalLanguage

func MinimalLanguage() *LanguageExtension

MinimalLanguage provides a language extension where all known extensions are associated with NOP functions- as with MakeLanguage()- except that arity, input, and output types are respected and pure transformations are implemented. It is less featureful than DefaultLanguage but will not introduce template errors that may be present when using an unmodified MakeLanguage.

func (*LanguageExtension) Join

Join adds the keywords from toAdd that do not exist in l and adds them

func (*LanguageExtension) On

func (l *LanguageExtension) On(keyword string, action interface{}) *LanguageExtension

On provides a mechanism for defining an activity that will take place on encountering a keyword. It inserts the key and value pair into the language and returns a reference to the language. The language is mutated and the returned version is simply to allow method chaning, e.g.:

language = MakeLanguage().On("foo", foo).On("bar", bar).On("baz", baz)

func (*LanguageExtension) Render

func (l *LanguageExtension) Render(dotValues interface{}, name, toRender string) (bytes.Buffer, error)

Render provides a lightweight interface over template.New and template.Execute, it creates a new template given the name and input string, renders it with the currently defined language extensions, and writes the output into the provided io.Writer. If any error is returned at any point it is passed on to the user.

func (*LanguageExtension) Validate

func (l *LanguageExtension) Validate() (missingKeywords []string, extraKeywords []string, valid bool)

Validate checks the defined language against the known keywords and returns the deltas, if any. It returns true if the language exactly matches the known keyword list and false, with deltas, otherwise.

Directories

Path Synopsis
Package platform queries the underlying operating system
Package platform queries the underlying operating system

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL