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 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 objt and its type with the given adapter and provider. It derives field metadata from reflection (optionally overlaid by fields), registers the struct type, and registers reachable named nested struct types so nested field access type-checks at compile time.
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 wraps a Go value for use as a CEL object. The wrapper type is used as the CEL object type so member functions dispatch to the wrapper.
func NewObject ¶
NewObject returns a CEL wrapper for val and its CEL object type.
Example ¶
package main import ( "fmt" "github.com/google/cel-go/cel" "github.com/google/cel-go/common/types" "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: func(target any) bool { x := target.(*xcel.Object[*Person]) return x.Raw != nil && x.Raw.Name != "" }, GetFrom: 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: func(target any) bool { x := target.(*xcel.Object[*Person]) return x.Raw != nil && x.Raw.Age >= 0 }, GetFrom: 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 returns the underlying Go value when typeDesc matches the wrapped type.
func (*Object[T]) ConvertToType ¶
ConvertToType implements ref.Val.ConvertToType for the wrapper.
func (*Object[T]) Equal ¶
Equal reports whether other is an *Object[T] with an equal underlying 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 Structs 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)