-
Notifications
You must be signed in to change notification settings - Fork 0
/
inout.go
71 lines (56 loc) · 1.07 KB
/
inout.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
package loong
import "context"
type InOut struct {
in *LazyBag
out Var
}
func newInOut() InOut {
return InOut{
in: NewLazyBag(),
out: NewVar(),
}
}
func (io InOut) GetInput(key string) (any, bool) {
return io.in.Get(key)
}
func (io *InOut) SetResult(key string, val any) {
io.out.Set(key, val)
}
func (io InOut) Get(key string) (any, bool) {
k, _ := Expr(key)
return io.out.Get(k)
}
func (io *InOut) Set(key string, val any) {
io.in.Set(key, val)
}
type LazyGetFunc func(key string) (any, error)
type LazyBag struct {
m map[string]any
}
func NewLazyBag() *LazyBag {
return &LazyBag{
m: make(map[string]any),
}
}
func (b *LazyBag) Get(key string) (any, bool) {
v, ok := b.m[key]
if !ok {
return nil, false
}
if f, ok := v.(LazyGetFunc); ok {
a, err := f(key)
if err != nil {
panic(err)
}
return a, true
}
return v, true
}
func (b *LazyBag) Set(key string, val any) {
b.m[key] = val
}
func lazy(ctx context.Context, eval ActivationEvaluator, el string) LazyGetFunc {
return func(_ string) (any, error) {
return eval.Eval(ctx, el)
}
}