template

package
v0.0.0-...-a355528 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const CacheDirEnvVar = "INFRAKIT_PLAYBOOKS_CACHE"

CacheDirEnvVar is the environment variable used to set the cache of the playbooks

Variables

This section is empty.

Functions

func DeepCopyObject

func DeepCopyObject(from interface{}) (interface{}, error)

DeepCopyObject makes a deep copy of the argument, using encoding/gob encode/decode.

func Dir

func Dir() string

Dir returns the directory to use for playbooks caching which can be customize via environment variable

func Escape

func Escape(source []byte) []byte

Escape replaces all the {{ and }} with \{\{ and \}\} to escape template content.

func Fetch

func Fetch(s string, opt Options) ([]byte, error)

Fetch fetchs content from the given URL string. Supported schemes are http:// https:// file:// unix://

func FromHCL

func FromHCL(o interface{}) (interface{}, error)

FromHCL decode the input HCL encoded as string or byte slice into a Go value

func FromINI

func FromINI(v string) (map[string]interface{}, error)

FromINI decodes content formatted in INI format at path

func FromJSON

func FromJSON(o interface{}) (interface{}, error)

FromJSON decode the input JSON encoded as string or byte slice into a Go value.

func FromMap

func FromMap(m map[string]interface{}, raw interface{}) error

FromMap decodes map into raw struct

func FromYAML

func FromYAML(o interface{}) (interface{}, error)

FromYAML decode the input YAML encoded as string or byte slice into a Go value.

func GetURL

func GetURL(root, rel string) (*url.URL, error)

GetURL returns a url string of the base and a relative path. e.g. http://host/foo/bar/baz, ./boo.tpl gives http://host/foo/bar/boo.tpl

func IndexOf

func IndexOf(srch interface{}, array interface{}, strictOptional ...bool) int

IndexOf returns the index of search in array. -1 if not found or array is not iterable. An optional true will turn on strict type check while by default string representations are used to compare values.

func QueryObject

func QueryObject(exp string, target interface{}) (interface{}, error)

QueryObject applies a JMESPath query specified by the expression, against the target object.

func Setup

func Setup() error

Setup sets up the necessary environment for running this module -- ie make sure cache directory exists, etc.

func SplitLines

func SplitLines(o interface{}) ([]string, error)

SplitLines splits the input into a string slice.

func ToJSON

func ToJSON(o interface{}) (string, error)

ToJSON encodes the input struct into a JSON string.

func ToJSONFormat

func ToJSONFormat(prefix, indent string, o interface{}) (string, error)

ToJSONFormat encodes the input struct into a JSON string with format prefix, and indent.

func ToMap

func ToMap(raw interface{}) (map[string]interface{}, error)

ToMap encodes the input as a map

func ToYAML

func ToYAML(o interface{}) (string, error)

ToYAML encodes the input struct into a YAML string.

func Unescape

func Unescape(source []byte) []byte

Unescape replaces all the \{\{ and \}\} back to the normal unescaped {{ and }}.

func UnixTime

func UnixTime() interface{}

UnixTime returns a timestamp in unix time

func ValidURL

func ValidURL(s string) string

ValidURL makes sure the input is of the URL form. If the input does not container :// then a str:// is prepended so that the input string is interpreted literally as the template itself.

Types

type Context

type Context interface {
	// Funcs returns a list of special template functions of the form func(template.Context, arg1, arg2) interface{}
	Funcs() []Function
}

Context is a marker interface for a user-defined struct that is passed into the template engine (as context) and accessible in the exported template functions. Template functions can have the signature func(template.Context, arg1, arg2 ...) (string, error) and when functions like this are registered, the template engine will dynamically create and export a function of the form func(arg1, arg2...) (string, error) where the context instance becomes an out-of-band struct that can be mutated by functions. This in essence allows structured data as output of the template, in addition to a string from evaluating the template.

type Function

type Function struct {

	// Name is the function name to bind in the template
	Name string

	// Description provides help for the function
	Description []string `json:",omitempty"`

	// Func is the reference to the actual function
	Func interface{} `json:"-"`

	// Function is the signature of the function
	Function string

	// Usage shows how to use it
	Usage string `json:",omitempty"`
}

Function contains the description of an exported template function

func UpdateDocumentation

func UpdateDocumentation(in []Function) []Function

UpdateDocumentation uses reflection to generate documentation on usage and function signature.

type FunctionExporter

type FunctionExporter interface {
	// Funcs returns a list of special template functions of the form func(template.Context, arg1, arg2) interface{}
	Funcs() []Function
}

