Documentation ¶
Index ¶
- Variables
- func Compile[CE any](e *Expr, source CompileSource[CE]) (CE, error)
- func TypeOf[T any]() reflect.Type
- type CompileSource
- type CompileSourceLookup
- type Compiler
- type Expr
- type Options
- type Parameter
- type ParseError
- type Position
- type Reflect
- type ReflectCompiled
- type ReflectConversion
- type ReflectOptions
- type System
- type SystemError
- type Type
- type TypeCompilers
- type TypeName
- type Value
- type ValueCompilers
Constants ¶
This section is empty.
Variables ¶
var ErrNoExpression = NewParseError(nil, "undefined expression")
No expression was passed to the parse function.
var ErrNoRoot = NewParseError(nil, "undefined root type")
No root type was specified in the options for parsing.
var ErrNoTypes = NewParseError(nil, "undefined types")
No types are defined in the system.
Functions ¶
Types ¶
type CompileSource ¶
type CompileSource[CE any] interface { // Returns the initial compiled expression value. This is passed to the compiler functions for the // first expressions in a chain. GetInitial(e *Expr) (CE, error) // Returns a compiled value for a constant expression. GetConstantCompiled(e *Expr, root *Type, previous CE, arguments []CE) (CE, error) // Returns a compiler for a value expression. GetValueCompiler(e *Expr, root *Type, previous CE) (Compiler[CE], error) }
A helper to the compile function.
type CompileSourceLookup ¶
type CompileSourceLookup[CE any] struct { // The initial compiled expression value. This is passed to the compiler functions for the // first expressions in a chain. Initial CE // Compilers for each type and their values. TypeCompilers TypeCompilers[CE] // A compiler for a constant expression. ConstantCompiler Compiler[CE] }
A CompileSource implementation where compilers are looked up based on type->value.
func (CompileSourceLookup[CE]) GetConstantCompiled ¶
func (csl CompileSourceLookup[CE]) GetConstantCompiled(e *Expr, root *Type, previous CE, arguments []CE) (CE, error)
func (CompileSourceLookup[CE]) GetInitial ¶
func (csl CompileSourceLookup[CE]) GetInitial(e *Expr) (CE, error)
func (CompileSourceLookup[CE]) GetValueCompiler ¶
func (csl CompileSourceLookup[CE]) GetValueCompiler(e *Expr, root *Type, previous CE) (Compiler[CE], error)
type Compiler ¶
A compiler is a function that is given an expression, the root type, a previously compiled expression (CE), argument CEs, and returns a CE for the given expression.
type Expr ¶
type Expr struct { // The string parsed from the expression input. Token string // The start position of the expression in the input. Start Position // The end position of the expression in the input. End Position // If this expression is a constant value and not a value. Constant bool // The parsed value if this expression is a constant. Parsed any // The value this expression is in the parent type. Value *Value // The parent type if any. If prev is nil this represents the root type. ParentType *Type // The type of this value/constant. Type *Type // The arguments to pass as the parameters to the value. Arguments []*Expr // The next expression in the chain on the result of this one. Next *Expr // The previous expression in the chain or nil. When nil this is either a constant // or a value on the root type. Prev *Expr // The expression this is an argument for, which is only set on the first expression in a chain. Parent *Expr // The parameter this expression is on if any. This is only set for the first expression in the chain. Parameter *Parameter // The system that created the expression. System *System }
type Options ¶
type Options struct { // The type that is used as the root of the expressions. RootType TypeName // The expected types if any. If none are given then the type of the expression is returned. // If one or more are given then the expression is automatically cast to a desired type // if possible, otherwise errors if it can't meet the expected types. ExpectedTypes []TypeName // The expression to parse. Expression string }
The parse options for an expression string into an Expression struct.
type Parameter ¶
type Parameter struct { // The expected type for the parameter. Either this or Generic is required. Type TypeName `json:"type,omitempty"` // If there is no expected type, but this parameter and potentially others need to be the same type. // The generic parameters can also decide the type of generic value. Generic bool `json:"generic,omitempty"` // The name of the parameter. Name string `json:"name,omitempty"` // A more detailed description of the parameter. Description string `json:"description,omitempty"` // A default value, making this an optional parameter. This must be a valid value that can be parsed by the type. Default *string `json:"default,omitempty"` // contains filtered or unexported fields }
A parameter to a parameterized value. Type or Generic is required.
func (Parameter) ParameterType ¶
type ParseError ¶
type ParseError struct { Message string Expr *Expr Parameter *Parameter Start *Position End *Position }
An error occurred during the parsing or linking of System.Parse.
func NewParseError ¶
func NewParseError(expr *Expr, message string) ParseError
Creates a new parse error given the expression (if any) and the message.
type Position ¶
type Position struct { // The index of the character in Options.Expression Index int // The line of the character, where \n delimits lines. Line int // The column of the character in its line. Column int }
The position of a character in a multi-line string.
type Reflect ¶
type Reflect struct {
// contains filtered or unexported fields
}
func NewReflect ¶
func NewReflect(options ReflectOptions) (r *Reflect, err error)
func (Reflect) Compile ¶
func (r Reflect) Compile(e *Expr) ReflectCompiled
type ReflectCompiled ¶
type ReflectConversion ¶
type ReflectOptions ¶
type System ¶
type System struct {
// contains filtered or unexported fields
}
A type system that validates types, values, parameters, etc.
func NewSystemRequired ¶
Returns a System given a set of types and panics if any of the types, values, parameters, etc are malformed.
func (System) Parse ¶
Parses an expression with the given set of options. Even if the expression is invalid it will be returned and all attempts of determining types and values will be made to best inform the user precisely what is wrong and what is valid.
func (System) ParseOrder ¶
Returns the types that can parse constants in the order determined by the system.
type SystemError ¶
type SystemError struct { Message string Type *Type Value *Value Parameter *Parameter Path *string }
An error occurred building a system from types.
func (SystemError) Error ¶
func (e SystemError) Error() string
type Type ¶
type Type struct { // The name of this type, should be unique. Name TypeName `json:"name"` // A description of this type. Description string `json:"description,omitempty"` // All values of this type. Values []Value `json:"values,omitempty"` // All types that this type can be converted to, and which value path can be used to do it. As map[TypeName]string `json:"as,omitempty"` // The type might be an enumerated value which means it has to be one of the specified values. // Parse can be specified to validate this and return a different data type other than string. Enums []string `json:"enums,omitempty"` // A custom parse function that converts a constant into a real value that is stored in Expression.Parsed. // If the given input does not match the type an error must be returned. Parse func(x string) (any, error) `json:"-"` // The parse order of the type. By default all types are considered equal and have an order of 0. // Higher parse orders are used first. For all types with the same parse order they are ordered // whether they have a Parse function (it prefers this). For two types with equivalent parse function // specificity they are ordered by type name length (preferring longer types before shorter). ParseOrder int `json:"parseOrder,omitempty"` // contains filtered or unexported fields }
A data type in an expression system. It can have values, with and without parameters. It can also be automatically cast to another type with the `As` field.
func (Type) AsValue ¶
Returns the value that's used to convert to the given type. If this type was not given to a system then a nil panic will occur.
func (Type) EnumFor ¶
Returns the enum value that matches the given text. If this type was not given to a system then a nil panic will occur.
func (Type) ParseInput ¶
Parses the constant input and returns a matching value. If there is no parse or matching enum option then an error is returned.
type TypeCompilers ¶
type TypeCompilers[CE any] map[TypeName]ValueCompilers[CE]
A set of value compilers mapped by a type.
type Value ¶
type Value struct { // The main path for the value. Alternatives can be specified with Aliases. Path string `json:"path"` // The aliases to the path, to allow for more than one way to refer to the value. Aliases []string `json:"aliases,omitempty"` // The description of the value. Description string `json:"description,omitempty"` // The type of the value. Type TypeName `json:"type,omitempty"` // If the value is has a generic type that's determined based on one or more generic parameters. Generic bool `json:"generic,omitempty"` // The parameters for the value. Parameters []Parameter `json:"parameters,omitempty"` // If the last parameter can be specified any number of times. Variadic bool `json:"variadic,omitempty"` // contains filtered or unexported fields }
A value (possibly with parameters) on a type.
func (Value) GetType ¶
Determines the type for this value for the given expression. If this value is generic the types of the generic parameters will be used to determine the returned type.
func (Value) MaxParameters ¶
Returns the maximum number of possible parameters. If this value is not parameterized this returns 0. If this value is parameterized and variadic it returns the largest possible int.
func (Value) MinParameters ¶
Returns the minimum number of required parameters
type ValueCompilers ¶
A set of compilers mapped by their lowecased paths.