2018-09-18 22:42:38 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-26 22:32:50 +02:00
|
|
|
"runtime"
|
2019-10-12 03:30:42 +02:00
|
|
|
"strings"
|
2019-02-26 22:32:50 +02:00
|
|
|
|
2019-12-25 01:47:21 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-11-22 05:45:00 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/logging"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Program struct {
|
2019-10-11 23:31:23 +02:00
|
|
|
src string
|
|
|
|
body core.Expression
|
|
|
|
params map[string]struct{}
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:31:23 +02:00
|
|
|
func NewProgram(src string, body core.Expression, params map[string]struct{}) (*Program, error) {
|
2018-09-28 06:28:33 +02:00
|
|
|
if src == "" {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "source")
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
if body == nil {
|
2018-09-28 06:28:33 +02:00
|
|
|
return nil, core.Error(core.ErrMissedArgument, "body")
|
|
|
|
}
|
|
|
|
|
2019-10-11 23:31:23 +02:00
|
|
|
return &Program{src, body, params}, nil
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Program) Source() string {
|
|
|
|
return p.src
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:31:23 +02:00
|
|
|
func (p *Program) Params() []string {
|
|
|
|
res := make([]string, 0, len(p.params))
|
|
|
|
|
|
|
|
for name := range p.params {
|
|
|
|
res = append(res, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:45:33 +02:00
|
|
|
func (p *Program) Run(ctx context.Context, setters ...Option) (result []byte, err error) {
|
2019-10-11 23:31:23 +02:00
|
|
|
opts := NewOptions(setters)
|
|
|
|
|
2019-10-12 03:30:42 +02:00
|
|
|
err = p.validateParams(opts)
|
2019-10-11 23:31:23 +02:00
|
|
|
|
2019-10-12 03:30:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-11 23:31:23 +02:00
|
|
|
}
|
2018-11-22 05:45:00 +02:00
|
|
|
|
2019-10-11 23:31:23 +02:00
|
|
|
ctx = opts.WithContext(ctx)
|
2018-11-22 05:45:00 +02:00
|
|
|
logger := logging.FromContext(ctx)
|
|
|
|
|
2018-11-05 18:45:33 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// find out exactly what the error was and set err
|
|
|
|
switch x := r.(type) {
|
|
|
|
case string:
|
|
|
|
err = errors.New(x)
|
|
|
|
case error:
|
|
|
|
err = x
|
|
|
|
default:
|
|
|
|
err = errors.New("unknown panic")
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:45:00 +02:00
|
|
|
b := make([]byte, 0, 20)
|
|
|
|
runtime.Stack(b, true)
|
|
|
|
|
|
|
|
logger.Error().
|
|
|
|
Timestamp().
|
|
|
|
Err(err).
|
|
|
|
Str("stack", string(b)).
|
|
|
|
Msg("Panic")
|
|
|
|
|
2018-11-05 18:45:33 +02:00
|
|
|
result = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
scope, closeFn := core.NewRootScope()
|
2019-02-26 22:32:50 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
defer func() {
|
|
|
|
if err := closeFn(); err != nil {
|
|
|
|
logger.Error().
|
|
|
|
Timestamp().
|
|
|
|
Err(err).
|
2019-02-26 22:32:50 +02:00
|
|
|
Msg("closing root scope")
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
}()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-28 06:28:33 +02:00
|
|
|
out, err := p.body.Exec(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
js, _ := values.None.MarshalJSON()
|
|
|
|
|
|
|
|
return js, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.MarshalJSON()
|
|
|
|
}
|
2018-09-28 01:05:56 +02:00
|
|
|
|
2018-09-28 04:10:17 +02:00
|
|
|
func (p *Program) MustRun(ctx context.Context, setters ...Option) []byte {
|
2018-09-28 01:05:56 +02:00
|
|
|
out, err := p.Run(ctx, setters...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2019-10-12 03:30:42 +02:00
|
|
|
|
|
|
|
func (p *Program) validateParams(opts *Options) error {
|
|
|
|
if len(p.params) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:27:43 +02:00
|
|
|
// There might be no errors.
|
|
|
|
// Thus, we allocate this slice lazily, on a first error.
|
2019-10-12 03:30:42 +02:00
|
|
|
var missedParams []string
|
|
|
|
|
|
|
|
for n := range p.params {
|
|
|
|
_, exists := opts.params[n]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
if missedParams == nil {
|
|
|
|
missedParams = make([]string, 0, len(p.params))
|
|
|
|
}
|
|
|
|
|
|
|
|
missedParams = append(missedParams, "@"+n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missedParams) > 0 {
|
|
|
|
return core.Error(ErrMissedParam, strings.Join(missedParams, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|