Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/state-cloud/client-go/internal/tagexpr"
)
func main() {
type T struct {
A int `tagexpr:"$<0||$>=100"`
B string `tagexpr:"len($)>1 && regexp('^\\w*$')"`
C bool `tagexpr:"expr1:(f.g)$>0 && $; expr2:'C must be true when T.f.g>0'"`
d []string `tagexpr:"@:len($)>0 && $[0]=='D'; msg:sprintf('invalid d: %v',$)"`
e map[string]int `tagexpr:"len($)==$['len']"`
e2 map[string]*int `tagexpr:"len($)==$['len']"`
f struct {
g int `tagexpr:"$"`
}
h int `tagexpr:"$>minVal"`
}
vm := tagexpr.New("tagexpr")
t := &T{
A: 107,
B: "abc",
C: true,
d: []string{"x", "y"},
e: map[string]int{"len": 1},
e2: map[string]*int{"len": new(int)},
f: struct {
g int `tagexpr:"$"`
}{1},
h: 10,
}
tagExpr, err := vm.Run(t)
if err != nil {
panic(err)
}
fmt.Println(tagExpr.Eval("A"))
fmt.Println(tagExpr.Eval("B"))
fmt.Println(tagExpr.Eval("C@expr1"))
fmt.Println(tagExpr.Eval("C@expr2"))
if !tagExpr.Eval("d").(bool) {
fmt.Println(tagExpr.Eval("d@msg"))
}
fmt.Println(tagExpr.Eval("e"))
fmt.Println(tagExpr.Eval("e2"))
fmt.Println(tagExpr.Eval("f.g"))
fmt.Println(tagExpr.EvalWithEnv("h", map[string]interface{}{"minVal": 9}))
fmt.Println(tagExpr.EvalWithEnv("h", map[string]interface{}{"minVal": 11}))
}
Output: true true true C must be true when T.f.g>0 invalid d: [x y] true false 1 true false
Index ¶
- Constants
- func FakeBool(v interface{}) bool
- func MustRegFunc(funcName string, fn func(...interface{}) interface{}, force ...bool)
- func RegFunc(funcName string, fn func(...interface{}) interface{}, force ...bool) error
- type Boolean
- type Expr
- type ExprHandler
- func (e *ExprHandler) Eval() interface{}
- func (e *ExprHandler) EvalBool() bool
- func (e *ExprHandler) EvalFloat() float64
- func (e *ExprHandler) EvalString() string
- func (e *ExprHandler) ExprSelector() ExprSelector
- func (e *ExprHandler) Path() string
- func (e *ExprHandler) StringSelector() string
- func (e *ExprHandler) TagExpr() *TagExpr
- type ExprNode
- type ExprSelector
- type FieldHandler
- type FieldSelector
- type Null
- type Number
- type String
- type TagExpr
- func (t *TagExpr) Eval(exprSelector string) interface{}
- func (t *TagExpr) EvalBool(exprSelector string) bool
- func (t *TagExpr) EvalFloat(exprSelector string) float64
- func (t *TagExpr) EvalString(exprSelector string) string
- func (t *TagExpr) EvalWithEnv(exprSelector string, env map[string]interface{}) interface{}
- func (t *TagExpr) Field(fieldSelector string) (fh *FieldHandler, found bool)
- func (t *TagExpr) Range(fn func(*ExprHandler) error) error
- func (t *TagExpr) RangeFields(fn func(*FieldHandler) bool) bool
- type VM
Examples ¶
Constants ¶
const ( // FieldSeparator in the expression selector, // the separator between field names FieldSeparator = "." // ExprNameSeparator in the expression selector, // the separator of the field name and expression name ExprNameSeparator = "@" // DefaultExprName the default name of single model expression DefaultExprName = ExprNameSeparator )
Variables ¶
This section is empty.
Functions ¶
func MustRegFunc ¶
MustRegFunc registers function expression. NOTE:
example: len($), regexp("\\d") or regexp("\\d",$);
If @force=true, allow to cover the existed same @funcName;
The go number types always are float64;
The go string types always are string;
Panic if there is an error.
Types ¶
type ExprHandler ¶
type ExprHandler struct {
// contains filtered or unexported fields
}
ExprHandler expr handler
func (*ExprHandler) Eval ¶
func (e *ExprHandler) Eval() interface{}
Eval evaluate the value of the struct tag expression. NOTE:
result types: float64, string, bool, nil
func (*ExprHandler) EvalBool ¶
func (e *ExprHandler) EvalBool() bool
EvalBool evaluates the value of the struct tag expression. NOTE:
If the expression value is not 0, '' or nil, return true.
func (*ExprHandler) EvalFloat ¶
func (e *ExprHandler) EvalFloat() float64
EvalFloat evaluates the value of the struct tag expression. NOTE:
If the expression value type is not float64, return 0.
func (*ExprHandler) EvalString ¶
func (e *ExprHandler) EvalString() string
EvalString evaluates the value of the struct tag expression. NOTE:
If the expression value type is not string, return "".
func (*ExprHandler) ExprSelector ¶
func (e *ExprHandler) ExprSelector() ExprSelector
ExprSelector returns the expression selector of ExprSelector type.
func (*ExprHandler) Path ¶
func (e *ExprHandler) Path() string
Path returns the path description of the expression.
func (*ExprHandler) StringSelector ¶
func (e *ExprHandler) StringSelector() string
StringSelector returns the expression selector of string type.
func (*ExprHandler) TagExpr ¶
func (e *ExprHandler) TagExpr() *TagExpr
TagExpr returns the *TagExpr.
type ExprNode ¶
type ExprNode interface {
SetParent(ExprNode)
Parent() ExprNode
LeftOperand() ExprNode
RightOperand() ExprNode
SetLeftOperand(ExprNode)
SetRightOperand(ExprNode)
String() string
Run(context.Context, string, *TagExpr) interface{}
}
ExprNode expression interface
type ExprSelector ¶
type ExprSelector string
ExprSelector expression selector
func (ExprSelector) Field ¶
func (e ExprSelector) Field() string
Field returns the field selector it belongs to.
func (ExprSelector) Name ¶
func (e ExprSelector) Name() string
Name returns the name of the expression.
func (ExprSelector) ParentField ¶
func (e ExprSelector) ParentField() (string, bool)
ParentField returns the parent field selector it belongs to.
func (ExprSelector) Split ¶
func (e ExprSelector) Split() (field FieldSelector, name string)
Split returns the field selector and the expression name.
func (ExprSelector) String ¶
func (e ExprSelector) String() string
String returns string type value.
type FieldHandler ¶
type FieldHandler struct {
// contains filtered or unexported fields
}
FieldHandler field handler
func (*FieldHandler) EvalFuncs ¶
func (f *FieldHandler) EvalFuncs() map[ExprSelector]func() interface{}
EvalFuncs returns the tag expression eval functions.
func (*FieldHandler) FieldSelector ¶
func (f *FieldHandler) FieldSelector() FieldSelector
FieldSelector returns the field selector of FieldSelector type.
func (*FieldHandler) StringSelector ¶
func (f *FieldHandler) StringSelector() string
StringSelector returns the field selector of string type.
func (*FieldHandler) StructField ¶
func (f *FieldHandler) StructField() reflect.StructField
StructField returns the field StructField object.
type FieldSelector ¶
type FieldSelector string
FieldSelector expression selector
func (FieldSelector) Name ¶
func (f FieldSelector) Name() string
Name returns the current field name.
func (FieldSelector) Parent ¶
func (f FieldSelector) Parent() (string, bool)
Parent returns the parent FieldSelector.
func (FieldSelector) Split ¶
func (f FieldSelector) Split() (paths []string, name string)
Split returns the path segments and the current field name.
func (FieldSelector) String ¶
func (f FieldSelector) String() string
String returns string type value.
type TagExpr ¶
type TagExpr struct {
// contains filtered or unexported fields
}
TagExpr struct tag expression evaluator
func (*TagExpr) Eval ¶
Eval evaluates the value of the struct tag expression by the selector expression. NOTE:
format: fieldName, fieldName.exprName, fieldName1.fieldName2.exprName1 result types: float64, string, bool, nil
func (*TagExpr) EvalBool ¶
EvalBool evaluates the value of the struct tag expression by the selector expression. NOTE:
If the expression value is not 0, '' or nil, return true.
func (*TagExpr) EvalFloat ¶
EvalFloat evaluates the value of the struct tag expression by the selector expression. NOTE:
If the expression value type is not float64, return 0.
func (*TagExpr) EvalString ¶
EvalString evaluates the value of the struct tag expression by the selector expression. NOTE:
If the expression value type is not string, return "".
func (*TagExpr) EvalWithEnv ¶
EvalWithEnv evaluates the value with the given env NOTE:
format: fieldName, fieldName.exprName, fieldName1.fieldName2.exprName1 result types: float64, string, bool, nil
func (*TagExpr) Field ¶
func (t *TagExpr) Field(fieldSelector string) (fh *FieldHandler, found bool)
Field returns the field handler specified by the selector.
func (*TagExpr) Range ¶
func (t *TagExpr) Range(fn func(*ExprHandler) error) error
Range loop through each tag expression. When fn returns false, interrupt traversal and return false. NOTE:
eval result types: float64, string, bool, nil
func (*TagExpr) RangeFields ¶
func (t *TagExpr) RangeFields(fn func(*FieldHandler) bool) bool
RangeFields loop through each field. When fn returns false, interrupt traversal and return false.
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM struct tag expression interpreter
func New ¶
New creates a tag expression interpreter that uses tagName as the tag name. NOTE:
If no tagName is specified, no tag expression will be interpreted, but still can operate the various fields.
func (*VM) Run ¶
Run returns the tag expression handler of the @structPtrOrReflectValue. NOTE:
If the structure type has not been warmed up, it will be slower when it is first called.
Disable new -d=checkptr behaviour for Go 1.14