-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetchers.go
224 lines (185 loc) · 5.74 KB
/
fetchers.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cuefrontend
import (
"context"
"fmt"
"io/fs"
"strings"
"cuelang.org/go/cue"
"google.golang.org/protobuf/types/descriptorpb"
"namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type cueWorkspace struct {
ServerPath string `json:"serverPath"`
}
type cueServerReference struct {
PackageName string `json:"packageName"`
Id string `json:"id"`
Name string `json:"name"`
Endpoints []cueEndpoint `json:"endpoints"`
}
type cueEndpoint struct {
Type string `json:"type"`
ServiceName string `json:"serviceName"`
AllocatedName string `json:"allocatedName"`
FullyQualifiedName string `json:"fullyQualifiedName"`
ContainerPort int32 `json:"containerPort"`
}
func FetchServer(packages pkggraph.PackageLoader, stack pkggraph.StackEndpoints) FetcherFunc {
return func(ctx context.Context, v cue.Value) (interface{}, error) {
var server cueServerReference
if err := v.Decode(&server); err != nil {
return nil, err
}
pkg, err := packages.LoadByName(ctx, schema.PackageName(server.PackageName))
if err != nil {
return nil, err
}
if pkg.Server == nil {
return nil, fnerrors.BadInputError("%s: expected a server", pkg.PackageName())
}
server.Id = pkg.Server.Id
server.Name = pkg.Server.Name
server.Endpoints = []cueEndpoint{}
for _, endpoint := range stack.EndpointsBy(pkg.PackageName()) {
server.Endpoints = append(server.Endpoints, cueEndpoint{
Type: endpoint.Type.String(),
ServiceName: endpoint.ServiceName,
AllocatedName: endpoint.AllocatedName,
FullyQualifiedName: endpoint.FullyQualifiedName,
ContainerPort: endpoint.GetPort().GetContainerPort(),
})
}
return server, nil
}
}
func FetchServerWorkspace(loc protos.Location) FetcherFunc {
return func(ctx context.Context, v cue.Value) (interface{}, error) {
return cueWorkspace{
ServerPath: loc.Rel(),
}, nil
}
}
type cueProtoload struct {
Sources []string `json:"sources"`
SkipCodegen bool `json:"skip_codegen"`
Types map[string]cueProto `json:"types"`
Services map[string]cueProto `json:"services"`
}
func FetchProto(pl pkggraph.PackageLoader, fsys fs.FS, loc pkggraph.Location) FetcherFunc {
return func(ctx context.Context, v cue.Value) (interface{}, error) {
var load cueProtoload
if err := v.Decode(&load); err != nil {
return nil, err
}
opts, err := parsing.MakeProtoParseOpts(ctx, pl, loc.Module.Workspace)
if err != nil {
return nil, err
}
fds, err := opts.ParseAtLocation(fsys, loc, load.Sources)
if err != nil {
return nil, err
}
load.Types = map[string]cueProto{}
load.Services = map[string]cueProto{}
for _, d := range fds.File {
if err := fillFromFile(fds, d, &load, load.SkipCodegen); err != nil {
return load, err
}
}
return load, nil
}
}
func fillFromFile(fds *protos.FileDescriptorSetAndDeps, d *descriptorpb.FileDescriptorProto, out *cueProtoload, skipCodegen bool) error {
for _, index := range d.PublicDependency {
if int(index) >= len(d.Dependency) {
return fnerrors.InternalError("%s: public_dependency out of bonds", d.GetName())
}
dep := d.Dependency[index]
var filedep *descriptorpb.FileDescriptorProto
for _, d := range fds.File {
if d.GetName() == dep {
filedep = d
break
}
}
if filedep == nil {
for _, d := range fds.Dependency {
if d.GetName() == dep {
filedep = d
break
}
}
}
if filedep == nil {
return fnerrors.InternalError("%s: public_dependency refers to unknown dependency %q", d.GetName(), dep)
}
if err := fillFromFile(fds, filedep, out, skipCodegen); err != nil {
return err
}
}
for _, t := range d.GetMessageType() {
out.Types[t.GetName()] = cueProto{
Typename: fmt.Sprintf("%s.%s", d.GetPackage(), t.GetName()),
Sources: out.Sources,
SkipCodegen: skipCodegen,
}
}
for _, svc := range d.GetService() {
out.Services[svc.GetName()] = cueProto{
Typename: fmt.Sprintf("%s.%s", d.GetPackage(), svc.GetName()),
Sources: out.Sources,
SkipCodegen: skipCodegen,
}
}
return nil
}
type cueResource struct {
Path string `json:"path"`
Contents []byte `json:"contents"`
}
func FetchResource(fsys fs.FS, loc pkggraph.Location) FetcherFunc {
return func(ctx context.Context, v cue.Value) (interface{}, error) {
var load cueResource
if err := v.Decode(&load); err != nil {
return nil, err
}
if load.Path == "" {
return nil, fnerrors.NewWithLocation(loc, "#FromPath needs to have a path specified")
}
rsc, err := LoadResource(fsys, loc, load.Path)
if err != nil {
return nil, err
}
load.Contents = rsc.Contents
return load, nil
}
}
func LoadResource(fsys fs.FS, loc pkggraph.Location, path string) (*schema.FileContents, error) {
if strings.HasPrefix(path, "../") {
return nil, fnerrors.NewWithLocation(loc, "resources must be loaded from within the package")
}
contents, err := fs.ReadFile(fsys, loc.Rel(path))
if err != nil {
return nil, err
}
return &schema.FileContents{
Path: loc.Rel(path),
Contents: contents,
}, nil
}
func FetchPackage(pl pkggraph.PackageLoader) FetcherFunc {
return func(ctx context.Context, v cue.Value) (interface{}, error) {
packageName, err := v.String()
if err != nil {
return nil, fnerrors.New("expected a string when loading a package: %w", err)
}
return ConsumeNoValue, pl.Ensure(ctx, schema.PackageName(packageName))
}
}