-
Notifications
You must be signed in to change notification settings - Fork 2
/
executors.go
84 lines (64 loc) · 2.03 KB
/
executors.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
83
84
package sdkexecutor
import (
"context"
"fmt"
"strings"
"go.autokitteh.dev/autokitteh/internal/kittehs"
"go.autokitteh.dev/autokitteh/sdk/sdkerrors"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
)
type Executors struct {
values map[string]map[string]sdktypes.Value // scope -> {values}; scope is usually module name.
callers map[string]Caller // xid -> caller
}
func (ms *Executors) Call(ctx context.Context, v sdktypes.Value, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
if !v.IsFunction() {
return sdktypes.InvalidValue, sdkerrors.ErrInvalidArgument{}
}
xid := v.GetFunction().ExecutorID()
c := ms.GetCaller(xid)
if c == nil {
return sdktypes.InvalidValue, fmt.Errorf("executor not found: %w", sdkerrors.ErrNotFound)
}
return c.Call(ctx, v, args, kwargs)
}
func (ms *Executors) AddExecutor(name string, x Executor) error {
if err := ms.AddCaller(x.ExecutorID(), x); err != nil {
return fmt.Errorf("add caller: %w", err)
}
if err := ms.AddValues(name, x.Values()); err != nil {
return fmt.Errorf("add values: %w", err)
}
return nil
}
func (ms *Executors) AddCaller(xid sdktypes.ExecutorID, c Caller) error {
if ms.callers == nil {
ms.callers = make(map[string]Caller)
}
if _, ok := ms.callers[xid.String()]; ok {
return sdkerrors.ErrConflict
}
ms.callers[xid.String()] = c
return nil
}
func (ms *Executors) AddValues(scope string, vs map[string]sdktypes.Value) error {
if ms.values == nil {
ms.values = make(map[string]map[string]sdktypes.Value)
}
if _, ok := ms.values[scope]; ok {
return sdkerrors.ErrConflict
}
ms.values[scope] = vs
return nil
}
func (ms *Executors) ValuesForScopePrefix(prefix string) map[string]map[string]sdktypes.Value {
return kittehs.FilterMapKeys(ms.values, func(n string) bool {
return strings.HasPrefix(n, prefix)
})
}
func (ms *Executors) GetCaller(xid sdktypes.ExecutorID) Caller {
return ms.callers[xid.String()]
}
func (ms *Executors) GetValues(scope string) map[string]sdktypes.Value {
return ms.values[scope]
}