Documentation
¶
Overview ¶
Package value provides a Patcher that uses interfaces to allow custom types that can be represented as standard go values to be used more easily in expressions.
Index ¶
- Variables
- type AnyValuer
- type ArrayValuer
- type BoolValuer
- type DurationValuer
- type Float32Valuer
- type Float64Valuer
- type Int16Valuer
- type Int32Valuer
- type Int64Valuer
- type Int8Valuer
- type IntValuer
- type MapValuer
- type StringValuer
- type TimeValuer
- type Uint16Valuer
- type Uint32Valuer
- type Uint64Valuer
- type Uint8Valuer
- type UintValuer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ValueGetter = expr.Option(func(c *conf.Config) { c.Visitors = append(c.Visitors, patcher{}) getValueFunc(c) })
ValueGetter is a Patcher that allows custom types to be represented as standard go values for use with expr. It also adds the `$patcher_value_getter` function to the program for efficiently calling matching interfaces.
The purpose of this Patcher is to make it seemless to use custom types in expressions without the need to first convert them to standard go values. It may also facilitate using already existing structs or maps as environments when they contain compatabile types.
An example usage may be modeling a database record with columns that have varying data types and constraints. In such an example you may have custom types that, beyond storing a simple value, such as an integer, may contain metadata such as column type and if a value is specifically a NULL value.
Use it directly as an Option to expr.Compile()
Functions ¶
This section is empty.
Types ¶
type AnyValuer ¶
type AnyValuer interface {
AsAny() any
}
A AnyValuer provides a generic function for a custom type to return standard go values. It allows for returning a `nil` value but does not provide any type checking at expression compile.
A custom type may implement both AnyValuer and a type specific interface to enable both compile time checking and the ability to return a `nil` value.
Example ¶
package main import ( "fmt" "github.com/expr-lang/expr" "github.com/expr-lang/expr/patcher/value" "github.com/expr-lang/expr/vm" ) type myInt struct { Int int } func (v *myInt) AsInt() int { return v.Int } func (v *myInt) AsAny() any { return v.Int } func main() { env := make(map[string]any) env["ValueOne"] = &myInt{1} env["ValueTwo"] = &myInt{2} program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), value.ValueGetter) if err != nil { panic(err) } out, err := vm.Run(program, env) if err != nil { panic(err) } fmt.Println(out) }
Output: 3
type ArrayValuer ¶
type ArrayValuer interface {
AsArray() []any
}
type BoolValuer ¶
type BoolValuer interface {
AsBool() bool
}
type DurationValuer ¶
type Float32Valuer ¶
type Float32Valuer interface {
AsFloat32() float32
}
type Float64Valuer ¶
type Float64Valuer interface {
AsFloat64() float64
}
type Int16Valuer ¶
type Int16Valuer interface {
AsInt16() int16
}
type Int32Valuer ¶
type Int32Valuer interface {
AsInt32() int32
}
type Int64Valuer ¶
type Int64Valuer interface {
AsInt64() int64
}
type Int8Valuer ¶
type Int8Valuer interface {
AsInt8() int8
}
type StringValuer ¶
type StringValuer interface {
AsString() string
}
type TimeValuer ¶
type Uint16Valuer ¶
type Uint16Valuer interface {
AsUint16() uint16
}
type Uint32Valuer ¶
type Uint32Valuer interface {
AsUint32() uint32
}
type Uint64Valuer ¶
type Uint64Valuer interface {
AsUint64() uint64
}
type Uint8Valuer ¶
type Uint8Valuer interface {
AsUint8() uint8
}
type UintValuer ¶
type UintValuer interface {
AsUint() uint
}