-
Notifications
You must be signed in to change notification settings - Fork 14
/
binder.go
55 lines (45 loc) · 1.06 KB
/
binder.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
package lua
import (
"strings"
"github.com/krakendio/binder"
)
type Binder = binder.Binder
type Context = binder.Context
type Handler = binder.Handler
type BinderWrapper struct {
binder *binder.Binder
sourceMap *SourceMap
}
func NewBinderWrapper(binderOptions binder.Options) BinderWrapper {
b := binder.New(binderOptions)
m := NewSourceMap()
return BinderWrapper{b, &m}
}
func (b BinderWrapper) GetBinder() *binder.Binder {
return b.binder
}
func (b BinderWrapper) WithConfig(cfg *Config) error {
var srcBlock []string
for _, source := range cfg.Sources {
src, ok := cfg.Get(source)
if !ok {
return ErrUnknownSource(source)
}
srcBlock = append(srcBlock, src)
b.sourceMap.Append(source, src)
}
if len(srcBlock) > 0 {
if err := b.binder.DoString(strings.Join(srcBlock, "\n")); err != nil {
return ToError(err, b.sourceMap)
}
}
return nil
}
func (b BinderWrapper) WithCode(key string, src string) error {
if err := b.binder.DoString(src); err != nil {
v := *b.sourceMap
v.Append(key, src)
return ToError(err, &v)
}
return nil
}