generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
secrets.go
38 lines (30 loc) 路 1.01 KB
/
secrets.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
package ftl
import (
"context"
"fmt"
"github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
"github.com/TBD54566975/ftl/internal/modulecontext"
)
// SecretType is a type that can be used as a secret value.
type SecretType interface{ any }
// Secret declares a typed secret for the current module.
func Secret[T SecretType](name string) SecretValue[T] {
module := callerModule()
return SecretValue[T]{reflection.Ref{Module: module, Name: name}}
}
// SecretValue is a typed secret for the current module.
type SecretValue[T SecretType] struct {
reflection.Ref
}
func (s SecretValue[T]) String() string { return fmt.Sprintf("secret \"%s\"", s.Ref) }
func (s SecretValue[T]) GoString() string {
var t T
return fmt.Sprintf("ftl.SecretValue[%T](\"%s\")", t, s.Ref)
}
// Get returns the value of the secret from FTL.
func (s SecretValue[T]) Get(ctx context.Context) (out T) {
if err := modulecontext.FromContext(ctx).GetSecret(s.Name, &out); err != nil {
panic(fmt.Errorf("failed to get %s: %w", s, err))
}
return
}