exec

package
v0.0.0-...-b160839 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 17 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDefined

func IsDefined(val Value) bool

IsDefined returns true if the given value is not undefined.

func Self

func Self(r *Renderer) map[string]func() (string, error)

Types

type BaseValue

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

BaseValue serves as a base for value containers.

func NewBaseValue

func NewBaseValue(valueFactory *ValueFactory, isSafe bool) *BaseValue

NewBaseValue creates a new BaseValue container.

func (*BaseValue) Bool

func (*BaseValue) Bool() bool

func (*BaseValue) Contains

func (*BaseValue) Contains(other Value) bool

func (*BaseValue) EqualValueTo

func (*BaseValue) EqualValueTo(other Value) bool

func (*BaseValue) Escaped

func (*BaseValue) Escaped() string

func (*BaseValue) Float

func (*BaseValue) Float() float64

func (*BaseValue) GetItem

func (*BaseValue) GetItem(key any) Value

func (*BaseValue) Index

func (*BaseValue) Index(i int) Value

func (*BaseValue) Integer

func (*BaseValue) Integer() int

func (*BaseValue) Interface

func (*BaseValue) Interface() any

func (*BaseValue) IsBool

func (*BaseValue) IsBool() bool

func (*BaseValue) IsCallable

func (*BaseValue) IsCallable() bool

func (*BaseValue) IsDict

func (*BaseValue) IsDict() bool

func (*BaseValue) IsFloat

func (*BaseValue) IsFloat() bool

func (*BaseValue) IsInteger

func (*BaseValue) IsInteger() bool

func (*BaseValue) IsIterable

func (*BaseValue) IsIterable() bool

func (*BaseValue) IsList

func (*BaseValue) IsList() bool

func (*BaseValue) IsNil

func (*BaseValue) IsNil() bool

func (*BaseValue) IsNumber

func (*BaseValue) IsNumber() bool

func (*BaseValue) IsSafe

func (v *BaseValue) IsSafe() bool

func (*BaseValue) IsSliceable

func (*BaseValue) IsSliceable() bool

func (*BaseValue) IsString

func (*BaseValue) IsString() bool

func (*BaseValue) Items

func (*BaseValue) Items() []*Pair

func (*BaseValue) Iterate

func (*BaseValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())

func (*BaseValue) IterateOrder

func (*BaseValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), reverse, sorted, caseSensitive bool)

func (*BaseValue) Keys

func (*BaseValue) Keys() ValuesList

func (*BaseValue) Len

func (*BaseValue) Len() int

func (*BaseValue) ReflectValue

func (*BaseValue) ReflectValue() reflect.Value

func (*BaseValue) SetItem

func (*BaseValue) SetItem(key string, value any)

func (*BaseValue) Slice

func (*BaseValue) Slice(i, j int) Value

func (*BaseValue) String

func (*BaseValue) String() string

func (*BaseValue) Values

func (*BaseValue) Values() ValuesList

type ChainedStrictUndefinedValue

type ChainedStrictUndefinedValue struct {
	StrictUndefinedValue
}

ChainedStrictUndefinedValue is like ChainedUndefinedValue but throws an error on any other type of access.

func (*ChainedStrictUndefinedValue) GetItem

func (u *ChainedStrictUndefinedValue) GetItem(key any) Value

Get returns the value for the given key.

type ChainedUndefinedValue

type ChainedUndefinedValue struct {
	UndefinedValue
}

ChainedUndefinedValue represents an undefined value that renders to an empty string. Most other access methods will throw an error. It is different from UndefinedValue in that it allows for chaining for `Get` calls.

func (*ChainedUndefinedValue) GetItem

func (u *ChainedUndefinedValue) GetItem(key any) Value

Get returns the value for the given key.

type Context

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

func NewContext

func NewContext(data map[string]any, userData any, valueFactory *ValueFactory) *Context

func NewEmptyContext

func NewEmptyContext(valueFactory *ValueFactory) *Context

func (*Context) Get

func (ctx *Context) Get(name string) Value

func (*Context) Inherit

func (ctx *Context) Inherit() *Context

func (*Context) Set

func (ctx *Context) Set(name string, value any)

func (*Context) Update

func (ctx *Context) Update(other map[string]any) *Context

Update updates this context with the key/value pairs from a map.

type Dict

type Dict struct {
	Pairs []*Pair
}

Dict represents a mapping of key-value [Pair]s.

func NewDict

