-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
48 lines (37 loc) · 1.04 KB
/
context.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
package sdkmodule
import (
"context"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
)
var ctxKey = struct{}{}
type callContext struct {
module *module
fnv sdktypes.Value
}
func wrapCallContext(ctx context.Context, m *module, fnv sdktypes.Value) context.Context {
return context.WithValue(ctx, ctxKey, &callContext{
module: m,
fnv: fnv,
})
}
func callContextFromContext(ctx context.Context) *callContext {
if c, ok := ctx.Value(ctxKey).(*callContext); ok {
return c
}
return &callContext{}
}
func FunctionValueFromContext(ctx context.Context) sdktypes.Value {
return callContextFromContext(ctx).fnv
}
func FunctionDataFromContext(ctx context.Context) []byte {
if fnv := FunctionValueFromContext(ctx); fnv.IsValid() {
return fnv.GetFunction().Data()
}
return nil
}
func FunctionConnectionIDFromContext(ctx context.Context) (sdktypes.ConnectionID, error) {
if fnv := FunctionValueFromContext(ctx); fnv.IsValid() {
return sdktypes.StrictParseConnectionID(string(fnv.GetFunction().Data()))
}
return sdktypes.InvalidConnectionID, nil
}