generated from TBD54566975/tbd-project-template
/
ref.go
87 lines (73 loc) 路 1.89 KB
/
ref.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
85
86
87
package schema
import (
"google.golang.org/protobuf/proto"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/internal/slices"
)
// Ref is an untyped reference to a symbol.
type Ref struct {
Pos Position `parser:"" protobuf:"1,optional"`
Module string `parser:"(@Ident '.')?" protobuf:"3"`
Name string `parser:"@Ident" protobuf:"2"`
// Only used for data references.
TypeParameters []Type `parser:"[ '<' @@ (',' @@)* '>' ]" protobuf:"4"`
}
// RefKey is a map key for a reference.
type RefKey struct {
Module string
Name string
}
func (r RefKey) String() string { return makeRef(r.Module, r.Name) }
func (r Ref) ToRefKey() RefKey {
return RefKey{Module: r.Module, Name: r.Name}
}
func (r *Ref) ToProto() proto.Message {
return &schemapb.Ref{
Pos: posToProto(r.Pos),
Module: r.Module,
Name: r.Name,
TypeParameters: slices.Map(r.TypeParameters, typeToProto),
}
}
func (r *Ref) schemaChildren() []Node {
out := make([]Node, 0, len(r.TypeParameters))
for _, t := range r.TypeParameters {
out = append(out, t)
}
return out
}
func (r *Ref) schemaType() {}
var _ Type = (*Ref)(nil)
func (r *Ref) Position() Position { return r.Pos }
func (r *Ref) String() string {
out := makeRef(r.Module, r.Name)
if len(r.TypeParameters) > 0 {
out += "<"
for i, t := range r.TypeParameters {
if i != 0 {
out += ", "
}
out += t.String()
}
out += ">"
}
return out
}
func RefFromProto(s *schemapb.Ref) *Ref {
return &Ref{
Pos: posFromProto(s.Pos),
Name: s.Name,
Module: s.Module,
TypeParameters: slices.Map(s.TypeParameters, typeToSchema),
}
}
func ParseRef(ref string) (*Ref, error) {
return refParser.ParseString("", ref)
}
func refListToSchema(s []*schemapb.Ref) []*Ref {
var out []*Ref
for _, n := range s {
out = append(out, RefFromProto(n))
}
return out
}