func NewDict() *Dict

NewDict creates a new Dict.

func (*Dict) Get

func (d *Dict) Get(key Value) (value Value, ok bool)

Get returns the Value for the given key from d.

func (*Dict) Keys

func (d *Dict) Keys() ValuesList

Keys returns a [ValueList] of keys contained in the dict.

func (*Dict) String

func (d *Dict) String() string

String returns a string representation of d in the form "'key1': 'value', "'key2': 'value'".

type EvalConfig

type EvalConfig struct {
	*parse.Config

	Globals        map[string]any
	Filters        *FilterSet
	Statements     *StatementSet
	Tests          *TestSet
	TemplateLoadFn TemplateLoadFn

	// ExtensionConfig stores configuration for extensions.
	ExtensionConfig map[string]ext.Inheritable

	// CustomTypes allows to add custom value representations for types that are
	// not supported by default. For example, if you want to resolve value from
	// a custom ordered map type, you can add a custom value representation for
	// that and implement the [GetItem] method.
	CustomTypes map[reflect.Type]ValueFunc

	// Undefined is the type of undefined values that the resolver returns when
	// a value is not found.
	Undefined UndefinedFunc

	// NewlineSequence defines the sequence that starts a newline. Must be one
	// of '\r', '\n' or '\r\n'. The default is '\n' which is a useful default
	// for Linux and OS X systems as well as web applications.
	NewlineSequence string

	// TrimBlocks will remove the first newline after a block (block, not
	// variable tag!), if set to true. Defaults to false.
	TrimBlocks bool

	// LstripBlocks will strip leading spaces and tabs from the start of a line
	// to a block, if set to true. Defaults to false.
	LstripBlocks bool

	// KeepTrailingNewline will preserve the trailing newline when rendering
	// templates, if set to true. The default is false, which causes a single
	// newline, if present, to be stripped from the end of the template.
	KeepTrailingNewline bool

	// Autoescape will escape XML/HTML automatically, if set to true. Defaults
	// to false.
	Autoescape bool
}

EvalConfig is the configuration for the execution of a template.

func NewEvalConfig

func NewEvalConfig() *EvalConfig

NewEvalConfig creates a new evaluator configuration.

func (EvalConfig) Inherit

func (cfg EvalConfig) Inherit() *EvalConfig

Inherit copies the configuration and returns a new configuration.

type Evaluator

type Evaluator struct {
	*EvalConfig
	Ctx          *Context
	ValueFactory *ValueFactory
	Current      parse.Node
}

func (*Evaluator) Eval

func (e *Evaluator) Eval(node parse.Expression) Value

func (*Evaluator) ExecuteFilter

func (e *Evaluator) ExecuteFilter(fc *parse.FilterCall, v Value) Value

ExecuteFilter execute a filter node

func (*Evaluator) ExecuteFilterByName

func (e *Evaluator) ExecuteFilterByName(name string, in Value, params *VarArgs) Value

ExecuteFilterByName execute a filter given its name

func (*Evaluator) ExecuteTest

func (e *Evaluator) ExecuteTest(tc *parse.TestCall, v Value) Value

func (*Evaluator) ExecuteTestByName

func (e *Evaluator) ExecuteTestByName(name string, in Value, params *VarArgs) Value

type FilterFunction

type FilterFunction func(e *Evaluator, in Value, params *VarArgs) Value

FilterFunction is the type filter functions must fulfil

type FilterSet

type FilterSet map[string]FilterFunction

func (FilterSet) Exists

func (fs FilterSet) Exists(name string) bool

Exists returns true if the given filter is already registered

func (*FilterSet) Register

func (fs *FilterSet) Register(name string, fn FilterFunction) error

Register registers a new filter. If there's already a filter with the same name, Register will panic. You usually want to call this function in the filter's init() function: http://golang.org/doc/effective_go.html#init

See http://www.florian-schlachter.de/post/gonja/ for more about writing filters and tags.

func (*FilterSet) Replace

func (fs *FilterSet) Replace(name string, fn FilterFunction) error

Replace replaces an already registered filter with a new implementation. Use this function with caution since it allows you to change existing filter behavior.

func (*FilterSet) Update

func (fs *FilterSet) Update(other FilterSet) FilterSet

type GenericValue

type GenericValue struct {
	BaseValue

	// Value holds the actual value in form of a reflection value.
	Value reflect.Value

	// IndirectValue holds the indirect (resolved) value . This is used to avoid
	// resolving the pointer values more than once.
	IndirectValue reflect.Value
	// contains filtered or unexported fields
}

