-
Notifications
You must be signed in to change notification settings - Fork 9
/
dependency.go
299 lines (244 loc) · 8.05 KB
/
dependency.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// 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 golang
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"path/filepath"
"github.com/kr/text"
"github.com/protocolbuffers/txtpbfmt/parser"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
"namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/gosupport"
"namespacelabs.dev/foundation/internal/integrations/shared"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type nodeLoc struct {
Location pkggraph.Location
Node *schema.Node
}
type initializer struct {
nodeLoc
initializeBefore []string
initializeAfter []string
hasPostInitializer bool
}
type instancedDepList struct {
services []nodeLoc
instances []*instancedDep
initializers []initializer
}
type instancedDep struct {
Location pkggraph.Location
Parent *schema.Node
Scope *schema.Provides // Parent provider - nil for singleton dependencies.
Instance *schema.Instantiate
Provisioned *typeProvider
}
type typeProvider struct {
Provides *schema.Provides
PackageName schema.PackageName
GoPackage string // GoPackage where the ProvisionYYY() method is defined.
DepVars []gosupport.TypeDef
Method string
Args []string
SerializedMsg string
ProtoComments string
Dependencies []*typeProvider
}
func expandInstancedDeps(ctx context.Context, loader pkggraph.PackageLoader, includes []schema.PackageName) (instancedDepList, error) {
var e instancedDepList
for _, ref := range includes {
pkg, err := loader.LoadByName(ctx, ref)
if err != nil {
return e, err
}
referenced := pkg.Node()
if referenced == nil {
continue
}
if err := expandNode(ctx, loader, pkg.Location, referenced, true, &e); err != nil {
return e, err
}
if referenced.GetKind() == schema.Node_SERVICE {
e.services = append(e.services, nodeLoc{Location: pkg.Location, Node: referenced})
}
if x := referenced.InitializerFor(schema.Framework_GO); x != nil {
e.initializers = append(e.initializers, initializer{
nodeLoc: nodeLoc{Location: pkg.Location, Node: referenced},
initializeBefore: x.InitializeBefore,
initializeAfter: x.InitializeAfter,
hasPostInitializer: x.HasPostInitializer,
})
}
}
return e, nil
}
func expandNode(ctx context.Context, loader pkggraph.PackageLoader, loc pkggraph.Location, n *schema.Node, produceSerialized bool, e *instancedDepList) error {
if !isGoNode(n) {
return nil
}
for k, dep := range n.GetInstantiate() {
var prov typeProvider
if err := makeDep(ctx, loader, dep, produceSerialized, &prov); err != nil {
return fnerrors.NewWithLocation(loc, "%s.dependency[%d]: %w", n.GetPackageName(), k, err)
}
if len(prov.DepVars) > 0 {
e.instances = append(e.instances, &instancedDep{
Location: loc,
Parent: n,
Instance: dep,
Provisioned: &prov,
})
}
}
for _, p := range n.Provides {
for k, dep := range p.GetInstantiate() {
var prov typeProvider
if err := makeDep(ctx, loader, dep, produceSerialized, &prov); err != nil {
return fnerrors.NewWithLocation(loc, "%s.%s.dependency[%d]: %w", n.GetPackageName(), p.Name, k, err)
}
if len(prov.DepVars) > 0 {
e.instances = append(e.instances, &instancedDep{
Location: loc,
Parent: n,
Instance: dep,
Provisioned: &prov,
Scope: p,
})
}
}
}
return nil
}
func isGoNode(n *schema.Node) bool {
if n.ServiceFramework == schema.Framework_GO {
return true
}
if n.InitializerFor(schema.Framework_GO) != nil {
return true
}
for _, pr := range n.Provides {
for _, available := range pr.AvailableIn {
if available.Go != nil {
return true
}
}
}
return false
}
func makeDep(ctx context.Context, loader pkggraph.PackageLoader, dep *schema.Instantiate, produceSerialized bool, prov *typeProvider) error {
pkg, err := loader.LoadByName(ctx, schema.PackageName(dep.PackageName))
if err != nil {
return fnerrors.New("failed to load %s/%s: %w", dep.PackageName, dep.Type, err)
}
// XXX Well, yes, this shouldn't live here. But being practical. We need to either have
// a way to define how to generate the types. Or we need to use generics (although generics
// don't replace all of the uses).
if shared.IsStdGrpcExtension(dep.PackageName, dep.Type) {
grpcServiceType, err := shared.PrepareGrpcBackendDep(ctx, loader, dep)
if err != nil {
return err
}
// XXX not hermetic.
path := filepath.Dir(grpcServiceType.Location.Abs(grpcServiceType.SourceFileName))
gopkg, err := gosupport.ComputeGoPackage(path)
if err != nil {
return err
}
clientType := fmt.Sprintf("%sClient", grpcServiceType.Name)
prov.GoPackage = gopkg
prov.Method = "New" + clientType
prov.Args = append(prov.Args, gosupport.MakeGoPubVar(dep.Name+"Conn"))
prov.DepVars = append(prov.DepVars, gosupport.TypeDef{
GoName: gosupport.MakeGoPubVar(dep.Name),
GoImportURL: gopkg,
GoTypeName: clientType,
})
return nil
}
_, p := parsing.FindProvider(pkg, schema.PackageName(dep.PackageName), dep.Type)
if p == nil {
return fnerrors.New("didn't find a provider for %s/%s", dep.PackageName, dep.Type)
}
var goprovider *schema.Provides_AvailableIn_Go
for _, prov := range p.AvailableIn {
if prov.Go != nil {
goprovider = prov.Go
break
}
}
if goprovider == nil {
return fnerrors.New("%s: not available for Go", dep.Name)
}
prov.Provides = p
prov.PackageName = schema.PackageName(dep.PackageName)
prov.GoPackage, _ = packageFrom(pkg.Location)
prov.Method = makeProvidesMethod(p)
goImport := goprovider.Package
if goImport == "" {
goImport = prov.GoPackage
}
prov.DepVars = append(prov.DepVars, gosupport.TypeDef{
GoName: gosupport.MakeGoPubVar(dep.Name),
GoImportURL: goImport,
GoTypeName: goprovider.Type,
})
if produceSerialized {
return serializeContents(ctx, loader, p, dep, prov)
}
return nil
}
func makeProvidesMethod(p *schema.Provides) string {
return "Provide" + gosupport.MakeGoPubVar(p.Name)
}
func makeProvidesDepsType(p *schema.Provides) string {
return gosupport.MakeGoPubVar(p.Name) + "Deps"
}
func serializeContents(ctx context.Context, loader pkggraph.PackageLoader, provides *schema.Provides, instance *schema.Instantiate, prov *typeProvider) error {
pkg, err := loader.LoadByName(ctx, schema.PackageName(instance.PackageName))
if err != nil {
return err
}
parsed, ok := pkg.Provides[provides.Name]
if !ok {
return fnerrors.InternalError("%s: protos were not loaded as expected?", instance.PackageName)
}
files, msgdesc, err := protos.LoadMessageByName(parsed, provides.Type.Typename)
if err != nil {
return fnerrors.InternalError("%s: failed to load message %q: %w", instance.PackageName, provides.Type.Typename, err)
}
raw := dynamicpb.NewMessage(msgdesc)
if err := proto.Unmarshal(instance.Constructor.Value, raw.Interface()); err != nil {
return fnerrors.InternalError("failed to unmarshal constructor: %w", err)
}
// Clean up all values which are not meant to be shipped into the binary.
protos.CleanupForNonProvisioning(raw)
deterministicBytes, err := proto.MarshalOptions{Deterministic: true}.Marshal(raw.Interface())
if err != nil {
return fnerrors.InternalError("failed to marshal depvar: %w", err)
}
prov.SerializedMsg = base64.StdEncoding.EncodeToString(deterministicBytes)
resolver, err := protos.AsResolver(files)
if err != nil {
return fnerrors.InternalError("failed to create resolver: %w", err)
}
serialized, err := prototext.MarshalOptions{Multiline: true, Resolver: resolver}.Marshal(raw.Interface())
if err == nil {
stableFmt, err := parser.Format(serialized)
if err == nil {
var b bytes.Buffer
_, _ = text.NewIndentWriter(&b, []byte("// ")).Write(stableFmt)
prov.ProtoComments = b.String()
}
}
return nil
}