diff --git a/pkg/expr/expr.go b/pkg/expr/expr.go index af5495f8a7a8cc777c0b99d3e60d4a68ef298eba..18b846928da768657a78f32d4510085bdae27a53 100644 --- a/pkg/expr/expr.go +++ b/pkg/expr/expr.go @@ -5,7 +5,7 @@ import ( "strings" "git.perx.ru/perxis/perxis-go/pkg/data" - "github.com/expr-lang/expr/compiler" + exprcompiler "github.com/expr-lang/expr/compiler" "github.com/expr-lang/expr/parser" "github.com/expr-lang/expr/vm" "golang.org/x/net/context" @@ -39,7 +39,7 @@ func Eval(ctx context.Context, input string, env map[string]interface{}) (interf env, _ = cfg.Env.(map[string]interface{}) - program, err := compiler.Compile(tree, GetDefaultConfig(env)) + program, err := exprcompiler.Compile(tree, GetDefaultConfig(env)) if err != nil { return nil, err } diff --git a/pkg/expr/mongo.go b/pkg/expr/mongo.go index 5a7b929e96a62e64c7a55639d333e3fc042db3d3..11f124d874d19c00a09c31197417400366ccda98 100644 --- a/pkg/expr/mongo.go +++ b/pkg/expr/mongo.go @@ -8,7 +8,7 @@ import ( "github.com/expr-lang/expr" "github.com/expr-lang/expr/ast" - "github.com/expr-lang/expr/compiler" + exprcompiler "github.com/expr-lang/expr/compiler" "github.com/expr-lang/expr/conf" "github.com/expr-lang/expr/parser" "go.mongodb.org/mongo-driver/bson" @@ -56,7 +56,7 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter } } - c := &mongoCompiler{tree: tree, env: env, config: config, identifierRenameFn: identifierRenameFn} + c := &compiler{tree: tree, env: env, config: config, identifierRenameFn: identifierRenameFn} v, ok := c.compile(tree.Node).(bson.M) if !ok || v == nil { return nil, fmt.Errorf("invalid expression") @@ -64,19 +64,19 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter return v, nil } -type mongoCompiler struct { +type compiler struct { env map[string]interface{} tree *parser.Tree config *conf.Config identifierRenameFn func(string) string } -func (c *mongoCompiler) eval(node ast.Node) interface{} { +func (c *compiler) eval(node ast.Node) interface{} { t := &parser.Tree{ Node: node, Source: c.tree.Source, } - prg, err := compiler.Compile(t, c.config) + prg, err := exprcompiler.Compile(t, c.config) if err != nil { panic(fmt.Sprintf("compile error %s", err.Error())) } @@ -87,7 +87,7 @@ func (c *mongoCompiler) eval(node ast.Node) interface{} { return ret } -func (c *mongoCompiler) compile(node ast.Node) interface{} { +func (c *compiler) compile(node ast.Node) interface{} { switch n := node.(type) { case *ast.NilNode: return c.NilNode(n) @@ -136,11 +136,11 @@ func (c *mongoCompiler) compile(node ast.Node) interface{} { } } -func (c *mongoCompiler) NilNode(node *ast.NilNode) interface{} { +func (c *compiler) NilNode(node *ast.NilNode) interface{} { return nil } -func (c *mongoCompiler) IdentifierNode(node *ast.IdentifierNode) string { +func (c *compiler) IdentifierNode(node *ast.IdentifierNode) string { identifier := node.Value if c.identifierRenameFn != nil { identifier = c.identifierRenameFn(identifier) @@ -148,7 +148,7 @@ func (c *mongoCompiler) IdentifierNode(node *ast.IdentifierNode) string { return identifier } -func (c *mongoCompiler) IntegerNode(node *ast.IntegerNode) int { +func (c *compiler) IntegerNode(node *ast.IntegerNode) int { return node.Value //t := node.Type() //if t == nil { @@ -189,23 +189,23 @@ func (c *mongoCompiler) IntegerNode(node *ast.IntegerNode) int { //} } -func (c *mongoCompiler) FloatNode(node *ast.FloatNode) float64 { +func (c *compiler) FloatNode(node *ast.FloatNode) float64 { return node.Value } -func (c *mongoCompiler) BoolNode(node *ast.BoolNode) bool { +func (c *compiler) BoolNode(node *ast.BoolNode) bool { return node.Value } -func (c *mongoCompiler) StringNode(node *ast.StringNode) string { +func (c *compiler) StringNode(node *ast.StringNode) string { return node.Value } -func (c *mongoCompiler) ConstantNode(node *ast.ConstantNode) interface{} { +func (c *compiler) ConstantNode(node *ast.ConstantNode) interface{} { return node.Value } -func (c *mongoCompiler) UnaryNode(node *ast.UnaryNode) interface{} { +func (c *compiler) UnaryNode(node *ast.UnaryNode) interface{} { op := c.compile(node.Node) switch node.Operator { @@ -217,7 +217,7 @@ func (c *mongoCompiler) UnaryNode(node *ast.UnaryNode) interface{} { } } -func (c *mongoCompiler) identifier(node ast.Node) string { +func (c *compiler) identifier(node ast.Node) string { switch l := node.(type) { case *ast.MemberNode: return c.MemberNode(l) @@ -227,7 +227,7 @@ func (c *mongoCompiler) identifier(node ast.Node) string { panic(fmt.Sprintf("incorrect identifier node (%v) ", ast.Dump(node))) } -func (c *mongoCompiler) BinaryNode(node *ast.BinaryNode) interface{} { +func (c *compiler) BinaryNode(node *ast.BinaryNode) interface{} { switch node.Operator { case "==": return bson.M{c.identifier(node.Left): c.eval(node.Right)} @@ -322,11 +322,11 @@ func (c *mongoCompiler) BinaryNode(node *ast.BinaryNode) interface{} { } } -func (c *mongoCompiler) ChainNode(node *ast.ChainNode) string { +func (c *compiler) ChainNode(node *ast.ChainNode) string { panic("unsupported chain node") } -func (c *mongoCompiler) MemberNode(node *ast.MemberNode) string { +func (c *compiler) MemberNode(node *ast.MemberNode) string { v := c.compile(node.Node) if val, ok := v.(string); ok { return fmt.Sprintf("%s.%s", val, node.Property) @@ -334,11 +334,11 @@ func (c *mongoCompiler) MemberNode(node *ast.MemberNode) string { panic(fmt.Sprintf("unsupported property for %v", ast.Dump(node.Node))) } -func (c *mongoCompiler) SliceNode(node *ast.SliceNode) interface{} { +func (c *compiler) SliceNode(node *ast.SliceNode) interface{} { panic("unsupported slice node") } -func (c *mongoCompiler) CallNode(node *ast.CallNode) interface{} { +func (c *compiler) CallNode(node *ast.CallNode) interface{} { switch node.Callee.String() { case "search", "q": val := c.compile(node.Arguments[0]) @@ -438,7 +438,7 @@ func (c *mongoCompiler) CallNode(node *ast.CallNode) interface{} { //c.emit(op, c.makeConstant(Call{Name: node.Name, Size: len(node.Arguments)})...) } -func (c *mongoCompiler) BuiltinNode(node *ast.BuiltinNode) interface{} { +func (c *compiler) BuiltinNode(node *ast.BuiltinNode) interface{} { panic("unsupported builin node") //switch node.Name { //case "len": @@ -583,18 +583,18 @@ func (c *mongoCompiler) BuiltinNode(node *ast.BuiltinNode) interface{} { // return size //} -func (c *mongoCompiler) ClosureNode(node *ast.ClosureNode) interface{} { +func (c *compiler) ClosureNode(node *ast.ClosureNode) interface{} { return c.compile(node.Node) } -func (c *mongoCompiler) PointerNode(node *ast.PointerNode) interface{} { +func (c *compiler) PointerNode(node *ast.PointerNode) interface{} { panic("unsupported pointer node") //c.emit(OpLoad, c.makeConstant("array")...) //c.emit(OpLoad, c.makeConstant("i")...) //c.emit(OpIndex) } -func (c *mongoCompiler) ConditionalNode(node *ast.ConditionalNode) interface{} { +func (c *compiler) ConditionalNode(node *ast.ConditionalNode) interface{} { panic("unsupported conditional node") //c.compile(node.Cond) //otherwise := c.emit(OpJumpIfFalse, c.placeholder()...) @@ -610,11 +610,11 @@ func (c *mongoCompiler) ConditionalNode(node *ast.ConditionalNode) interface{} { //c.patchJump(end) } -func (c *mongoCompiler) VariableDeclaratorNode(node *ast.VariableDeclaratorNode) int { +func (c *compiler) VariableDeclaratorNode(node *ast.VariableDeclaratorNode) int { panic("unsupported variable declarator node ") } -func (c *mongoCompiler) ArrayNode(node *ast.ArrayNode) interface{} { +func (c *compiler) ArrayNode(node *ast.ArrayNode) interface{} { panic("unsupported array node") //for _, node := range node.Nodes { // c.compile(node) @@ -624,7 +624,7 @@ func (c *mongoCompiler) ArrayNode(node *ast.ArrayNode) interface{} { //c.emit(OpArray) } -func (c *mongoCompiler) MapNode(node *ast.MapNode) interface{} { +func (c *compiler) MapNode(node *ast.MapNode) interface{} { panic("unsupported map node") //for _, pair := range node.Pairs { // c.compile(pair) @@ -634,7 +634,7 @@ func (c *mongoCompiler) MapNode(node *ast.MapNode) interface{} { //c.emit(OpMap) } -func (c *mongoCompiler) PairNode(node *ast.PairNode) interface{} { +func (c *compiler) PairNode(node *ast.PairNode) interface{} { panic("unsupported pair node") //c.compile(node.Key) //c.compile(node.Value)