GenericValue is a container for values of various types.

func (*GenericValue) Bool

func (v *GenericValue) Bool() bool

Bool returns the underlying value as bool. Non bool values will be evaluated to true in the following cases:

  • int != 0
  • uint != 0
  • float != 0.0
  • len(array/chan/map/slice/string) > 0
  • bool == true
  • underlying value is a struct

Otherwise returns always FALSE.

func (*GenericValue) Contains

func (v *GenericValue) Contains(other Value) bool

Contains reports whether the underlying value (which must be of type struct, map, string, array or slice) contains of another Value (e. g. used to check whether a struct contains of a specific field or a map contains a specific key).

func (*GenericValue) EqualValueTo

func (v *GenericValue) EqualValueTo(other Value) bool

EqualValueTo reports whether two values are containing the same value or object.

func (*GenericValue) Escaped

func (v *GenericValue) Escaped() string

Escaped returns the HTML escaped version of String()

func (*GenericValue) Float

func (v *GenericValue) Float() float64

Float returns the underlying value as a float (converts the underlying value, if necessary). If it's not possible to convert the underlying value, it will return 0.0.

func (*GenericValue) GetItem

func (v *GenericValue) GetItem(key any) Value

Get returns the value for the given key. If 'value' has no such key, the undefined value is returned.

func (*GenericValue) Index

func (v *GenericValue) Index(i int) Value

func (*GenericValue) Integer

func (v *GenericValue) Integer() int

Integer returns the underlying value as an integer (converts the underlying value, if necessary). If it's not possible to convert the underlying value, it will return 0.

func (*GenericValue) Interface

func (v *GenericValue) Interface() any

Interface returns the underlying value as an interface{}.

func (*GenericValue) IsBool

func (v *GenericValue) IsBool() bool

IsBool reports whether the underlying value is a bool.

func (*GenericValue) IsCallable

func (v *GenericValue) IsCallable() bool

IsCallable reports whether the underlying value is a callable function.

func (*GenericValue) IsDict

func (v *GenericValue) IsDict() bool

IsDict reports whether the underlying value is a dictionary.

func (*GenericValue) IsFloat

func (v *GenericValue) IsFloat() bool

IsFloat reports whether the underlying value is a float.

func (*GenericValue) IsInteger

func (v *GenericValue) IsInteger() bool

IsInteger reports whether the underlying value is an integer.

func (*GenericValue) IsIterable

func (v *GenericValue) IsIterable() bool

IsIterable reports whether the underlying value is an iterable type. Iterable types are strings, lists and dictionaries.

func (*GenericValue) IsList

func (v *GenericValue) IsList() bool

IsList reports whether the underlying value is a list.

func (*GenericValue) IsNil

func (v *GenericValue) IsNil() bool

IsNil reports whether the underlying value is NIL.

func (*GenericValue) IsNumber

func (v *GenericValue) IsNumber() bool

IsNumber reports whether the underlying value is either an integer or a float.

func (*GenericValue) IsSliceable

func (v *GenericValue) IsSliceable() bool

IsSliceable reports whether the underlying value is of type array, slice or string. You normally would use IsSliceable() before using the Slice() operation.

func (*GenericValue) IsString

func (v *GenericValue) IsString() bool

IsString reports whether the underlying value is a string.

func (*GenericValue) Items

func (v *GenericValue) Items() []*Pair

Items returns a list items contained in v.

func (*GenericValue) Iterate

func (v *GenericValue) Iterate(fn func(idx, count int, key, value Value) (cont bool), empty func())

Iterate iterates over a map, array, slice or a string. It calls the function's first argument for every value with the following arguments:

idx      current 0-index
count    total amount of items
key      *Value for the key or item
value    *Value (only for maps, the respective value for a specific key)

