Skip to content
Snippets Groups Projects
Commit 60c45b0c authored by Alena Petraki's avatar Alena Petraki :nail_care_tone1: Committed by Pavel Antonov
Browse files

feat(core): Добавлен вызов Visitors при компиляции expr-выражения в bson....

feat(core): Добавлен вызов Visitors при компиляции expr-выражения в bson. Добавлен метод Schema.SetSingleLocale

Close #PRXS-2656
parent 7e3853fc
Branches
Tags
No related merge requests found
......@@ -23,6 +23,7 @@ var geoTypes = map[string]string{
type MongoExprConfig struct {
Env map[string]any
IdentifierRenameFn func(s string) string
Visitors []ast.Visitor
Ops []expr.Option
}
......@@ -55,6 +56,7 @@ func convertToMongo(ctx context.Context, config *MongoExprConfig, tree *parser.T
env[EnvContextKey] = ctx
exprConfig := GetDefaultConfig(env)
exprConfig.Visitors = config.Visitors
for _, op := range config.Ops {
op(exprConfig)
......@@ -62,13 +64,16 @@ func convertToMongo(ctx context.Context, config *MongoExprConfig, tree *parser.T
env = exprConfig.Env.(map[string]interface{})
if len(exprConfig.Visitors) >= 0 {
for _, v := range exprConfig.Visitors {
ast.Walk(&tree.Node, v)
c := &compiler{
tree: tree,
env: env,
config: exprConfig,
identifierRenameFn: config.IdentifierRenameFn,
visitors: config.Visitors,
}
for _, v := range c.visitors {
ast.Walk(&tree.Node, v)
}
c := &compiler{tree: tree, env: env, config: exprConfig, identifierRenameFn: config.IdentifierRenameFn}
v, ok := c.compile(tree.Node).(bson.M)
if !ok || v == nil {
return nil, fmt.Errorf("invalid expression")
......@@ -81,6 +86,7 @@ type compiler struct {
tree *parser.Tree
config *conf.Config
identifierRenameFn func(string) string
visitors []ast.Visitor
}
func (c *compiler) eval(node ast.Node) interface{} {
......@@ -731,6 +737,9 @@ func (c *compiler) handleTryNode(node *ast.CallNode) (result interface{}) {
if err != nil {
panic(err)
}
for _, v := range c.visitors {
ast.Walk(&tree.Node, v)
}
subcompiler := &compiler{tree: tree, env: c.env, config: c.config, identifierRenameFn: c.identifierRenameFn}
result = subcompiler.compile(tree.Node).(bson.M)
return
......
......@@ -111,6 +111,11 @@ func (s Schema) SetMetadata(md map[string]string) *Schema {
return &s
}
func (f Schema) SetSingleLocale(r bool) *Schema {
f.SingleLocale = r
return &f
}
func (s *Schema) ConvertTypes() error {
b, err := s.MarshalJSON()
if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment