1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/program.go

120 lines
2.1 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package runtime
import (
"context"
"runtime"
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"
2018-11-05 18:45:33 +02:00
"github.com/pkg/errors"
2018-09-18 22:42:38 +02:00
)
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)
// Check params
if len(p.params) > 0 && len(opts.params) == 0 {
return nil, ErrMissedParams
}
for n := range p.params {
_, exists := opts.params[n]
if !exists {
return nil, errors.Wrap(ErrMissedParam, n)
}
}
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()
defer func() {
if err := closeFn(); err != nil {
logger.Error().
Timestamp().
Err(err).
Msg("closing root scope")
}
}()
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 04:10:17 +02:00
func (p *Program) MustRun(ctx context.Context, setters ...Option) []byte {
out, err := p.Run(ctx, setters...)
if err != nil {
panic(err)
}
return out
}