If the underlying value has no items or is not one of the types above, the empty function (function's second argument) will be called.

func (*GenericValue) IterateOrder

func (v *GenericValue) IterateOrder(
	fn func(idx, count int, key, value Value) (cont bool),
	empty func(),
	reverse bool,
	sorted bool,
	caseSensitive bool,
)

IterateOrder behaves like [Value.Iterate], but can iterate through an array/slice/string in reverse. Does not affect the iteration through a map because maps don't have any particular order. However, you can force an order using the `sorted` keyword (and even use `reversed sorted`).

func (*GenericValue) Keys

func (v *GenericValue) Keys() ValuesList

Keys returns a list of keys contained in v.

func (*GenericValue) Len

func (v *GenericValue) Len() int

Len returns the length for an array, chan, map, slice or string. Otherwise it will return 0.

func (*GenericValue) ReflectValue

func (v *GenericValue) ReflectValue() reflect.Value

ReflectValue returns the underlying reflect value.

func (*GenericValue) SetItem

func (v *GenericValue) SetItem(key string, value interface{})

XXX: need to work on that

func (*GenericValue) Slice

func (v *GenericValue) Slice(i, j int) Value

Slice slices an array, slice or string. Otherwise it will return an empty []int.

func (*GenericValue) String

func (v *GenericValue) String() string

String returns a string for the underlying value. If this value is not of type string, gonja tries to convert it. Currently the following types for underlying values are supported:

  1. string
  2. int/uint (any size)
  3. float (any precision)
  4. bool
  5. array/slice
  6. map
  7. String() will be called on the underlying value if provided

NIL values will lead to an empty string. Unsupported types are leading to their respective type name.

func (*GenericValue) Type

func (v *GenericValue) Type() reflect.Type

Type returns the type of the value.

func (*GenericValue) Values

func (v *GenericValue) Values() ValuesList

Values returns a list of values contained in v.

type KVPair

type KVPair struct {
	Key   string
	Value Value
}

KVPair represents a key/value pair.

type Kwarg

type Kwarg struct {
	Name    string
	Default any
}

Kwarg represents a keyword argument.

type Macro

type Macro func(params *VarArgs) Value

FilterFunction is the type filter functions must fulfil

func MacroNodeToFunc

func MacroNodeToFunc(node *parse.MacroNode, r *Renderer) Macro

type MacroSet

type MacroSet map[string]Macro

func (MacroSet) Exists

func (ms MacroSet) Exists(name string) bool

Exists returns true if the given filter is already registered

func (*MacroSet) Register

func (ms *MacroSet) Register(name string, fn Macro) error

Register registers a new filter. If there's already a filter with the same name, Register will panic. You usually want to call this function in the filter's init() function: http://golang.org/doc/effective_go.html#init

func (*MacroSet) Replace

func (ms *MacroSet) Replace(name string, fn Macro) error

Replace replaces an already registered filter with a new implementation. Use this function with caution since it allows you to change existing filter behavior.

type NilValue

type NilValue struct {
	BaseValue
}

NilValue represents a value that is nil.

func NewNilValue

func NewNilValue() *NilValue

NewNilValue creates a new NilValue.

func (*NilValue) Bool

func (*NilValue) Bool() bool

func (*NilValue) Contains

func (*NilValue) Contains(other Value) bool

func (*NilValue) EqualValueTo

func (*NilValue) EqualValueTo(other Value) bool

func (*NilValue) Escaped

func (*NilValue) Escaped() string

func (*NilValue) Float

func (*NilValue) Float() float64

func (*NilValue) GetItem

func (*NilValue) GetItem(key any) Value

func (*NilValue) Index

func (*NilValue) Index(i int) Value

func (*NilValue) Integer

func (*NilValue) Integer() int

func (*NilValue) Interface

func (*NilValue) Interface() any

func (*NilValue) IsBool

func (*NilValue) IsBool() bool

func (*NilValue) IsCallable

func (*NilValue) IsCallable() bool

func (*NilValue) IsDict

func (*NilValue) IsDict() bool

func (*NilValue) IsFloat

func (*NilValue) IsFloat() bool

func (*NilValue) IsInteger

func (*NilValue) IsInteger() bool

func (*NilValue) IsIterable

func (*NilValue) IsIterable() bool

func (*NilValue) IsList

func (*NilValue) IsList() bool

func (*NilValue) IsNil

func (*NilValue) IsNil() bool

func (*NilValue) IsNumber

func (*NilValue) IsNumber() bool

func (*NilValue) IsSafe

func (*NilValue) IsSafe() bool

func (*NilValue) IsSliceable

func (*NilValue) IsSliceable() bool

func (*NilValue) IsString

func (*NilValue) IsString() bool

func (*NilValue) Items

func (*NilValue) Items() []*Pair

func (*NilValue) Iterate

func (*NilValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())

func (*NilValue) IterateOrder

func (*NilValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), reverse, sorted, caseSensitive bool)

func (*NilValue) Keys

