Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInvalidKind occurs when you call an inappropriate method for a given kind.
	ErrInvalidKind = errors.New("invalid kind")

	// ErrNotRepresentable occurs when a type is encountered that is not supported by the language.
	ErrNotRepresentable = errors.New("type cannot be represented")

	// ErrUntypedNil occurs when an untyped nil is used inappropriately.
	ErrUntypedNil = errors.New("untyped nil value")
)

Functions

func Eval

func Eval(resolver Resolver, expr string) (interface{}, error)

    Eval returns the result of evaluating the provided expression.

    func EvalProgram

    func EvalProgram(resolver Resolver, program *Program) (v interface{}, err error)

      EvalProgram returns the result of executing the program with the given resolver.

      func TypeEqual

      func TypeEqual(a, b Type) bool

        TypeEqual returns true if the two types are equal.

        Types

        type ArrayType

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

          ArrayType is the type of array-like values.

          func NewArrayType

          func NewArrayType(count int, elem Type) *ArrayType

            NewArrayType returns a new array type.

            func (ArrayType) Elem

            func (t ArrayType) Elem() Type

              Elem is the type of element in the array.

              func (ArrayType) Kind

              func (ArrayType) Kind() Kind

                Kind implements Type.

                func (ArrayType) Len

                func (t ArrayType) Len() int

                  Len is the length of the array.

                  func (ArrayType) String

                  func (t ArrayType) String() string

                    String implements Type.

                    type ConversionError

                    type ConversionError struct {
                    	From Type
                    	To   Type
                    }

                      ConversionError is returned when an invalid type conversion is attempted.

                      func (ConversionError) Error

                      func (e ConversionError) Error() string

                      type Field

                      type Field struct {
                      	Name string
                      	Type Type
                      }

                        Field represents a struct field.

                        type FuncType

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

                          FuncType is the type of function values.

                          func NewFuncType

                          func NewFuncType(in []Type, out []Type, variadic bool) *FuncType

                            NewFuncType returns a new function type.

                            func (FuncType) In

                            func (t FuncType) In(i int) Type

                              In gets the nth input parameter.

                              func (FuncType) IsVariadic

                              func (t FuncType) IsVariadic() bool

                                IsVariadic returns true for variadic functions.

                                func (FuncType) Kind

                                func (FuncType) Kind() Kind

                                  Kind implements Type.

                                  func (FuncType) NumIn

                                  func (t FuncType) NumIn() int

                                    NumIn returns the number of input parameters.

                                    func (FuncType) NumOut

                                    func (t FuncType) NumOut() int

                                      NumOut returns the number of output parameters.

                                      func (FuncType) Out

                                      func (t FuncType) Out(i int) Type

                                        Out gets the nth output parameter.

                                        func (FuncType) String

                                        func (t FuncType) String() string

                                          String implements Type.

                                          type InvalidOpError

                                          type InvalidOpError struct {
                                          	Op string
                                          	V  Value
                                          }

                                            InvalidOpError is returned when an attempt is made to perform an operation on a type that is not supported.

                                            func (InvalidOpError) Error

                                            func (e InvalidOpError) Error() string

                                            type Kind

                                            type Kind int

                                              Kind is the most basic type descriptor.

                                              const (
                                              	Invalid Kind = iota
                                              
                                              	// Primitives
                                              	Bool
                                              	Int
                                              	Int8
                                              	Int16
                                              	Int32
                                              	Int64
                                              	Uint
                                              	Uint8
                                              	Uint16
                                              	Uint32
                                              	Uint64
                                              	Uintptr
                                              	Float32
                                              	Float64
                                              	String
                                              
                                              	// Untyped constants
                                              	UntypedBool
                                              	UntypedInt
                                              	UntypedFloat
                                              	UntypedNil
                                              
                                              	// Composite types
                                              	Array
                                              	Slice
                                              	Struct
                                              	Map
                                              	Ptr
                                              	Func
                                              	Pkg
                                              )

                                                Enumeration of valid kinds of types.

                                                type MapResolver

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

                                                  MapResolver resolves map keys.

                                                  func NewMapResolver

                                                  func NewMapResolver(m map[string]Value) *MapResolver

                                                    NewMapResolver creates a new struct resolver.

                                                    func (*MapResolver) Resolve

                                                    func (r *MapResolver) Resolve(ident string) Value

                                                      Resolve implements Resolver.

                                                      type MapType

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

                                                        MapType is the type of maps.

                                                        func (MapType) Key

                                                        func (t MapType) Key() Type

                                                          Key is the type of the map's keys.

                                                          func (MapType) Kind

                                                          func (MapType) Kind() Kind

                                                            Kind implements Type.

                                                            func (MapType) String

                                                            func (t MapType) String() string

                                                              String implements Type.

                                                              func (MapType) Value

                                                              func (t MapType) Value() Type

                                                                Value is the type of the map's values.

                                                                type MapTypeResolver

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

                                                                  MapTypeResolver resolves map keys.

                                                                  func NewMapTypeResolver

                                                                  func NewMapTypeResolver(m map[string]Type) *MapTypeResolver

                                                                    NewMapTypeResolver creates a new struct resolver.

                                                                    func (*MapTypeResolver) TypeResolve

                                                                    func (r *MapTypeResolver) TypeResolve(ident string) Type

                                                                      TypeResolve implements TypeResolver.

                                                                      type MetaResolver

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

                                                                        MetaResolver runs multiple resolvers serially.

                                                                        func NewMetaResolver

                                                                        func NewMetaResolver() *MetaResolver

                                                                          NewMetaResolver creates a new meta resolver.

                                                                          func (*MetaResolver) AddResolver

                                                                          func (r *MetaResolver) AddResolver(n Resolver)

                                                                            AddResolver adds a new resolver below other resolvers.

                                                                            func (*MetaResolver) Resolve

                                                                            func (r *MetaResolver) Resolve(ident string) Value

                                                                              Resolve implements Resolver.

                                                                              type MetaTypeResolver

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

                                                                                MetaTypeResolver runs multiple type resolvers serially.

                                                                                func NewMetaTypeResolver

                                                                                func NewMetaTypeResolver() *MetaTypeResolver

                                                                                  NewMetaTypeResolver creates a new meta type resolver.

                                                                                  func (*MetaTypeResolver) AddResolver

                                                                                  func (r *MetaTypeResolver) AddResolver(n TypeResolver)

                                                                                    AddResolver adds a new resolver below other resolvers.

                                                                                    func (*MetaTypeResolver) TypeResolve

                                                                                    func (r *MetaTypeResolver) TypeResolve(ident string) Type

                                                                                      TypeResolve implements TypeResolver.

                                                                                      type NoSuchFieldError

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

                                                                                        NoSuchFieldError is returned when an unknown field is accessed.

                                                                                        func (NoSuchFieldError) Error

                                                                                        func (err NoSuchFieldError) Error() string

                                                                                        type Package

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

                                                                                          Package represents a package value.

                                                                                          func NewPackage

                                                                                          func NewPackage(symbols map[string]Value) Package

                                                                                            NewPackage creates a new package.

                                                                                            func (Package) Symbol

                                                                                            func (p Package) Symbol(ident string) Value

                                                                                              Symbol returns a symbol, or nil if the symbol doesn't exist.

                                                                                              func (Package) Type

                                                                                              func (p Package) Type() Type

                                                                                                Type returns the type for this package.

                                                                                                type PackageType

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

                                                                                                  PackageType is the type of a package.

                                                                                                  func NewPackageType

                                                                                                  func NewPackageType(symbols map[string]Type) *PackageType

                                                                                                    NewPackageType returns a new package with the given symbols.

                                                                                                    func (PackageType) Kind

                                                                                                    func (PackageType) Kind() Kind

                                                                                                      Kind implements Type.

                                                                                                      func (PackageType) String

                                                                                                      func (PackageType) String() string

                                                                                                        String implements Type.

                                                                                                        func (PackageType) Symbol

                                                                                                        func (t PackageType) Symbol(ident string) Type

                                                                                                          Symbol returns a symbol by the given name, or nil if none could be found.

                                                                                                          type PrimitiveType

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

                                                                                                            PrimitiveType is the type of primitives.

                                                                                                            func (PrimitiveType) Kind

                                                                                                            func (t PrimitiveType) Kind() Kind

                                                                                                              Kind implements Type.

                                                                                                              func (PrimitiveType) String

                                                                                                              func (t PrimitiveType) String() string

                                                                                                                String implements Type.

                                                                                                                type Program

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

                                                                                                                  Program represents a parsed expression.

                                                                                                                  func Parse

                                                                                                                  func Parse(r io.RuneScanner) *Program

                                                                                                                    Parse parses an expression into a program.

                                                                                                                    func ParseString

                                                                                                                    func ParseString(s string) *Program

                                                                                                                      ParseString parses an expression from a string.

                                                                                                                      type PtrType

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

                                                                                                                        PtrType is the type of pointers.

                                                                                                                        func NewPtrType

                                                                                                                        func NewPtrType(elem Type) *PtrType

                                                                                                                          NewPtrType returns a new pointer type.

                                                                                                                          func (PtrType) Elem

                                                                                                                          func (t PtrType) Elem() Type

                                                                                                                            Elem returns the element being pointed to by the pointer.

                                                                                                                            func (PtrType) Kind

                                                                                                                            func (PtrType) Kind() Kind

                                                                                                                              Kind implements Type.

                                                                                                                              func (PtrType) String

                                                                                                                              func (t PtrType) String() string

                                                                                                                                String implements Type.

                                                                                                                                type ReferenceError

                                                                                                                                type ReferenceError struct{}

                                                                                                                                  ReferenceError is returned when it is not possible to take the address of a value.

                                                                                                                                  func (ReferenceError) Error

                                                                                                                                  func (e ReferenceError) Error() string

                                                                                                                                  type Resolver

                                                                                                                                  type Resolver interface {
                                                                                                                                  	Resolve(ident string) Value
                                                                                                                                  }

                                                                                                                                    Resolver resolves runtime values.

                                                                                                                                    type SliceType

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

                                                                                                                                      SliceType is the type of array-like values.

                                                                                                                                      func NewSliceType

                                                                                                                                      func NewSliceType(elem Type) *SliceType

                                                                                                                                        NewSliceType returns a new array type.

                                                                                                                                        func (SliceType) Elem

                                                                                                                                        func (t SliceType) Elem() Type

                                                                                                                                          Elem is the type of element in the slice.

                                                                                                                                          func (SliceType) Kind

                                                                                                                                          func (SliceType) Kind() Kind

                                                                                                                                            Kind implements Type.

                                                                                                                                            func (SliceType) String

                                                                                                                                            func (t SliceType) String() string

                                                                                                                                              String implements Type.

                                                                                                                                              type StructResolver

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

                                                                                                                                                StructResolver resolves struct fields.

                                                                                                                                                func NewStructResolver

                                                                                                                                                func NewStructResolver(s reflect.Value) *StructResolver

                                                                                                                                                  NewStructResolver creates a new struct resolver.

                                                                                                                                                  func (*StructResolver) Resolve

                                                                                                                                                  func (r *StructResolver) Resolve(ident string) Value

                                                                                                                                                    Resolve implements Resolver.

                                                                                                                                                    type StructType

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

                                                                                                                                                      StructType is the type of struct values.

                                                                                                                                                      func NewStructType

                                                                                                                                                      func NewStructType(fields []Field) *StructType

                                                                                                                                                        NewStructType returns a new struct type.

                                                                                                                                                        func (StructType) Field

                                                                                                                                                        func (t StructType) Field(i int) Field

                                                                                                                                                          Field returns the nth field in the struct.

                                                                                                                                                          func (StructType) FieldByName

                                                                                                                                                          func (t StructType) FieldByName(name string) (Field, bool)

                                                                                                                                                            FieldByName returns the field with the given name.

                                                                                                                                                            func (StructType) Kind

                                                                                                                                                            func (StructType) Kind() Kind

                                                                                                                                                              Kind implements Type.

                                                                                                                                                              func (StructType) NumFields

                                                                                                                                                              func (t StructType) NumFields() int

                                                                                                                                                                NumFields returns the number of fields in the struct.

                                                                                                                                                                func (StructType) String

                                                                                                                                                                func (t StructType) String() string

                                                                                                                                                                  String implements Type.

                                                                                                                                                                  type StructTypeResolver

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

                                                                                                                                                                    StructTypeResolver resolves types of struct fields.

                                                                                                                                                                    func NewStructTypeResolver

                                                                                                                                                                    func NewStructTypeResolver(s interface{}) *StructTypeResolver

                                                                                                                                                                      NewStructTypeResolver creates a new struct type resolver.

                                                                                                                                                                      func (*StructTypeResolver) TypeResolve

                                                                                                                                                                      func (r *StructTypeResolver) TypeResolve(ident string) Type

                                                                                                                                                                        TypeResolve implements TypeResolver.

                                                                                                                                                                        type Type

                                                                                                                                                                        type Type interface {
                                                                                                                                                                        	Kind() Kind
                                                                                                                                                                        	String() string
                                                                                                                                                                        }

                                                                                                                                                                          Type is the representation of an expr type.

                                                                                                                                                                          func NewLiteralType

                                                                                                                                                                          func NewLiteralType(k Kind) Type

                                                                                                                                                                            NewLiteralType returns a new primitive type.

                                                                                                                                                                            func NewMapType

                                                                                                                                                                            func NewMapType(key Type, val Type) Type

                                                                                                                                                                              NewMapType returns a new map type.

                                                                                                                                                                              func NewPrimitiveType

                                                                                                                                                                              func NewPrimitiveType(k Kind) Type

                                                                                                                                                                                NewPrimitiveType returns a new primitive type.

                                                                                                                                                                                func TypeOf

                                                                                                                                                                                func TypeOf(i interface{}) Type

                                                                                                                                                                                  TypeOf returns the type of a runtime value.

                                                                                                                                                                                  type TypeResolver

                                                                                                                                                                                  type TypeResolver interface {
                                                                                                                                                                                  	TypeResolve(ident string) Type
                                                                                                                                                                                  }

                                                                                                                                                                                    TypeResolver resolves types.

                                                                                                                                                                                    type TypeResolverAdapter

                                                                                                                                                                                    type TypeResolverAdapter struct {
                                                                                                                                                                                    	Resolver
                                                                                                                                                                                    }

                                                                                                                                                                                      TypeResolverAdapter adapts a runtime resolver to a type resolver by taking types of values retrieve from Resolve.

                                                                                                                                                                                      func NewTypeResolverAdapter

                                                                                                                                                                                      func NewTypeResolverAdapter(r Resolver) *TypeResolverAdapter

                                                                                                                                                                                        NewTypeResolverAdapter creates a new TypeResolverAdapter from a resolver.

                                                                                                                                                                                        func (*TypeResolverAdapter) TypeResolve

                                                                                                                                                                                        func (r *TypeResolverAdapter) TypeResolve(ident string) Type

                                                                                                                                                                                          TypeResolve implements TypeResolver.

                                                                                                                                                                                          type Value

                                                                                                                                                                                          type Value interface {
                                                                                                                                                                                          	Type() Type
                                                                                                                                                                                          	Value() reflect.Value
                                                                                                                                                                                          	RawValue() interface{}
                                                                                                                                                                                          
                                                                                                                                                                                          	Negate() Value
                                                                                                                                                                                          	Not() Value
                                                                                                                                                                                          	BitNot() Value
                                                                                                                                                                                          	Deref() Value
                                                                                                                                                                                          	Ref() Value
                                                                                                                                                                                          	Dot(ident string) Value
                                                                                                                                                                                          
                                                                                                                                                                                          	LogicalOr(rhs Value) Value
                                                                                                                                                                                          	LogicalAnd(rhs Value) Value
                                                                                                                                                                                          	Equal(rhs Value) Value
                                                                                                                                                                                          	NotEqual(rhs Value) Value
                                                                                                                                                                                          	Lesser(rhs Value) Value
                                                                                                                                                                                          	LesserEqual(rhs Value) Value
                                                                                                                                                                                          	Greater(rhs Value) Value
                                                                                                                                                                                          	GreaterEqual(rhs Value) Value
                                                                                                                                                                                          	Add(rhs Value) Value
                                                                                                                                                                                          	Sub(rhs Value) Value
                                                                                                                                                                                          	Or(rhs Value) Value
                                                                                                                                                                                          	Xor(rhs Value) Value
                                                                                                                                                                                          	Mul(rhs Value) Value
                                                                                                                                                                                          	Div(rhs Value) Value
                                                                                                                                                                                          	Rem(rhs Value) Value
                                                                                                                                                                                          	Lsh(rhs Value) Value
                                                                                                                                                                                          	Rsh(rhs Value) Value
                                                                                                                                                                                          	And(rhs Value) Value
                                                                                                                                                                                          	AndNot(rhs Value) Value
                                                                                                                                                                                          	Index(rhs Value) Value
                                                                                                                                                                                          	Call(in []Value) Value
                                                                                                                                                                                          }

                                                                                                                                                                                            Value represents a value at runtime.

                                                                                                                                                                                            func ValueOf

                                                                                                                                                                                            func ValueOf(i interface{}) Value

                                                                                                                                                                                              ValueOf returns a Value for the given runtime value.