Skip to content
Snippets Groups Projects
Select Git revision
  • 60c45b0cb14ae81835859ce9e5303a7fba5b733b
  • master default protected
  • feature/PRXS-3421-ImplementNewRefAPI
  • refactor/PRXS-3053-Files
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsSort
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • PRXS-3421-RecursiveReferences
  • 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
  • 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

mongo.go

Blame
  • mongo.go 18.55 KiB
    package expr
    
    import (
    	"context"
    	"fmt"
    	"regexp"
    	"strconv"
    	"strings"
    
    	"github.com/expr-lang/expr"
    	"github.com/expr-lang/expr/ast"
    	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"
    )
    
    var geoTypes = map[string]string{
    	"box":     "$box",
    	"polygon": "$polygon",
    }
    
    type MongoExprConfig struct {
    	Env                map[string]any
    	IdentifierRenameFn func(s string) string
    	Visitors           []ast.Visitor
    	Ops                []expr.Option
    }
    
    func ConvertToMongo(ctx context.Context, config *MongoExprConfig, expressions ...string) (b bson.M, err error) {
    	if len(expressions) == 0 {
    		return bson.M{}, nil
    	}
    	tree, err := parser.Parse("(" + strings.Join(expressions, ") && (") + ")")
    	if err != nil {
    		return nil, err
    	}
    	return convertToMongo(ctx, config, tree)
    }
    
    func convertToMongo(ctx context.Context, config *MongoExprConfig, tree *parser.Tree) (b bson.M, err error) {
    	defer func() {
    		if r := recover(); r != nil {
    			err = fmt.Errorf("%v", r)
    		}
    	}()
    
    	if config == nil {
    		config = new(MongoExprConfig)
    	}
    
    	env := config.Env
    	if env == nil {
    		env = make(map[string]interface{})
    	}
    
    	env[EnvContextKey] = ctx
    	exprConfig := GetDefaultConfig(env)
    	exprConfig.Visitors = config.Visitors
    
    	for _, op := range config.Ops {
    		op(exprConfig)
    	}
    
    	env = exprConfig.Env.(map[string]interface{})
    
    	c := &compiler{
    		tree:               tree,
    		env:                env,
    		config:             exprConfig,