func (*NilValue) Keys() ValuesList

func (*NilValue) Len

func (*NilValue) Len() int

func (*NilValue) ReflectValue

func (*NilValue) ReflectValue() reflect.Value

func (*NilValue) SetItem

func (*NilValue) SetItem(key string, value interface{})

func (*NilValue) Slice

func (*NilValue) Slice(i, j int) Value

func (*NilValue) String

func (*NilValue) String() string

type Pair

type Pair struct {
	Key   Value
	Value Value
}

Pair represents a pair of key and value.

func (*Pair) String

func (p *Pair) String() string

String returns a string representation of p in the form "'key': 'value'".

type ReducedVarArgs

type ReducedVarArgs struct {
	*VarArgs
	// contains filtered or unexported fields
}

ReducedVarArgs represents pythonic variadic args/kwargs but values are reduced (ie. kwargs given as args are accessible by name)

func (*ReducedVarArgs) Error

func (rva *ReducedVarArgs) Error() string

func (*ReducedVarArgs) IsError

func (rva *ReducedVarArgs) IsError() bool

IsError returns true if there was an error on Expect call

type Renderer

type Renderer struct {
	*EvalConfig
	Ctx          *Context
	ValueFactory *ValueFactory
	Template     *Template
	Root         *parse.TemplateNode
	Current      parse.Node
	Out          *strings.Builder
	Trim         *TrimState
}

Renderer is a node visitor in charge of rendering a template.

func NewRenderer

func NewRenderer(ctx *Context, valueVactory *ValueFactory, out *strings.Builder, cfg *EvalConfig, tpl *Template) *Renderer

NewRenderer initialize a new renderer

func (*Renderer) EndTag

func (r *Renderer) EndTag(trim *parse.Trim)

func (*Renderer) Eval

func (r *Renderer) Eval(node parse.Expression) Value

func (*Renderer) Evaluator

func (r *Renderer) Evaluator() *Evaluator

func (*Renderer) Execute

func (r *Renderer) Execute() (err error)

Execute performs the render process by visiting every node and turning them into text.

func (*Renderer) ExecuteWrapper

func (r *Renderer) ExecuteWrapper(wrapper *parse.WrapperNode) (err error)

ExecuteWrapper wraps the parse.Wrapper execution logic

func (*Renderer) Flush

func (r *Renderer) Flush(lstrip bool)

Flush flushes the contents of the buffer to the final output.

func (*Renderer) FlushAndTrim

func (r *Renderer) FlushAndTrim(trim, lstrip bool)

FlushAndTrim trims the contents of the buffer according to the trim policy and flushes it to the final output.

func (*Renderer) Inherit

func (r *Renderer) Inherit() *Renderer

Inherit creates a new sub renderer.

func (*Renderer) LStrip

func (r *Renderer) LStrip()

func (*Renderer) RenderValue

func (r *Renderer) RenderValue(value Value)

RenderValue renders a single value.

func (*Renderer) StartTag

func (r *Renderer) StartTag(trim *parse.Trim, lstrip bool)

func (*Renderer) String

func (r *Renderer) String() string

func (*Renderer) Tag

func (r *Renderer) Tag(trim *parse.Trim, lstrip bool)

func (*Renderer) WriteString

func (r *Renderer) WriteString(txt string) int

WriteString applies the trim policy on the given string and writes it to the buffer.

type Statement

type Statement interface {
	parse.Statement
	Execute(*Renderer, *parse.StatementBlockNode)
}

type StatementSet

type StatementSet map[string]parse.StatementParser

func (StatementSet) Exists

func (ss StatementSet) Exists(name string) bool

Exists returns true if the given test is already registered

func (*StatementSet) MustRegister

func (ss *StatementSet) MustRegister(name string, parser parse.StatementParser)

MustRegister is like Register but panics if the tag cannot be registered.

func (*StatementSet) Register

func (ss *StatementSet) Register(name string, parser parse.StatementParser) error

Register registers a new tag. You usually want to call this function in the tag's init() function: http://golang.org/doc/effective_go.html#init

func (*StatementSet) Replace

func (ss *StatementSet) Replace(name string, parser parse.StatementParser) error

Replace replaces an already registered tag with a new implementation. Use this function with caution since it allows you to change existing tag behavior.

func (*StatementSet) Update

func (ss *StatementSet) Update(other StatementSet) StatementSet

Update updates the statement set with the given statement set.

type StrictUndefinedValue

type StrictUndefinedValue struct {
	UndefinedValue
}

StrictUndefinedValue represents an undefined value that throws an error when accessed.

func (*StrictUndefinedValue) Contains

func (u *StrictUndefinedValue) Contains(other Value) bool

func (*StrictUndefinedValue) EqualValueTo

func (u *StrictUndefinedValue) EqualValueTo(other Value) bool

func (*StrictUndefinedValue) Iterate

func (u *StrictUndefinedValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())

func (*StrictUndefinedValue) IterateOrder

func (u *StrictUndefinedValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), reverse, sorted, caseSensitive bool)

func (*StrictUndefinedValue) Len

func (u *StrictUndefinedValue) Len() int

func (*StrictUndefinedValue) String

func (u *StrictUndefinedValue) String() string

type Template

type Template struct {
	Reader io.Reader
	Source string

	Env *EvalConfig
	// XXX: needed?
	Loader TemplateLoader

	Tokens *parse.Stream
	Parser *parse.Parser

	Root   *parse.TemplateNode
	Macros MacroSet
}

Template is the central template object. It represents a parsed template and is used to evaluate it.

func NewTemplate

func NewTemplate(name, source string, cfg *EvalConfig) (*Template, error)

NewTemplate creates a new template.

func (*Template) Execute

func (tpl *Template) Execute(ctx any) (string, error)

Execute executes the template with the given context and returns the rendered template as a string.

func (*Template) ExecuteBytes

func (tpl *Template) ExecuteBytes(ctx map[string]any) ([]byte, error)

ExecuteBytes executes the template with the given context and returns the rendered template as []byte.

func (*Template) Render

func (tpl *Template) Render(ctx any) (string, error)

Render is a alias for Execute.

type TemplateLoadFn

type TemplateLoadFn func(name string) (*Template, error)

TemplateLoadFn is a function that loads a template by name.

type TemplateLoader

type TemplateLoader interface {
	GetTemplate(string) (*Template, error)
}

TemplateLoader is an interface for loading templates by name.

type TestFunction

type TestFunction func(*Context, Value, *VarArgs) bool

TestFunction is the type test functions must fulfil

type TestSet

type TestSet map[string]TestFunction

TestSet maps test names to their TestFunction handler

func (TestSet) Exists

func (ts TestSet) Exists(name string) bool

Exists returns true if the given test is already registered

func (*TestSet) Register

func (ts *TestSet) Register(name string, fn TestFunction) error

Register registers a new test. If there's already a test with the same name, RegisterTest will panic. You usually want to call this function in the test's init() function: http://golang.org/doc/effective_go.html#init

See http://www.florian-schlachter.de/post/gonja/ for more about writing tests and tags.

func (*TestSet) Replace

func (ts *TestSet) Replace(name string, fn TestFunction) error

Replace replaces an already registered test with a new implementation. Use this function with caution since it allows you to change existing test behavior.

func (*TestSet) Update

func (ts *TestSet) Update(other TestSet) TestSet

type TrimState

type TrimState struct {
	Should      bool
	ShouldBlock bool
	Buffer      *strings.Builder
}

TrimState stores and applies the trim policy.

func (*TrimState) TrimBlocks

func (ts *TrimState) TrimBlocks(r rune) bool

type Undefined

type Undefined interface {
	Value

	// Undefind is a marker method to identify undefined values.
	Undefined()

	// VariableName returns the name of the variable that is undefined.
	VariableName() string

	// Hint returns a hint for the undefined variable.
	Hint() string
}

Undefined is an interface that represents an Undefined value.

func NewChainedStrictUndefinedValue

func NewChainedStrictUndefinedValue(varName, format string, args ...any) Undefined

NewChainedStrictUndefinedValue creates a new ChainedStrictUndefinedValue.

func NewChainedUndefinedValue

func NewChainedUndefinedValue(varName, format string, args ...any) Undefined

NewChainedUndefinedValue creates a new ChainedUndefinedValue.

func NewStrictUndefinedValue

func NewStrictUndefinedValue(varName, format string, args ...any) Undefined

NewStrictUndefinedValue creates a new StrictUndefinedValue.

func NewUndefinedValue

func NewUndefinedValue(name, format string, args ...any) Undefined

NewUndefinedValue creates a new UndefinedValue.

type UndefinedFunc

type UndefinedFunc func(name, hintFormat string, args ...any) Undefined

UndefinedFunc is a function that creates a new Undefined value.

