Files
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

118 lines
2.9 KiB
Go

package values
import (
"context"
"fmt"
"github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/modern-go/reflect2"
)
func makeInvalidExprErr(field *types.ModuleField, expr string, err error) types.RecordValueError {
return types.RecordValueError{
Kind: "valueExpression",
Message: fmt.Sprintf("invalid expression %q: %v", expr, err.Error()),
Meta: map[string]interface{}{"field": field.Name},
}
}
func makeExprEvalErr(field *types.ModuleField, expr string, err error) types.RecordValueError {
return types.RecordValueError{
Kind: "valueExpression",
Message: fmt.Sprintf("failed to evaluate formula expression %q: %v", expr, err.Error()),
Meta: map[string]interface{}{"field": field.Name},
}
}
func makeValueExprIncompErr(field *types.ModuleField) types.RecordValueError {
return types.RecordValueError{
Kind: "evaluatedValueIncompatible",
Message: "evaluated results incompatible",
Meta: map[string]interface{}{"field": field.Name},
}
}
// Expression evaluates expression in ModuleField.Expressions.Value and
// assigns results to the record on that field
func Expression(ctx context.Context, m *types.Module, r *types.Record, old *types.Record, rve *types.RecordValueErrorSet) {
var (
exprParser = expr.Parser()
scope = make(map[string]interface{})
reserved = map[string]bool{
"new": true,
"old": true,
}
)
// base scope with field=value(s) from new record
scope = r.Values.Dict(m.Fields)
// new record
r.SetModule(m)
scope["new"] = r.Dict()
if old != nil {
// old values on record (before update)
// this will not be set for new records
old.SetModule(m)
scope["old"] = old.Dict()
}
for _, f := range m.Fields {
if f.Expressions.ValueExpr == "" {
continue
}
expr := f.Expressions.ValueExpr
eval, err := exprParser.NewEvaluable(expr)
if err != nil {
rve.Push(makeInvalidExprErr(f, expr, err))
return
}
tmp, err := eval(ctx, scope)
if err != nil {
rve.Push(makeExprEvalErr(f, expr, err))
return
}
var strings []string
// When the expression results in null (nil) delete that record value.
// The below Values.Replace already does it so we simply ignore strings
if !reflect2.IsNil(tmp) {
if values, isSlice := tmp.([]interface{}); isSlice {
if !f.Multi {
rve.Push(makeValueExprIncompErr(f))
continue
}
strings = make([]string, len(values))
for i, value := range values {
strings[i] = sanitize(f, value)
}
} else {
if f.Multi {
rve.Push(makeValueExprIncompErr(f))
continue
}
strings = []string{sanitize(f, tmp)}
}
}
r.Values = r.Values.Replace(f.Name, strings...)
if !reserved[f.Name] {
// make sure we do not overrider reserved fields
scope[f.Name] = tmp
}
// Reset $new with updated data
r.SetModule(m)
scope["new"] = r.Dict()
}
}