FunctionExporter is implemented by any plugins wishing to show help on the function it exports.

type Options

type Options struct {

	// DelimLeft is the left delimiter, default is {{
	DelimLeft string

	// DelimRight is the right delimiter, default is }}
	DelimRight string

	// CustomizeFetch allows setting of http request header, etc. during fetch
	CustomizeFetch func(*http.Request) `json:"-" yaml:"-"`

	// Stderr is a function that returns stream to use for stderr
	Stderr func() io.Writer `json:"-" yaml:"-"`

	// MultiPass can affect the behavior of some functions like `var` where
	// evaluation of the function return different results based on whether the
	// template is meant to be evaluated in multiple passes.
	MultiPass bool

	// CacheDir is the location of the cache
	CacheDir string
}

Options contains parameters for customizing the behavior of the engine

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is the templating engine

func NewFromTemplate

func NewFromTemplate(from *Template, opt Options) *Template

NewFromTemplate creates a new template from existing, without any fetches (hence network calls)

func NewTemplate

func NewTemplate(s string, opt Options) (t *Template, err error)

NewTemplate fetches the content at the url and returns a template. If the string begins with str:// as scheme, then the rest of the string is interpreted as the body of the template.

func NewTemplateFromBytes

func NewTemplateFromBytes(buff []byte, contextURL string, opt Options) (*Template, error)

NewTemplateFromBytes builds the template from buffer with a contextURL which is used to deduce absolute path of any 'included' templates e.g. {{ include "./another.tpl" . }}

func (*Template) AddFunc

func (t *Template) AddFunc(name string, f interface{}) *Template

AddFunc adds a new function to support in template

func (*Template) DefaultFuncs

func (t *Template) DefaultFuncs() []Function

DefaultFuncs returns a list of default functions for binding in the template

func (*Template) DeferVar

func (t *Template) DeferVar(name string) string

DeferVar returns a template expression for a var that isn't resolved at this iteration

func (*Template) Dot

func (t *Template) Dot() interface{}

Dot returns the '.' in this template.

func (*Template) Execute

func (t *Template) Execute(output io.Writer, context interface{}) error

Execute is a drop-in replace of the execute method of template

func (*Template) Fetch

func (t *Template) Fetch(p string, opt ...interface{}) (string, error)

Fetch opens and reads a url and returns its content

func (*Template) Funcs

func (t *Template) Funcs() []Function

Funcs returns a list of registered functions used by the template when it rendered the view.

func (*Template) Global

func (t *Template) Global(name string, value interface{}) *Template

Global sets the a key, value in the context of this template. It is visible to all the 'included' and 'sourced' templates by the calling template.

func (*Template) Globals

func (t *Template) Globals() (map[string]interface{}, error)

Globals return all the globals created

func (*Template) Include

func (t *Template) Include(p string, opt ...interface{}) (string, error)

Include includes the template at the url inline.

func (*Template) Options

func (t *Template) Options() Options

Options returns the options used to initialize this template engine

func (*Template) Raw

func (t *Template) Raw(p string, opt ...interface{}) ([]byte, error)

Raw includes the raw bytes fetched from the url inline.

func (*Template) Ref

func (t *Template) Ref(name string) interface{}

Ref returns the value keyed by name in the context of this template.

func (*Template) RemoveFunc

func (t *Template) RemoveFunc(name ...string) *Template

RemoveFunc remove the functions

func (*Template) Render

func (t *Template) Render(context interface{}) (string, error)

Render renders the template given the context

func (*Template) SetOptions

func (t *Template) SetOptions(opt Options) *Template

SetOptions sets the runtime flags for the engine

func (*Template) Source

func (t *Template) Source(p string, opt ...interface{}) (string, error)

Source 'sources' the input file at url, also inherits all the variables.

func (*Template) Validate

func (t *Template) Validate() (*Template, error)

Validate parses the template and checks for validity.

func (*Template) Var

func (t *Template) Var(name string, optional ...interface{}) interface{}

Var implements the var function. It's a combination of global and ref Note that the behavior of the var function depends on whether the template is used in multiple passes where some var cannot be resolved to values in the first pass. In this case, if the MultiplePass flag is set, the var function will just echo back the same template expression in case of no value.

func (*Template) WithFunctions

func (t *Template) WithFunctions(functions func() []Function) *Template

WithFunctions allows client code to extend the template by adding its own functions.

type Void

type Void string

Void is used in the template functions return value type to indicate a void. Golang template does not allow functions with no return types to be bound.

const VoidValue Void = ""

VoidValue is the value of Void

Jump to

Keyboard shortcuts

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