type UndefinedValue

type UndefinedValue struct {
	BaseValue
	// contains filtered or unexported fields
}

UndefinedValue represents an undefined value that renders to an empty string. Most other access methods will throw an error.

func (UndefinedValue) Bool

func (u UndefinedValue) Bool() bool

func (*UndefinedValue) EqualValueTo

func (u *UndefinedValue) EqualValueTo(other Value) bool

func (*UndefinedValue) Escaped

func (*UndefinedValue) Escaped() string

func (UndefinedValue) Float

func (u UndefinedValue) Float() float64

func (UndefinedValue) GetItem

func (u UndefinedValue) GetItem(key any) Value

func (UndefinedValue) Hint

func (u UndefinedValue) Hint() string

Hint returns a hint for the undefined variable.

func (UndefinedValue) Index

func (u UndefinedValue) Index(i int) Value

func (UndefinedValue) Integer

func (u UndefinedValue) Integer() int

func (*UndefinedValue) Items

func (u *UndefinedValue) Items() []*Pair

func (*UndefinedValue) Iterate

func (*UndefinedValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())

func (*UndefinedValue) IterateOrder

func (*UndefinedValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), reverse, sorted, caseSensitive bool)

func (*UndefinedValue) Keys

func (u *UndefinedValue) Keys() ValuesList

func (*UndefinedValue) Len

func (*UndefinedValue) Len() int

func (UndefinedValue) Set

func (u UndefinedValue) Set(key string, value interface{})

func (UndefinedValue) Slice

func (u UndefinedValue) Slice(i, j int) Value

func (*UndefinedValue) String

func (*UndefinedValue) String() string

func (*UndefinedValue) Undefined

func (*UndefinedValue) Undefined()

Undefined is a marker method to identify undefined values.

func (*UndefinedValue) Values

func (u *UndefinedValue) Values() ValuesList

func (UndefinedValue) VariableName

func (u UndefinedValue) VariableName() string

VariableName returns the name of the variable that is undefined.

type Value

type Value interface {
	// IsString returns true if the value is a string, false otherwise.
	IsString() bool

	// IsBool returns true if the value is a boolean, false otherwise.
	IsBool() bool

	// IsFloat returns true if the value is a float, false otherwise.
	IsFloat() bool

	// IsInteger returns true if the value is an integer, false otherwise.
	IsInteger() bool

	// IsNumber returns true if the value is a number (integer or float), false
	// otherwise.
	IsNumber() bool

	// IsList returns true if the value is a list or array, false otherwise.
	IsList() bool

	// IsDict returns true if the value is a dictionary or map, false otherwise.
	IsDict() bool

	// IsNil returns true if the value is nil or null, false otherwise.
	IsNil() bool

	// IsSafe returns true if the value is safe for concurrent use, false
	// otherwise.
	IsSafe() bool

	// IsCallable returns true if the value is a callable function or method,
	// false otherwise.
	IsCallable() bool

	// IsIterable returns true if the value is iterable, false otherwise.
	IsIterable() bool

	// IsSliceable returns true if the value is sliceable, false otherwise.
	IsSliceable() bool

	// Interface returns the underlying value as an interface{}.
	Interface() any

	// ReflectValue returns the underlying reflect value.
	ReflectValue() reflect.Value

	// String returns the string representation of the value.
	String() string

	// Escaped returns the escaped string representation of the value.
	Escaped() string

	// Integer returns the integer representation of the value.
	Integer() int

	// Float returns the float representation of the value.
	Float() float64

	// Bool returns the boolean representation of the value.
	Bool() bool

	// Len returns the length of the value, if it's a list, dictionary, or
	// string.
	Len() int

	// Slice returns a slice of the value, if it's a list or string, from index
	// i to j.
	Slice(i, j int) Value

	// Index returns the value at the given index, if it's a list or string.
	Index(i int) Value

	// Contains returns true if the value contains the given value.
	Contains(other Value) bool

	// Keys returns the keys of the underlying map.
	Keys() ValuesList

	// Values returns the values of the underlying map.
	Values() ValuesList

	// Items returns the key-value pairs of the underlying map.
	Items() []*Pair

	// GetItem returns the value associated with the given key.
	GetItem(key any) Value

	// SetItem sets the value associated with the given key to the given value.
	SetItem(key string, value any)

	// Iterate iterates over the value's items, if it's a list or dictionary,
	// and calls the provided function for each item. If the value is empty, the
	// empty function is called instead.
	Iterate(fn func(idx, count int, key, value Value) (cont bool), empty func())

	// IterateOrder iterates over the value's items, if it's a dictionary, in a
	// specified order, and calls the provided function for each item. If the
	// value is empty, the empty function is called instead. If reverse is true,
	// the items are iterated in reverse order. If sorted is true, the items are
	// sorted by key. If caseSensitive is true, the keys are compared
	// case-sensitively.
	IterateOrder(fn func(idx, count int, key, value Value) (cont bool), empty func(), reverse, sorted, caseSensitive bool)

	// EqualValueTo returns true if the value is equal to the other value, false
	// otherwise.
	EqualValueTo(other Value) bool
}

