Documentation
¶
Overview ¶
Package tagexpr is an interesting go struct tag expression syntax for field validation, etc.
Copyright 2019 Bytedance Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Example ¶
package main import ( "fmt" tagexpr "github.com/bytedance/go-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:"$"` } } vm := tagexpr.New("tagexpr") err := vm.WarmUp(new(T)) if err != nil { panic(err) } 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}, } 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")) }
Output: true true true C must be true when T.f.g>0 invalid d: [x y] true false 1
Index ¶
- func RegFunc(funcName string, fn func(...interface{}) interface{}, force ...bool) error
- type Expr
- type ExprNode
- 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) Field(fieldSelector string) interface{}
- func (t *TagExpr) Range(fn func(exprSelector string, eval func() interface{}) bool)
- type VM
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegFunc ¶ added in v1.2.0
RegFunc 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.
Types ¶
type ExprNode ¶
type ExprNode interface { SetParent(ExprNode) Parent() ExprNode LeftOperand() ExprNode RightOperand() ExprNode SetLeftOperand(ExprNode) SetRightOperand(ExprNode) Run(string, *TagExpr) interface{} }
ExprNode expression interface
type TagExpr ¶
type TagExpr struct {
// contains filtered or unexported fields
}
TagExpr struct tag expression evaluator
func (*TagExpr) Eval ¶
Eval evaluate 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 ¶ added in v1.1.0
EvalBool evaluate the value of the struct tag expression by the selector expression. NOTE:
If the expression value type is not bool, return false.
func (*TagExpr) EvalFloat ¶ added in v1.1.0
EvalFloat evaluate 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 ¶ added in v1.1.0
EvalString evaluate the value of the struct tag expression by the selector expression. NOTE:
If the expression value type is not string, return "".
func (*TagExpr) Field ¶ added in v1.1.3
Field returns the field value specified by the selector. NOTE:
Return nil if the field is not exist
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.
func (*VM) MustRun ¶ added in v1.2.0
MustRun is similar to Run, but panic when error.
func (*VM) MustWarmUp ¶ added in v1.2.0
func (vm *VM) MustWarmUp(structOrStructPtr ...interface{})
MustWarmUp is similar to WarmUp, but panic when error.
func (*VM) Run ¶
Run returns the tag expression handler of the @structPtr. NOTE:
If the structure type has not been warmed up, it will be slower when it is first called.