-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
ferret.go
82 lines (60 loc) · 1.68 KB
/
ferret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ferret
import (
"context"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type Instance struct {
compiler *compiler.Compiler
drivers *drivers.Container
}
func New(setters ...Option) *Instance {
opts := NewOptions(setters)
return &Instance{
compiler: compiler.New(opts.compiler...),
drivers: drivers.NewContainer(),
}
}
func (i *Instance) Functions() core.Namespace {
return i.compiler
}
func (i *Instance) Drivers() *drivers.Container {
return i.drivers
}
func (i *Instance) Compile(query string) (*runtime.Program, error) {
return i.compiler.Compile(query)
}
func (i *Instance) MustCompile(query string) *runtime.Program {
return i.compiler.MustCompile(query)
}
func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Option) ([]byte, error) {
p, err := i.Compile(query)
if err != nil {
return nil, err
}
ctx = i.drivers.WithContext(ctx)
return p.Run(ctx, opts...)
}
func (i *Instance) MustExec(ctx context.Context, query string, opts ...runtime.Option) []byte {
out, err := i.Exec(ctx, query, opts...)
if err != nil {
panic(err)
}
return out
}
func (i *Instance) Run(ctx context.Context, program *runtime.Program, opts ...runtime.Option) ([]byte, error) {
if program == nil {
return nil, core.Error(core.ErrInvalidArgument, "program")
}
ctx = i.drivers.WithContext(ctx)
return program.Run(ctx, opts...)
}
func (i *Instance) MustRun(ctx context.Context, program *runtime.Program, opts ...runtime.Option) []byte {
out, err := i.Run(ctx, program, opts...)
if err != nil {
panic(err)
}
return out
}