Value is the interface that all value containers must implement. You can use BaseValue as a base for your own value container implementations.

type ValueFactory

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

ValueFactory is a factory for creating values.

func NewValueFactory

func NewValueFactory(undefined UndefinedFunc, customTypes map[reflect.Type]ValueFunc) *ValueFactory

NewValueFactory creates a new value factory.

func (*ValueFactory) NewUndefined

func (vf *ValueFactory) NewUndefined(name, hintFormat string, args ...any) Undefined

NewUndefined creates a new undefined value.

func (*ValueFactory) SafeValue

func (vf *ValueFactory) SafeValue(value any) Value

SafeValue creates a new Value container from the given value and marks it as safe.

func (*ValueFactory) Value

func (vf *ValueFactory) Value(value any) Value

Value creates a new Value container from the given value.

type ValueFunc

type ValueFunc func(value any, safe bool, valueFactory *ValueFactory) Value

ValueFunc is a function that creates a new value container.

type ValuesList

type ValuesList []Value

ValuesList represents a list of [Value]s.

func (ValuesList) Contains

func (vl ValuesList) Contains(value Value) bool

Contains reports whether a value is within vl.

func (ValuesList) Len

func (vl ValuesList) Len() int

Len is the number of elements in the collection.

func (ValuesList) Less

func (vl ValuesList) Less(i, j int) bool

Less reports whether the element with index i must sort before the element with index j.

func (ValuesList) String

func (vl ValuesList) String() string

String returns a string representation of vl in the form "['value1', 'value2']".

func (ValuesList) Swap

func (vl ValuesList) Swap(i, j int)

Swap swaps the elements with indexes i and j.

type VarArgs

type VarArgs struct {
	Args         []Value
	Kwargs       []KVPair
	ValueFactory *ValueFactory
}

VarArgs represents pythonic variadic args/kwargs.

func NewVarArgs

func NewVarArgs(valueFactory *ValueFactory) *VarArgs

NewVarArgs creates a new VarArgs.

func NewVarArgsWithValues

func NewVarArgsWithValues(valueFactory *ValueFactory, args []Value, kwargs []KVPair) *VarArgs

NewVarArgsWithValues creates a new VarArgs from a list of Values.

func (*VarArgs) Expect

func (va *VarArgs) Expect(args int, kwargs []*Kwarg) *ReducedVarArgs

Expect validates VarArgs against an expected signature

func (*VarArgs) ExpectArgs

func (va *VarArgs) ExpectArgs(args int) *ReducedVarArgs

ExpectArgs ensures VarArgs receive only arguments

func (*VarArgs) ExpectKwArgs

func (va *VarArgs) ExpectKwArgs(kwargs []*Kwarg) *ReducedVarArgs

ExpectKwArgs allow to specify optionally expected KwArgs

func (*VarArgs) ExpectNothing

func (va *VarArgs) ExpectNothing() *ReducedVarArgs

ExpectNothing ensures VarArgs does not receive any argument

func (*VarArgs) First

func (va *VarArgs) First() Value

First returns the first argument or nil AsValue.

func (*VarArgs) GetKwarg

func (va *VarArgs) GetKwarg(key string) Value

GetKwarg gets a keyword argument. It panics if the keyword argument does not exist. Make sure to define all the expected keyword arguments before calling this.

func (*VarArgs) HasKwarg

func (va *VarArgs) HasKwarg(key string) bool

HasKwarg returns true if the keyword argument exists.

func (*VarArgs) SetKwarg

func (va *VarArgs) SetKwarg(key string, value Value)

SetKwarg sets a keyword argument

func (*VarArgs) String

func (va *VarArgs) String() string

String returns a string representation of the variables arguments.

Jump to

Keyboard shortcuts

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