Documentation ¶
Index ¶
- Variables
- func NewFields[T any](objt *Object[T]) map[string]*types.FieldType
- func RegisterIdent(tp *TypeProvider, name string, value ref.Val)
- func RegisterObject[T any](ta TypeAdapter, tp *TypeProvider, objt *Object[T], t *types.Type, ...)
- func RegisterStructType(tp *TypeProvider, name string, fields map[string]*types.FieldType)
- func RegisterType(tp *TypeProvider, t *types.Type)
- type Object
- type TypeAdapter
- type TypeProvider
- func (TypeProvider) EnumValue(enumName string) ref.Val
- func (tp *TypeProvider) FindIdent(identName string) (ref.Val, bool)
- func (tp *TypeProvider) FindStructFieldNames(structType string) ([]string, bool)
- func (tp *TypeProvider) FindStructFieldType(messageType, fieldName string) (*types.FieldType, bool)
- func (tp *TypeProvider) FindStructType(structType string) (*types.Type, bool)
- func (TypeProvider) NewValue(typeName string, fields map[string]ref.Val) ref.Val
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func NewFields ¶
NewFields returns a map[string]*types.FieldType for the given object type wrapping a Go struct pointer value.
func RegisterIdent ¶
func RegisterIdent(tp *TypeProvider, name string, value ref.Val)
func RegisterObject ¶
func RegisterObject[T any](ta TypeAdapter, tp *TypeProvider, objt *Object[T], t *types.Type, fields map[string]*types.FieldType)
RegisterObject registers a CEL value wrapper for a Go value with the type adapter and type provider, which are provided by the caller when constructing a CEL environment.
func RegisterStructType ¶
func RegisterStructType(tp *TypeProvider, name string, fields map[string]*types.FieldType)
func RegisterType ¶
func RegisterType(tp *TypeProvider, t *types.Type)
Types ¶
type Object ¶
type Object[T any] struct { Raw T }
Object is a CEL value wrapper for a Go value that can be used in expressions.
func NewObject ¶
NewObject creates a new CEL value wrapper for a Go value that can be used in expressions.
Example ¶
package main import ( "fmt" "github.com/google/cel-go/cel" "github.com/google/cel-go/common/types" "github.com/google/cel-go/common/types/ref" "github.com/picatz/xcel" ) func main() { type Person struct { Name string Age int } person := &Person{ Name: "test", Age: -1, } ta, tp := xcel.NewTypeAdapter(), xcel.NewTypeProvider() obj, typ := xcel.NewObject(person) xcel.RegisterObject(ta, tp, obj, typ, map[string]*types.FieldType{ "name": { Type: types.StringType, IsSet: ref.FieldTester(func(target any) bool { x := target.(*xcel.Object[*Person]) return x.Raw != nil && x.Raw.Name != "" }), GetFrom: ref.FieldGetter(func(target any) (any, error) { x := target.(*xcel.Object[*Person]) if x.Raw == nil { return nil, fmt.Errorf("celval: object is nil") } return x.Raw.Name, nil }), }, "age": { Type: types.IntType, IsSet: ref.FieldTester(func(target any) bool { x := target.(*xcel.Object[*Person]) return x.Raw != nil && x.Raw.Age >= 0 }), GetFrom: ref.FieldGetter(func(target any) (any, error) { x := target.(*xcel.Object[*Person]) if x.Raw == nil { return nil, fmt.Errorf("celval: object is nil") } return x.Raw.Age, nil }), }, }) env, _ := cel.NewEnv( cel.Types(typ), cel.Variable("obj", typ), cel.CustomTypeAdapter(ta), cel.CustomTypeProvider(tp), ) ast, _ := env.Compile("obj.name == 'test' && obj.age > 0") prg, _ := env.Program(ast) out, _, _ := prg.Eval(map[string]any{ "obj": obj, }) fmt.Println(out.Value()) }
Output: false
func (*Object[T]) ConvertToNative ¶
ConvertToNative converts the CEL value wrapper to a native Go value.
func (*Object[T]) ConvertToType ¶
ConvertToType converts the CEL value wrapper to a CEL value of the specified type.
func (*Object[T]) Equal ¶
Equal returns true if the CEL value wrapper is equal to the specified CEL value.
type TypeAdapter ¶
func NewTypeAdapter ¶
func NewTypeAdapter() TypeAdapter
func (TypeAdapter) NativeToValue ¶
func (ta TypeAdapter) NativeToValue(value any) ref.Val
type TypeProvider ¶
type TypeProvider struct { Idents map[string]ref.Val Types map[string]*types.Type Stucts map[string]map[string]*types.FieldType StructFieldTypes map[string]map[string]*types.FieldType }
func NewTypeProvider ¶
func NewTypeProvider() *TypeProvider
func (*TypeProvider) FindIdent ¶
func (tp *TypeProvider) FindIdent(identName string) (ref.Val, bool)
func (*TypeProvider) FindStructFieldNames ¶
func (tp *TypeProvider) FindStructFieldNames(structType string) ([]string, bool)
func (*TypeProvider) FindStructFieldType ¶
func (tp *TypeProvider) FindStructFieldType(messageType, fieldName string) (*types.FieldType, bool)
func (*TypeProvider) FindStructType ¶
func (tp *TypeProvider) FindStructType(structType string) (*types.Type, bool)