generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
51 lines (41 loc) · 1.61 KB
/
api.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
package internal
import (
"context"
"github.com/TBD54566975/ftl/backend/schema"
)
// FTL is the interface that the FTL runtime provides to user code.
//
// In production, the FTL runtime will provide an implementation of this
// interface that communicates with the Controller over gRPC.
//
// In testing code, the implementation will inject fakes and other test
// implementations.
type FTL interface {
// FSMSend sends an event to an instance of an FSM.
FSMSend(ctx context.Context, fsm, instance string, data any) error
// PublishEvent sends an event to a pubsub topic.
PublishEvent(ctx context.Context, topic *schema.Ref, event any) error
// CallMap calls Get on an instance of an ftl.Map.
//
// "mapper" is a pointer to an instance of an ftl.MapHandle. "value" is the
// value being mapped. "mapImpl" is a function that will be called to
// compute the mapped value.
CallMap(ctx context.Context, mapper any, value any, mapImpl func(context.Context) (any, error)) any
// GetConfig unmarshals a configuration value into dest.
GetConfig(ctx context.Context, name string, dest any) error
// GetSecret unmarshals a secret value into dest.
GetSecret(ctx context.Context, name string, dest any) error
}
type ftlContextKey struct{}
// WithContext returns a new context with the FTL instance.
func WithContext(ctx context.Context, ftl FTL) context.Context {
return context.WithValue(ctx, ftlContextKey{}, ftl)
}
// FromContext returns the FTL instance from the context.
func FromContext(ctx context.Context) FTL {
ftl, ok := ctx.Value(ftlContextKey{}).(FTL)
if !ok {
panic("FTL not found in context")
}
return ftl
}