Coder Social home page Coder Social logo

go-expression's Introduction

expression EL表达式

Install

go get github.com/heartlhj/go-expression

功能

  • 字符串的提取和比较
    context := spel.StandardEvaluationContext{}
    m := make(map[string]interface{})
    m["name"] = "lisi"
    m["age"] = 18
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#name=='lisi'"
    //expressionString := "#name" //返回lisi
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
  • 数字比较,支持int和 float64
    context := spel.StandardEvaluationContext{}
    m := make(map[string]interface{})
    m["name"] = "lisi"
    m["age"] = 18
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#age>=10"
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)

float64比较

    context := spel.StandardEvaluationContext{}
    m := make(map[string]interface{})
    var ageFloat float64
    ageFloat = 10
    m["num"] = ageFloat
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#num>=9f"
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
  • 与(&&)或 (||)操作
    context := spel.StandardEvaluationContext{}
    m := make(map[string]interface{})
    m["name"] = "lisi"
    m["age"] = 18
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#name=='lisi' && #age>=3"
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
  • 复合结构,迭代提取
    context := spel.StandardEvaluationContext{}
    context.AddPropertyAccessor(spel.MapAccessor{})
    m := make(map[string]interface{})
    m["name"] = "lisi"
    m["age"] = 18
    m1 := make(map[string]interface{})
    m2 := make(map[string]interface{})
    m2["num"] = 12
    m1["code"] = m2
    m["order"] = m1
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#order.code.num==12"
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
  • 数组和切片提取
    context := spel.StandardEvaluationContext{}
    context.AddPropertyAccessor(spel.MapAccessor{})
    m := make(map[string]interface{})
    m["name"] = "lisi"
    m["age"] = 18
    m1 := make(map[string]interface{})
    //切片
    //orders := make([]Order, 2)
    //数组
    orders := [2]Order{}
    orders[0] = Order{name: "lisi", age: 12}
    orders[1] = Order{name: "wang", age: 24}
    m1["code"] = orders
    m["order"] = m1
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#order.code[0].name=='lisi'"
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
  • 字符串下标判断提取
    context := spel.StandardEvaluationContext{}
    context.AddPropertyAccessor(spel.MapAccessor{})
    m := make(map[string]interface{})
    m["name"] = "lisi"
    m["age"] = 18
    context.SetVariables(m)
    parser := SpelExpressionParser{}
    expressionString := "#name[2]=='i'"
    valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)

go-expression's People

Contributors

heartlhj avatar

Stargazers

鹧鸪天 avatar qinqiangqiang avatar  avatar tricolorfire avatar  avatar

Watchers

 avatar

go-expression's Issues

大于(>)、小于(<)操作符被识别为大于等于(>=)、小于等于(<=)

context := spel.StandardEvaluationContext{}
m := make(map[string]interface{})
m["name"] = "lisi"
m["age"] = 18
context.SetVariables(m)
parser := expression.SpelExpressionParser{}
expressionString := "#age>18"
valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)

// 结果打印为true
fmt.Println(valueContext)

貌似是这里Tokenizer中调用pushCharToken,放入的是一个 大于等于的算子

case ">":
if t.isTwoCharToken(TokenKind{TokenKindType: GE, TokenChars: []rune(GE), HasPayload: len([]rune(GE)) == 0}) {
	t.pushPairToken(TokenKind{TokenKindType: GE, TokenChars: []rune(GE), HasPayload: len([]rune(GE)) == 0})
} else {
	t.pushCharToken(TokenKind{TokenKindType: GE, TokenChars: []rune(GE), HasPayload: len([]rune(GE)) == 0})
}
break

在 InternalSpelExpressionParser的eatRelationalExpression方法中:

// 这里似乎有意把 > 当做 >=, < 当做 <= 处理,不知何意
if kindType == GT {
    eq := OpGE{Operator: &operator}
    return &eq, nil
}
if kindType == LT {
  eq := OpLE{Operator: &operator}
  return &eq, nil
}

不太能够理解作者的意图

EL 表达式或操作不行

context := spel.StandardEvaluationContext{} m := make(map[string]interface{}) m["create_user"] = "wsq_uid1" m["role_id"] = 1 m["money"] = 10000 m["things_num"] = 12 m["age"] = 18 context.SetVariables(m) parser := SpelExpressionParser{} expressionString := "#create_user=='lisi' || #age>=3" valueContext := parser.ParseExpression(expressionString).GetValueContext(&context) fmt.Println("结果为:", valueContext)
image

float64 类型的比较不生效

context := spel.StandardEvaluationContext{}
m := make(map[string]interface{})
    var ageFloat float64
ageFloat = 8.12
m["num"] = ageFloat
		context.SetVariables(m)
		parser := expression.SpelExpressionParser{}
		expressionString := "#num>=9f"
		valueContext := parser.ParseExpression(expressionString).GetValueContext(&context)
		So(valueContext, ShouldBeFalse)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.