Skip to content
Snippets Groups Projects
Select Git revision
  • d12a6fafffa0d2bc28afe7fcdd61961f8ef0e475
  • master default protected
  • feature/PRXS-3383-CollectionsRankSortAPI
  • 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
  • feature/PRXS-3143-3237-ExecuteOptions
  • feature/3149-LocaleCodeAsID-Implementation
  • 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

expr.go

Blame
  • expr.go 1.48 KiB
    package expr
    
    import (
    	"regexp"
    	"strings"
    
    	"git.perx.ru/perxis/perxis-go/pkg/data"
    	compiler2 "github.com/antonmedv/expr/compiler"
    	"github.com/antonmedv/expr/parser"
    	"github.com/antonmedv/expr/vm"
    	"golang.org/x/net/context"
    )
    
    var (
    	additionalFunctions = []string{"contains", "startsWith", "endsWith", "and", "or", "in", "not"}
    	isExpression        = regexp.MustCompile(`[()}{<>=|&%*+\-\/\]\[\\]`).MatchString
    )
    
    const EnvContextKey = "$context"
    
    func Eval(ctx context.Context, input string, env map[string]interface{}) (interface{}, error) {
    	tree, err := parser.Parse(input)
    	if err != nil {
    		return nil, err
    	}
    
    	e := GetEnv(ctx)
    
    	if e == nil {
    		e = make(map[string]interface{})
    	}
    
    	for k, v := range env {
    		e[k] = v
    	}
    
    	e[EnvContextKey] = ctx
    	cfg := GetDefaultConfig(e)
    
    	env, _ = cfg.Env.(map[string]interface{})
    
    	program, err := compiler2.Compile(tree, nil)
    	if err != nil {
    		return nil, err
    	}
    
    	output, err := vm.Run(program, env)
    	if err != nil {
    		return nil, err
    	}
    
    	return output, nil
    }
    
    func EvalKV(ctx context.Context, input string, kv ...interface{}) (interface{}, error) {
    	m := make(map[string]interface{})
    
    	for i := 0; i < len(kv)/2; i++ {
    		key, ok := kv[i].(string)
    		if !ok {
    			panic("key should be string")
    		}
    		m[key] = kv[i+1]
    	}
    
    	return Eval(ctx, input, m)
    }
    
    func IsExpression(input string) bool {
    	if isExpression(input) {