Documentation
¶
Overview ¶
Package values is an internal package that defines methods such as sorting, comparison, and type conversion, that apply to interface types.
It is similar to, and makes heavy use of, the reflect package.
Since the intent is to provide runtime services for the Liquid expression interpreter, this package does not implement "generic" generics. It attempts to implement Liquid semantics (which are largely Ruby semantics).
Index ¶
- func Call(fn reflect.Value, args []any) (any, error)
- func Convert(value any, typ reflect.Type) (any, error)
- func Equal(a, b any) bool
- func IsBlank(value any) bool
- func IsEmpty(value any) bool
- func Length(value any) int
- func Less(a, b any) bool
- func MustConvert(value any, t reflect.Type) any
- func MustConvertItem(item any, array any) any
- func NormalizeNumber(v any) any
- func ParseDate(s string) (time.Time, error)
- func Sort(data []any)
- func SortByProperty(data []any, key string, nilFirst bool)
- func ToLiquid(value any) any
- func Truthy(value any) bool
- type CallParityError
- type ContextAccess
- type ContextSetter
- type LiquidSentinel
- type Range
- type SafeValue
- type TypeError
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Call ¶
Call applies a function to arguments, converting them as necessary.
The conversion follows Liquid (Ruby?) semantics, which are more aggressive than Go conversion.
The function should return one or two values; the second value, if present, should be an error.
func Convert ¶
Convert value to the type. This is a more aggressive conversion, that will recursively create new map and slice values as necessary. It doesn't handle circular references. #nosec G115
func IsBlank ¶
IsBlank returns true if value is nil, false, an empty or whitespace-only string, an empty array, or an empty map — matching Liquid blank? semantics. Uses reflect.Kind so that defined types (e.g. type MyBool bool, type MyStr string) are handled the same way as their underlying built-in types.
func IsEmpty ¶
IsEmpty returns true if value is an empty string, empty array, or empty map according to Liquid semantics. nil and false are NOT empty.
func Length ¶
Length returns the length of a string or array. In keeping with Liquid semantics, and contra Go, it does not return the size of a map.
func MustConvert ¶
MustConvert is like Convert, but panics if conversion fails.
func MustConvertItem ¶
MustConvertItem converts item to conform to the type array's element, else panics. Unlike MustConvert, the second argument is a value not a type.
func NormalizeNumber ¶
NormalizeNumber converts any Go numeric scalar to one of three canonical types: int64 (signed integers), uint64 (unsigned integers), or float64. Non-numeric values are returned unchanged.
func Sort ¶
func Sort(data []any)
Sort any []any value. nil elements are sorted last (matching Ruby Liquid).
func SortByProperty ¶
SortByProperty sorts maps on their key indices.
func Truthy ¶ added in v1.1.0
Truthy returns true if value is truthy in Liquid semantics. Only nil and false (or defined bool types whose value is false) are falsy. This is the canonical truthiness gate for all condition evaluation. Uses reflect.Kind so defined types (e.g. type MyBool bool) work correctly.
Types ¶
type CallParityError ¶
type CallParityError struct{ NumArgs, NumParams int }
A CallParityError is a mismatch between the argument and parameter counts.
func (*CallParityError) Error ¶
func (e *CallParityError) Error() string
type ContextAccess ¶
ContextAccess is the minimal interface injected into drops that implement ContextSetter. It provides read/write access to the current rendering scope. Mirrors Ruby Liquid's Context object (limited to variable bindings).
type ContextSetter ¶
type ContextSetter interface {
SetContext(ctx ContextAccess)
}
ContextSetter is an optional interface for drop types that want to receive the current rendering context when they are looked up from the variable scope. SetContext is called by the renderer each time the drop is accessed. This mirrors Ruby Liquid's context= setter.
type LiquidSentinel ¶
type LiquidSentinel interface {
// contains filtered or unexported methods
}
LiquidSentinel is implemented exclusively by EmptyDrop and BlankDrop. Tagging these special singletons with this interface lets the expression evaluator preserve their identity through the evaluation pipeline (instead of discarding it via .Interface()), enabling correct case/when comparisons.
The unexported method prevents external packages from accidentally implementing this interface (sealed interface pattern).
type Range ¶
type Range struct {
// contains filtered or unexported fields
}
A Range is the range of integers from b to e inclusive.
type SafeValue ¶
type SafeValue struct {
Value interface{}
}
SafeValue is a wrapped interface{} to mark it as being safe so that auto-escape is not applied. It is used by the 'safe' filter which is added when (*Engine).SetAutoEscapeReplacer() is called.
type Value ¶
type Value interface {
// Value retrieval
Interface() any
Int() int
// Comparison
Equal(Value) bool
Less(Value) bool
Contains(Value) bool
IndexValue(Value) Value
PropertyValue(Value) Value
// Predicate
Test() bool
}
A Value is a Liquid runtime value.
var BlankDrop Value = &blankDropValue{}
BlankDrop is the singleton for the `blank` keyword. After PRE-A lands, the scanner will resolve the `blank` literal directly to this value instead of treating it as an undefined variable.
var EmptyDrop Value = &emptyDropValue{}
EmptyDrop is the singleton for the `empty` keyword. After PRE-A lands, the scanner will resolve the `empty` literal directly to this value instead of treating it as an undefined variable.