Skip to content
Snippets Groups Projects
Select Git revision
  • 370cdf26a7d102e5c30221e1a974b0ad7b4acac7
  • master default protected
  • fix/PRXS-3401-ValidateValidationOpts
  • feature/PRXS-3383-CollectionsRankSortAPI
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • feature/PRXS-3218-HideTemplateActions
  • feature/PRXS-3234-PruneIdents
  • feature/3146-UpdateItemStorageInterface
  • feature/3274-ObjectIndexesFixes
  • feature/PRXS-3143-3235-ReferenceOptions
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

system.go

Blame
  • mongo.go 15.51 KiB
    package expr
    
    import (
    	"context"
    	"fmt"
    	"regexp"
    	"strings"
    
    	"github.com/antonmedv/expr"
    	"github.com/antonmedv/expr/ast"
    	compiler2 "github.com/antonmedv/expr/compiler"
    	"github.com/antonmedv/expr/conf"
    	"github.com/antonmedv/expr/parser"
    	"go.mongodb.org/mongo-driver/bson"
    )
    
    var geoTypes = map[string]string{
    	"box":     "$box",
    	"polygon": "$polygon",
    }
    
    func ConvertToMongo(ctx context.Context, exp string, env map[string]interface{}, identifierRenameFn func(string) string, ops ...expr.Option) (b bson.M, err error) {
    	if exp == "" {
    		return bson.M{}, nil
    	}
    	tree, err := parser.Parse(exp)
    	if err != nil {
    		return nil, err
    	}
    	return convertToMongo(ctx, tree, env, identifierRenameFn, ops...)
    }
    
    func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]interface{}, identifierRenameFn func(string) string, ops ...expr.Option) (b bson.M, err error) {
    	defer func() {
    		if r := recover(); r != nil {
    			err = fmt.Errorf("%v", r)
    		}
    	}()
    
    	if env == nil {
    		env = make(map[string]interface{})
    	}
    
    	env[EnvContextKey] = ctx
    	config := GetDefaultConfig(env)
    
    	for _, op := range ops {
    		op(config)
    	}
    
    	env = config.Env.(map[string]interface{})
    
    	if len(config.Visitors) >= 0 {
    		for _, v := range config.Visitors {
    			ast.Walk(&tree.Node, v)
    		}
    	}
    
    	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")
    	}
    	return v, nil
    }
    
    type compiler struct {
    	env                map[string]interface{}
    	tree               *parser.Tree
    	config             *conf.Config