snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
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
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
This commit is contained in:
128
server/pkg/expr/parser.go
Normal file
128
server/pkg/expr/parser.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/PaesslerAG/gval"
|
||||
)
|
||||
|
||||
type (
|
||||
Parsable interface {
|
||||
Parse(string) (Evaluable, error)
|
||||
ParseEvaluators(ee ...Evaluator) error
|
||||
}
|
||||
|
||||
Evaluable interface {
|
||||
Eval(context.Context, *Vars) (interface{}, error)
|
||||
Test(context.Context, *Vars) (bool, error)
|
||||
}
|
||||
|
||||
Evaluator interface {
|
||||
GetExpr() string
|
||||
SetEval(eval Evaluable)
|
||||
}
|
||||
|
||||
gvalParser struct {
|
||||
lang gval.Language
|
||||
}
|
||||
|
||||
gvalEval struct {
|
||||
evaluable gval.Evaluable
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
parser gval.Language
|
||||
initialized = false
|
||||
)
|
||||
|
||||
func Init(ext ...func() []gval.Language) {
|
||||
aux := make([]gval.Language, 0, len(ext))
|
||||
|
||||
for _, e := range ext {
|
||||
aux = append(aux, e()...)
|
||||
}
|
||||
|
||||
initialized = true
|
||||
parser = gval.Full(aux...)
|
||||
}
|
||||
|
||||
// Parser returns global parser instance
|
||||
func Parser() gval.Language {
|
||||
if !initialized {
|
||||
Init(AllFunctions)
|
||||
}
|
||||
return parser
|
||||
}
|
||||
|
||||
func NewParser() Parsable {
|
||||
return NewGvalParser()
|
||||
}
|
||||
|
||||
func NewGvalParser() *gvalParser {
|
||||
return &gvalParser{lang: Parser()}
|
||||
}
|
||||
|
||||
func (p *gvalParser) Parse(expr string) (Evaluable, error) {
|
||||
var (
|
||||
ge = &gvalEval{}
|
||||
err error
|
||||
)
|
||||
|
||||
if ge.evaluable, err = p.lang.NewEvaluable(expr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ge, err
|
||||
}
|
||||
|
||||
func (p *gvalParser) ParseEvaluators(ee ...Evaluator) error {
|
||||
for _, e := range ee {
|
||||
evaluable, err := p.Parse(e.GetExpr())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.SetEval(evaluable)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *gvalEval) Eval(ctx context.Context, scope *Vars) (interface{}, error) {
|
||||
return e.evaluable(ctx, scope)
|
||||
}
|
||||
|
||||
func (e *gvalEval) Test(ctx context.Context, scope *Vars) (bool, error) {
|
||||
r, err := e.evaluable(ctx, scope)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return !isEmpty(r), nil
|
||||
}
|
||||
|
||||
func AllFunctions() []gval.Language {
|
||||
ff := make([]gval.Language, 0, 100)
|
||||
|
||||
ff = append(ff, GenericFunctions()...)
|
||||
ff = append(ff, StringFunctions()...)
|
||||
ff = append(ff, JsonFunctions()...)
|
||||
ff = append(ff, NumericFunctions()...)
|
||||
ff = append(ff, TimeFunctions()...)
|
||||
ff = append(ff, ArrayFunctions()...)
|
||||
ff = append(ff, KvFunctions()...)
|
||||
|
||||
return ff
|
||||
}
|
||||
|
||||
// utility function for examples
|
||||
func eval(e string, p interface{}) {
|
||||
result, err := Parser().Evaluate(e, p)
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
} else {
|
||||
fmt.Printf("%+v", UntypedValue(result))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user