@@ -157,8 +157,9 @@ func newGenerator(opt Options, pkg *packages.Package, fset *token.FileSet) (*gen
157
157
// kod.AutoMarshal struct.
158
158
tset := newTypeSet(pkg)
159
159
160
- // Find and process all components.
161
- components := map[string]*component{}
160
+ // Find and process all seen.
161
+ seen := map[string]*component{}
162
+ components := []*component{}
162
163
for _, file := range pkg.Syntax {
163
164
filename := fset.Position(file.Package).Filename
164
165
if filepath.Base(filename) == generatedCodeFile {
@@ -179,25 +180,28 @@ func newGenerator(opt Options, pkg *packages.Package, fset *token.FileSet) (*gen
179
180
// This code relies on the fact that a component
180
181
// interface and component implementation have to be in the same
181
182
// package. If we lift this requirement, then this code will break.
182
- if existing, ok := components [c.fullIntfName()]; ok {
183
+ if existing, ok := seen [c.fullIntfName()]; ok {
183
184
errs = append(errs, errorf(pkg.Fset, c.impl.Obj().Pos(),
184
185
"Duplicate implementation for component %s, other declaration: %v",
185
186
c.fullIntfName(), fset.Position(existing.impl.Obj().Pos())))
186
187
continue
187
188
}
188
- components[c.fullIntfName()] = c
189
+ seen[c.fullIntfName()] = c
190
+ components = append(components, c)
189
191
}
190
192
}
191
193
if err := errors.Join(errs...); err != nil {
192
194
return nil, err
193
195
}
194
196
197
+ // slices.Reverse(components)
198
+
195
199
return &generator{
196
200
opt: opt,
197
201
pkg: pkg,
198
202
tset: tset,
199
203
fileset: fset,
200
- components: lo.Values( components) ,
204
+ components: components,
201
205
}, nil
202
206
}
203
207
@@ -393,6 +397,10 @@ func (c *component) intfName() string {
393
397
return c.intf.Obj().Name()
394
398
}
395
399
400
+ func (c *component) fullMethodNameVar(methodName string) string {
401
+ return fmt.Sprintf("%s_%s_FullMethodName", c.intfName(), methodName)
402
+ }
403
+
396
404
// implName returns the component implementation name.
397
405
func (c *component) implName() string {
398
406
return c.impl.Obj().Name()
@@ -549,10 +557,10 @@ func (g *generator) generate() error {
549
557
return nil
550
558
}
551
559
552
- // Process components in deterministic order.
553
- sort.Slice(g.components, func(i, j int) bool {
554
- return g.components[i].intfName() < g.components[j].intfName()
555
- })
560
+ // // Process components in deterministic order.
561
+ // sort.Slice(g.components, func(i, j int) bool {
562
+ // return g.components[i].intfName() < g.components[j].intfName()
563
+ // })
556
564
557
565
// Generate the file body.
558
566
var body bytes.Buffer
@@ -574,7 +582,9 @@ func (g *generator) generate() error {
574
582
fn := func(format string, args ...interface{}) {
575
583
fmt.Fprintln(&header, fmt.Sprintf(format, args...))
576
584
}
585
+
577
586
g.generateImports(fn)
587
+ g.generateFullMethodNames(fn)
578
588
}
579
589
580
590
// Create a generated file.
@@ -643,6 +653,19 @@ func (g *generator) generateImports(p printFn) {
643
653
p(`)`)
644
654
}
645
655
656
+ func (g *generator) generateFullMethodNames(p printFn) {
657
+ p(``)
658
+ p(`// Full method names for components.`)
659
+ p(`const (`)
660
+ for _, comp := range g.components {
661
+ for _, m := range comp.methods() {
662
+ p(`// %s is the full name of the method [%s.%s].`, comp.fullMethodNameVar(m.Name()), comp.implName(), m.Name())
663
+ p(`%s = %q`, comp.fullMethodNameVar(m.Name()), comp.fullIntfName())
664
+ }
665
+ }
666
+ p(`)`)
667
+ }
668
+
646
669
// generateInstanceChecks generates code that checks that every component
647
670
// implementation type implements kod.InstanceOf[T] for the appropriate T.
648
671
func (g *generator) generateInstanceChecks(p printFn) {
@@ -798,10 +821,9 @@ func (g *generator) generateLocalStubs(p printFn) {
798
821
p(`info := interceptor.CallInfo {
799
822
Impl: s.impl,
800
823
Component: s.name,
801
- FullMethod: "%s.%s",
802
- Method: "%s",
824
+ FullMethod: %s,
803
825
}
804
- `, comp.fullIntfName(), m.Name(), m.Name( ))
826
+ `, comp.fullMethodNameVar( m.Name()))
805
827
806
828
p(`%s = s.interceptor(ctx, info, []any{%s}, []any{%s}, call)`,
807
829
lo.If(haveError(mt), "err").Else("_"),
0 commit comments