/
loader.go
402 lines (358 loc) · 10.8 KB
/
loader.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package source
import (
"context"
"fmt"
"go/ast"
"path"
"strconv"
"sync"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"github.com/eugenenosenko/gopoly/code"
"github.com/eugenenosenko/gopoly/config"
"github.com/eugenenosenko/gopoly/internal/xslices"
"github.com/eugenenosenko/gopoly/internal/xtypes"
)
type Loader interface {
Load(c context.Context, defs []*config.TypeDefinition) (code.SourceList, error)
}
type loader struct {
loadFunc func(patterns ...string) ([]*Package, error)
logf func(format string, args ...any)
}
type Config struct {
LoadFunc func(patterns ...string) ([]*Package, error)
Logf func(format string, args ...any)
}
type Definition struct {
Dec ast.Decl
PkgName string
PkgPath string
}
func NewLoader(c *Config) (*loader, error) {
if c == nil {
return nil, errors.New("source.NewLoader: config is nil")
}
return &loader{loadFunc: c.LoadFunc, logf: c.Logf}, nil
}
type PkgPath = string
func (l *loader) Load(ctx context.Context, c []*config.TypeDefinition) (code.SourceList, error) {
packages := xslices.ToSetFunc[[]*config.TypeDefinition](c,
func(t *config.TypeDefinition) string { return t.Package },
)
packageNames := maps.Keys(packages)
files, err := l.loadFunc(packageNames...)
if err != nil {
return nil, errors.Wrapf(err, "collecting ast.Files from %v", packageNames)
}
pdecs := make(map[PkgPath][]*Definition, 0)
for d := range declarations(ctx, files) {
pdecs[d.PkgPath] = append(pdecs[d.PkgPath], d)
}
ifaces, err := interfaces(pdecs, c)
if err != nil {
return nil, errors.Wrapf(err, "collecting interfaces from %v", packageNames)
}
psources := make(map[PkgPath]*code.Source, 0)
for pkg, i := range ifaces {
psources[pkg] = &code.Source{Package: code.Package(pkg), Interfaces: i}
}
for pkg, decs := range pdecs {
source := psources[pkg]
for _, dec := range decs {
d, ok := dec.Dec.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range d.Specs {
i, ok := spec.(*ast.ImportSpec)
if !ok {
continue
}
// imports are quoted, i.e. "time", "bytes"
importpath, err := strconv.Unquote(i.Path.Value)
if err != nil {
return nil, err
}
// check if imported types are in the supplied config
if _, present := packages[importpath]; present {
var aliased bool
sname := path.Base(importpath)
if alias := i.Name; alias != nil {
sname = alias.Name
aliased = true
}
source.Imports = append(source.Imports, &code.Import{
Source: psources[importpath],
ShortName: sname,
Path: importpath,
Aliased: aliased,
})
}
}
}
}
for pkg, decs := range pdecs {
types := xslices.Flatten(xslices.Map[[]*code.Interface, [][]*code.Variant](
psources[pkg].Interfaces,
func(i *code.Interface) []*code.Variant { return i.Variants },
))
variants := code.VariantList(types).AssociateByVariantName()
pifaces := ifaces[pkg].AssociateByName()
imports := psources[pkg].Imports.AssociateByShortName()
for _, dec := range decs {
d, ok := dec.Dec.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range d.Specs {
// look for type declarations
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
// filter out only structs
strct, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
variant, ok := variants[ts.Name.Name]
if !ok {
continue
}
pfields := make([]*code.PolyField, 0)
for _, field := range strct.Fields.List {
var iface *code.Interface
var kind int
var prefix string
if st, ok := field.Type.(*ast.Ident); ok { // scalar type field
iface, kind = pifaces[st.Name], code.KindScalar
} else if imp, ok := field.Type.(*ast.SelectorExpr); ok { // imported scalar type field
iface, kind = matchImportedPFIface(imp, imports), code.KindScalar
prefix = importPrefix(imp.X)
}
if arr, ok := field.Type.(*ast.ArrayType); ok {
// imported slice type
if imp, ok := arr.Elt.(*ast.SelectorExpr); ok {
iface, kind = matchImportedPFIface(imp, imports), code.KindSlice
prefix = importPrefix(imp.X)
} else if st, ok := arr.Elt.(*ast.Ident); ok {
iface, kind = pifaces[st.Name], code.KindSlice
}
}
if mp, ok := field.Type.(*ast.MapType); ok {
// imported map field-type
if imp, ok := mp.Value.(*ast.SelectorExpr); ok {
iface, kind = matchImportedPFIface(imp, imports), code.KindMap
prefix = importPrefix(imp.X)
} else if st, ok := mp.Value.(*ast.Ident); ok {
iface, kind = pifaces[st.Name], code.KindMap
}
}
if iface != nil {
pfields = append(pfields, &code.PolyField{
Name: field.Names[0].Name,
Tags: field.Tag.Value,
Interface: iface,
Kind: kind,
Prefix: prefix,
})
}
}
variant.Fields = pfields
}
}
}
return maps.Values(psources), nil
}
func importPrefix(e ast.Expr) string {
if ident, ok := e.(*ast.Ident); ok {
return ident.Name // short name or alias
}
return ""
}
func matchImportedPFIface(se *ast.SelectorExpr, imports map[string]*code.Import) *code.Interface {
name, sname := se.Sel.Name, importPrefix(se.X)
i, ok := imports[sname]
if !ok {
return nil
}
nifaces := i.Source.Interfaces.AssociateByName()
iface, ok := nifaces[name]
if !ok {
return nil
}
return iface
}
// interfaces iterates decs and tries to match ifaces that were passed with the config
func interfaces(defs map[PkgPath][]*Definition, tts []*config.TypeDefinition) (map[PkgPath]code.InterfaceList, error) {
if err := validateDefinitions(defs, tts); err != nil {
return nil, errors.Wrap(err, "validating type definitions")
}
// collect interfaces from definitions
pinterfaces := make(map[PkgPath]code.InterfaceList, 0)
for _, t := range tts {
expected := xslices.ToSet[[]string](maps.Values(t.Discriminator.Mapping))
i := &code.Interface{
Name: t.Name,
MarkerMethod: t.MarkerMethod,
Variants: []*code.Variant{},
Pkg: t.Package,
}
for _, dec := range defs[t.Package] {
// check only functions and methods
fun, ok := dec.Dec.(*ast.FuncDecl)
if !ok {
continue
}
// check whether function name matches marker-method
if t.MarkerMethod != fun.Name.Name {
continue
}
// get the receiver field
receiver, ok := xslices.First(fun.Recv.List)
if !ok {
continue
}
// fetch the name of the receiver type
var name string
if ident, ok := receiver.Type.(*ast.Ident); ok {
name = ident.Name
} else if star, ok := receiver.Type.(*ast.StarExpr); ok {
if !ok {
continue
}
if i, ok := star.X.(*ast.Ident); ok {
name = i.Name
}
}
// if discriminator check whether the receiver type is defined as a variant of the i-face
if t.DecodingStrategy.IsDiscriminator() {
if _, ok := expected[name]; ok {
i.Variants = append(i.Variants, &code.Variant{Name: name, Interface: i})
}
} else {
i.Variants = append(i.Variants, &code.Variant{Name: name, Interface: i})
}
}
if t.DecodingStrategy.IsStrict() {
pinterfaces[t.Package] = append(pinterfaces[t.Package], i) // just add all variants found
} else {
err := validateNoMissingVariants(i.Variants, expected) // validate if all were found
if err != nil {
return nil, errors.Wrapf(err, "mathing variants for '%s.%s'", t.Package, t.Name)
}
pinterfaces[t.Package] = append(pinterfaces[t.Package], i)
}
}
if len(pinterfaces) == 0 {
return nil, fmt.Errorf("failed to collect any interfaces")
}
return pinterfaces, nil
}
func validateNoMissingVariants(vars code.VariantList, expected map[string]struct{}) error {
variants := xslices.Map[[]*code.Variant, []string](vars, func(t *code.Variant) string { return t.Name })
// check if all variants are accounted for
if diff := xslices.Difference(variants, maps.Keys(expected)); len(diff) > 0 {
return fmt.Errorf("failed to match following %v variants", diff)
}
return nil
}
func validateDefinitions(pdefs map[PkgPath][]*Definition, types []*config.TypeDefinition) error {
provided := xslices.ToMap[[]*config.TypeDefinition, map[string]*config.TypeDefinition](
types, func(t *config.TypeDefinition) string {
return (&xtypes.Tuple[string, string]{First: t.Name, Second: t.Package}).String()
}, nil,
)
ifaces := make(map[string]*xtypes.Tuple[*config.TypeDefinition, *ast.InterfaceType])
for pkg, defs := range pdefs {
for _, def := range defs {
d, ok := def.Dec.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range d.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
// we are only interested in interfaces at this point
i, ok := ts.Type.(*ast.InterfaceType)
if !ok {
continue
}
// composite map key Name.Name + PkgPath
key := (&xtypes.Tuple[string, string]{First: ts.Name.Name, Second: pkg}).String()
// check if this interface is among those provided by the configuration
if t, present := provided[key]; present {
ifaces[key] = &xtypes.Tuple[*config.TypeDefinition, *ast.InterfaceType]{First: t, Second: i}
}
}
}
}
// check if all interfaces are accounted for
if diff := xslices.Difference(maps.Keys(ifaces), maps.Keys(provided)); len(diff) > 0 {
return fmt.Errorf("failed to locate following interfaces: %v", diff)
}
// validate signatures on the marker methods
for _, v := range ifaces {
found := false
for _, field := range v.Second.Methods.List {
if f, ok := field.Type.(*ast.FuncType); ok {
if len(f.Params.List) != 0 || f.Results != nil {
continue
}
}
name, ok := xslices.First(field.Names)
if !ok {
continue
}
if name.Name == v.First.MarkerMethod {
found = true
break
}
}
if !found {
return fmt.Errorf("interface '%s.%s' missing zero arg/returns marker-method '%s'",
v.First.Package,
v.First.Name,
v.First.MarkerMethod,
)
}
}
return nil
}
// declarations goes through the supplied packages and collects all ast-declarations
// for each package a new goroutine is started which starts a new goroutine for each file
// in the package, returns a channel of Definition.
func declarations(ctx context.Context, packs []*Package) <-chan *Definition {
c := make(chan *Definition)
go func() {
defer close(c)
var wgOuter sync.WaitGroup
wgOuter.Add(len(packs))
for _, pack := range packs {
go func(p *Package) {
defer wgOuter.Done()
var wgInner sync.WaitGroup
wgInner.Add(len(p.Files))
for _, file := range p.Files {
go func(f *ast.File, p *Package) {
defer wgInner.Done()
for _, d := range f.Decls {
select {
case <-ctx.Done():
return
case c <- &Definition{Dec: d, PkgName: p.Name, PkgPath: p.Path}:
}
}
}(file, p)
}
wgInner.Wait()
}(pack)
}
wgOuter.Wait()
}()
return c
}
var _ Loader = (*loader)(nil)