Documentation ¶
Index ¶
- Constants
- Variables
- func GetFloatValue(v interface{}) (float64, bool)
- func GetIntValue(v interface{}) (int64, bool)
- func GetStringValue(v interface{}) (string, bool)
- type AssignExp
- type AssignFunc
- type CompareExp
- type CompareFunc
- type Configuration
- type Context
- type DefaultContext
- type Dictionary
- func (m *Dictionary) Assign(assignName string, left string, right interface{}, context Context) error
- func (m *Dictionary) Compare(compareName string, left string, right interface{}, context Context) (bool, error)
- func (m *Dictionary) GetPipeFunction(name string) PipeFunction
- func (m *Dictionary) GetVarValue(varName string, context Context) (interface{}, error)
- func (m *Dictionary) ListAssigns() []string
- func (m *Dictionary) ListCompares() []string
- func (m *Dictionary) ListObjects() []string
- func (m *Dictionary) ListVars() []string
- func (m *Dictionary) RegisterAssign(assignName string, assignFunc AssignFunc)
- func (m *Dictionary) RegisterCompare(compareName string, compareFunc CompareFunc)
- func (m *Dictionary) RegisterObject(objectName string, object Object) error
- func (m *Dictionary) RegisterObjectInContext(objectName string, object Object, context Context)
- func (m *Dictionary) RegisterPipeFunction(name string, fn PipeFunction)
- func (m *Dictionary) RegisterVar(varName string, fetchFunc VarFunc) error
- type JsonExp
- type JsonExpGroup
- type NameValue
- type Object
- type PipeFunction
- type VarFunc
- type VarType
Constants ¶
View Source
const ( PipelineFnLen = "len" PipelineFnUpper = "upper" PipelineFnLower = "lower" PipelineFnFnv32 = "fnv32" PipelineFnFnv64 = "fnv64" PipelineFnMd5Lower = "md5" PipelineFnMd5Upper = "MD5" )
Variables ¶
View Source
var AddAssign = func(L string, lValue interface{}, R interface{}, ret Context) error { if ret == nil { return fmt.Errorf("param ret is nil") } lType := GetValueType(lValue) vType := GetValueType(R) if lType == VarInvalid { lType = vType } switch lType { case VarStr: old, _ := GetStringValue(lValue) add, _ := GetStringValue(R) ret.SetCtxData(L, old+add) case VarFloat: old, _ := GetFloatValue(lValue) add, _ := GetFloatValue(R) ret.SetCtxData(L, old+add) case VarInt: old, _ := GetIntValue(lValue) add, _ := GetIntValue(R) ret.SetCtxData(L, old+add) default: return fmt.Errorf("invalid operand") } return nil }
View Source
var Any = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value not string-incompatible") } if l == "" || r == "" { return false, nil } lList := strings.Split(l, ",") rList := strings.Split(r, ",") any := false for _, v := range rList { for _, vL := range lList { if v == vL { any = true break } } if any { return true, nil } } return false, nil }
View Source
var Assign = func(L string, lValue interface{}, R interface{}, ret Context) error { if ret == nil { return fmt.Errorf("param ret is nil") } ret.SetCtxData(L, R) return nil }
assign functions
View Source
var Between = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarStr { l, _ := GetStringValue(L) if r, ok := GetStringValue(R); !ok { return false, fmt.Errorf("right value not string-incompatible") } else { rList := strings.Split(r, ",") if len(rList) != 2 { return false, fmt.Errorf("right value not a between string") } else { return l >= rList[0] && l <= rList[1], nil } } } else if tp == VarInt { l, _ := GetIntValue(L) if r, ok := GetStringValue(R); !ok { return false, fmt.Errorf("right value not a between string") } else { rList := strings.Split(r, ",") if len(rList) != 2 { return false, fmt.Errorf("right value not a between string") } else { if b, errB := strconv.ParseInt(rList[0], 0, 64); errB == nil { if e, errE := strconv.ParseInt(rList[1], 0, 64); errE == nil { return l >= b && l <= e, nil } } return false, fmt.Errorf("right value not a between string") } } } else if tp == VarFloat { l, _ := GetFloatValue(L) if r, ok := GetStringValue(R); !ok { return false, fmt.Errorf("right value not a between string") } else { rList := strings.Split(r, ",") if len(rList) != 2 { return false, fmt.Errorf("right value not a between string") } else { if b, errB := strconv.ParseFloat(rList[0], 64); errB == nil { if e, errE := strconv.ParseFloat(rList[1], 64); errE == nil { return l >= b && l <= e, nil } } return false, fmt.Errorf("right value not a between string") } } } else { return false, fmt.Errorf("invalid data type of L") } }
View Source
var Contain = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value not string-incompatible") } if l == "" || r == "" { return false, nil } ret := strings.Contains(l, r) return ret, nil }
View Source
var Cover = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value is not string incompatible") } if l == "" || r == "" { return false, nil } rList := strings.Split(r, ",") for _, v := range rList { if strings.Contains(l, v) { return true, nil } } return false, nil }
View Source
var Date = func(context Context) (interface{}, error) { return time.Now().Format("2006-01-02"), nil }
View Source
var DateTime = func(context Context) (interface{}, error) { return time.Now().Format("2006-01-02 15:04:05"), nil }
View Source
var Day = func(context Context) (interface{}, error) { return time.Now().Format("02"), nil }
View Source
var DivAssign = func(L string, lValue interface{}, R interface{}, ret Context) error { if ret == nil { return fmt.Errorf("param ret is nil") } old, _ := GetFloatValue(lValue) add, _ := GetFloatValue(R) if add <= 0.00001 && add >= -0.00001 { return fmt.Errorf("invalid operand") } ret.SetCtxData(L, old/add) return nil }
View Source
var Equal = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarInvalid { return false, fmt.Errorf("invalid param L") } else if tp == VarStr { s, _ := GetStringValue(L) if sR, ok := GetStringValue(R); ok { return s == sR, nil } else { return false, fmt.Errorf("right value not string-incompatible") } } else if tp == VarInt { i, _ := GetIntValue(L) if iR, ok := GetIntValue(R); ok { return i == iR, nil } else { return false, fmt.Errorf("right value not int-incompatible") } } else if tp == VarFloat { f, _ := GetFloatValue(L) if fR, ok := GetFloatValue(R); ok { return f == fR, nil } else { return false, fmt.Errorf("right value not float-incompatible") } } return false, nil }
View Source
var Has = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value not string-incompatible") } if l == "" || r == "" { return false, nil } lList := strings.Split(l, ",") rList := strings.Split(r, ",") for _, v := range rList { has := false for _, vL := range lList { if v == vL { has = true break } } if !has { return false, nil } } return true, nil }
View Source
var HeadMatch = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value not string-incompatible") } if l == "" || r == "" { return false, nil } ret := strings.Index(l, r) == 0 return ret, nil }
View Source
var Hour = func(context Context) (interface{}, error) { return time.Now().Format("15"), nil }
View Source
var IHour = func(context Context) (interface{}, error) { return time.Now().Hour(), nil }
View Source
var IMinute = func(context Context) (interface{}, error) { return time.Now().Minute(), nil }
View Source
var IMonth = func(context Context) (interface{}, error) { return time.Now().Month(), nil }
View Source
var ISecond = func(context Context) (interface{}, error) { return time.Now().Second(), nil }
View Source
var IYear = func(context Context) (interface{}, error) { return time.Now().Year(), nil }
View Source
var In = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } if r, ok := GetStringValue(R); !ok { return false, fmt.Errorf("right value not string-incompatible") } else { rList := strings.Split(r, ",") for _, v := range rList { if l == v { return true, nil } } } return false, nil }
View Source
var Less = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarInvalid { return false, fmt.Errorf("invalid param L") } else if tp == VarStr { s, _ := GetStringValue(L) if sR, ok := GetStringValue(R); ok { return s < sR, nil } else { return false, fmt.Errorf("right value not string-incompatible") } } else if tp == VarInt { i, _ := GetIntValue(L) if iR, ok := GetIntValue(R); ok { return i < iR, nil } else { return false, fmt.Errorf("right value not int-incompatible") } } else if tp == VarFloat { f, _ := GetFloatValue(L) if fR, ok := GetFloatValue(R); ok { return f < fR, nil } else { return false, fmt.Errorf("right value not float-incompatible") } } return false, nil }
View Source
var LessEqual = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarInvalid { return false, fmt.Errorf("invalid param L") } else if tp == VarStr { s, _ := GetStringValue(L) if sR, ok := GetStringValue(R); ok { return s <= sR, nil } else { return false, fmt.Errorf("right value not string-incompatible") } } else if tp == VarInt { i, _ := GetIntValue(L) if iR, ok := GetIntValue(R); ok { return i <= iR, nil } else { return false, fmt.Errorf("right value not int-incompatible") } } else if tp == VarFloat { f, _ := GetFloatValue(L) if fR, ok := GetFloatValue(R); ok { return f <= fR, nil } else { return false, fmt.Errorf("right value not float-incompatible") } } return false, nil }
View Source
var Minute = func(context Context) (interface{}, error) { return time.Now().Format("04"), nil }
View Source
var ModAssign = func(L string, lValue interface{}, R interface{}, ret Context) error { if ret == nil { return fmt.Errorf("param ret is nil") } old, _ := GetFloatValue(lValue) add, _ := GetFloatValue(R) addInt := int64(add) if addInt == 0 { return fmt.Errorf("invalid operand") } ret.SetCtxData(L, int64(old)%addInt) return nil }
View Source
var Month = func(context Context) (interface{}, error) { return time.Now().Format("01"), nil }
View Source
var More = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarInvalid { return false, fmt.Errorf("invalid param L") } else if tp == VarStr { s, _ := GetStringValue(L) if sR, ok := GetStringValue(R); ok { return s > sR, nil } else { return false, fmt.Errorf("right value not string-incompatible") } } else if tp == VarInt { i, _ := GetIntValue(L) if iR, ok := GetIntValue(R); ok { return i > iR, nil } else { return false, fmt.Errorf("right value not int-incompatible") } } else if tp == VarFloat { f, _ := GetFloatValue(L) if fR, ok := GetFloatValue(R); ok { return f > fR, nil } else { return false, fmt.Errorf("right value not float-incompatible") } } return false, nil }
View Source
var MoreEqual = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarInvalid { return false, fmt.Errorf("invalid param L") } else if tp == VarStr { s, _ := GetStringValue(L) if sR, ok := GetStringValue(R); ok { return s >= sR, nil } else { return false, fmt.Errorf("right value not string-incompatible") } } else if tp == VarInt { i, _ := GetIntValue(L) if iR, ok := GetIntValue(R); ok { return i >= iR, nil } else { return false, fmt.Errorf("right value not int-incompatible") } } else if tp == VarFloat { f, _ := GetFloatValue(L) if fR, ok := GetFloatValue(R); ok { return f >= fR, nil } else { return false, fmt.Errorf("right value not float-incompatible") } } return false, nil }
View Source
var MulAssign = func(L string, lValue interface{}, R interface{}, ret Context) error { if ret == nil { return fmt.Errorf("param ret is nil") } lType := GetValueType(lValue) vType := GetValueType(R) if lType == VarInvalid { lType = vType } switch lType { case VarStr: if vType == VarInt { old, _ := GetStringValue(lValue) ret.SetCtxData(L, strings.Repeat(old, R.(int))) } else { return fmt.Errorf("invalid operand") } case VarFloat: old, _ := GetFloatValue(lValue) ret.SetCtxData(L, old*R.(float64)) case VarInt: old, _ := GetIntValue(lValue) ret.SetCtxData(L, old*R.(int64)) default: return fmt.Errorf("invalid operand") } return nil }
View Source
var None = func(L, R interface{}, context Context) (bool, error) { any, err := Any(L, R, context) if err != nil { return false, err } return !any, nil }
View Source
var NotBetween = func(L, R interface{}, context Context) (bool, error) { ret, err := Between(L, R, context) if err != nil { return false, err } return !ret, nil }
View Source
var NotContain = func(L, R interface{}, context Context) (bool, error) { ret, err := Contain(L, R, context) if err != nil { return false, err } return !ret, nil }
View Source
var NotCover = func(L, R interface{}, context Context) (bool, error) { ret, err := Cover(L, R, context) if err != nil { return false, err } return !ret, err }
compares
View Source
var NotEqual = func(L, R interface{}, context Context) (bool, error) { tp := GetValueType(L) if tp == VarInvalid { return false, fmt.Errorf("invalid param L") } else if tp == VarStr { s, _ := GetStringValue(L) if sR, ok := GetStringValue(R); ok { return s != sR, nil } else { return false, fmt.Errorf("right value not string-incompatible") } } else if tp == VarInt { i, _ := GetIntValue(L) if iR, ok := GetIntValue(R); ok { return i != iR, nil } else { return false, fmt.Errorf("right value not int-incompatible") } } else if tp == VarFloat { f, _ := GetFloatValue(L) if fR, ok := GetFloatValue(R); ok { return f != fR, nil } else { return false, fmt.Errorf("right value not float-incompatible") } } return false, nil }
View Source
var NotHeadMatch = func(L, R interface{}, context Context) (bool, error) { ret, err := HeadMatch(L, R, context) if err != nil { return false, err } return !ret, nil }
View Source
var NotIn = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } if r, ok := GetStringValue(R); !ok { return false, fmt.Errorf("right value not string-incompatible") } else { rList := strings.Split(r, ",") in := false for _, v := range rList { if l == v { in = true break } } return !in, nil } }
View Source
var NotTailMatch = func(L, R interface{}, context Context) (bool, error) { ret, err := TailMatch(L, R, context) if err != nil { return false, err } return !ret, nil }
View Source
var Rand = func(context Context) (interface{}, error) { if ret, ok := context.GetCtxData("$rand"); ok { if rd, ok := GetIntValue(ret); ok { return rd, nil } } return rand.Intn(100) + 1, nil }
View Source
var RegExpMatch = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value not string-incompatible") } if l == "" || r == "" { return false, nil } if matched, err := regexp.Match(r, goutil.UnsafeStringToBytes(l)); err == nil { return matched, nil } else { return false, err } }
View Source
var Second = func(context Context) (interface{}, error) { return time.Now().Format("05"), nil }
View Source
var ShortTime = func(context Context) (interface{}, error) { return time.Now().Format("15:04"), nil }
View Source
var SubAssign = func(L string, lValue interface{}, R interface{}, ret Context) error { if ret == nil { return fmt.Errorf("param ret is nil") } vType := GetValueType(R) switch vType { case VarFloat: old, _ := GetFloatValue(lValue) ret.SetCtxData(L, old-R.(float64)) case VarInt: old, _ := GetIntValue(lValue) ret.SetCtxData(L, old-R.(int64)) default: return fmt.Errorf("invalid operand") } return nil }
View Source
var TailMatch = func(L, R interface{}, context Context) (bool, error) { l, lOk := GetStringValue(L) if !lOk { return false, fmt.Errorf("invalid L") } r, rOk := GetStringValue(R) if !rOk { return false, fmt.Errorf("right value not string-incompatible") } if l == "" || r == "" { return false, nil } ret := strings.Index(l, r) == len(l)-len(r) return ret, nil }
Functions ¶
func GetFloatValue ¶
func GetIntValue ¶
func GetStringValue ¶
Types ¶
type AssignFunc ¶
type CompareExp ¶
type CompareFunc ¶
type Configuration ¶
type Configuration struct {
// contains filtered or unexported fields
}
Configuration对象代表一个json配置,其中包含,0个或n个key/value键值对,以及0个或n个JSON表达式组
func NewConfiguration ¶
func NewConfiguration(jsonSource []byte, dict *Dictionary) (*Configuration, error)
传入json,创建一个Configuration对象
func (*Configuration) GetJsonExpGroup ¶
func (m *Configuration) GetJsonExpGroup(key string) (*JsonExpGroup, bool)
获取JSON表达式组
func (*Configuration) GetNameValue ¶
func (m *Configuration) GetNameValue(key string, context Context) (interface{}, bool)
获取键值
type DefaultContext ¶
type DefaultContext = goutil.DefaultContext
type Dictionary ¶
type Dictionary struct {
// contains filtered or unexported fields
}
func NewDictionary ¶
func NewDictionary() *Dictionary
func (*Dictionary) Assign ¶
func (m *Dictionary) Assign(assignName string, left string, right interface{}, context Context) error
func (*Dictionary) GetPipeFunction ¶ added in v1.4.6
func (m *Dictionary) GetPipeFunction(name string) PipeFunction
func (*Dictionary) GetVarValue ¶
func (m *Dictionary) GetVarValue(varName string, context Context) (interface{}, error)
func (*Dictionary) ListAssigns ¶
func (m *Dictionary) ListAssigns() []string
func (*Dictionary) ListCompares ¶
func (m *Dictionary) ListCompares() []string
func (*Dictionary) ListObjects ¶ added in v1.2.3
func (m *Dictionary) ListObjects() []string
func (*Dictionary) ListVars ¶
func (m *Dictionary) ListVars() []string
func (*Dictionary) RegisterAssign ¶
func (m *Dictionary) RegisterAssign(assignName string, assignFunc AssignFunc)
注册赋值运算符
func (*Dictionary) RegisterCompare ¶
func (m *Dictionary) RegisterCompare(compareName string, compareFunc CompareFunc)
注册条件运算符
func (*Dictionary) RegisterObject ¶ added in v1.2.3
func (m *Dictionary) RegisterObject(objectName string, object Object) error
注册对象,对象名必须以"$"开头,且不能与变量重名
func (*Dictionary) RegisterObjectInContext ¶ added in v1.4.9
func (m *Dictionary) RegisterObjectInContext(objectName string, object Object, context Context)
func (*Dictionary) RegisterPipeFunction ¶ added in v1.4.6
func (m *Dictionary) RegisterPipeFunction(name string, fn PipeFunction)
func (*Dictionary) RegisterVar ¶
func (m *Dictionary) RegisterVar(varName string, fetchFunc VarFunc) error
注册变量,变量名必须以"$"开头,且不能与object重名
type JsonExp ¶
type JsonExp struct {
// contains filtered or unexported fields
}
func (*JsonExp) GetAssignExpList ¶
func (*JsonExp) GetCompareExpList ¶
func (m *JsonExp) GetCompareExpList() []*CompareExp
type JsonExpGroup ¶
type JsonExpGroup struct {
// contains filtered or unexported fields
}
a group of JsonExp json-exp group configuration json demo: { //json-exp group, from first to last, executing each json-exp node, breaking execution if variant $break = true, $break is a system variant "filter": [ //json-exp node1, if all compare-exp return true, then execute assign-exp [ ["left-var","compare-name","right-var"], //compare-exp ["left-var","compare-name","right-var"], //compare-exp ["left-var","assign-name","right-var"], //the last exp is always an assign-exp ], //json-exp node2 [ ["left-var","compare-name","right-var"], //compare-exp ["left-var","compare-name","right-var"], //compare-exp //multi-assign-exp [ ["left-var","assign-name","right-var"], ["left-var","assign-name","right-var"], ["left-var","assign-name","right-var"] ] ], //json-exp node3 [ //... ] ] }
func NewJsonExpGroup ¶
func NewJsonExpGroup(dict *Dictionary, groupSource interface{}) (*JsonExpGroup, error)
func (*JsonExpGroup) List ¶
func (m *JsonExpGroup) List() []*JsonExp
type PipeFunction ¶ added in v1.4.6
Click to show internal directories.
Click to hide internal directories.