generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
63 lines (50 loc) 路 1.67 KB
/
types.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
package ftl
import (
"context"
"fmt"
"strings"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)
// Handle represents a resource that can be retrieved such as a database connection, secret, etc.
type Handle[T any] interface {
Get(ctx context.Context) T
}
// Unit is a type that has no value.
//
// It can be used as a parameter or return value to indicate that a function
// does not accept or return any value.
type Unit struct{}
// Ref is an untyped reference to a symbol.
type Ref struct {
Module string `json:"module"`
Name string `json:"name"`
}
func ParseRef(ref string) (Ref, error) {
var out Ref
if err := out.UnmarshalText([]byte(ref)); err != nil {
return out, err
}
return out, nil
}
func (v *Ref) UnmarshalText(text []byte) error {
parts := strings.Split(string(text), ".")
if len(parts) != 2 {
return fmt.Errorf("invalid reference %q", string(text))
}
v.Module = parts[0]
v.Name = parts[1]
return nil
}
func (v Ref) String() string { return v.Module + "." + v.Name }
func (v Ref) ToProto() *schemapb.Ref {
return &schemapb.Ref{Module: v.Module, Name: v.Name}
}
func RefFromProto(p *schemapb.Ref) Ref { return Ref{Module: p.Module, Name: p.Name} }
// A Verb is a function that accepts input and returns output.
type Verb[Req, Resp any] func(context.Context, Req) (Resp, error)
// A Sink is a function that accepts input but returns nothing.
type Sink[Req any] func(context.Context, Req) error
// A Source is a function that does not accept input but returns output.
type Source[Resp any] func(context.Context) (Resp, error)
// An Empty is a function that does not accept input or return output.
type Empty func(context.Context) error