Skip to content

Commit

Permalink
fix(injectablegen): should handle InjectContext
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Dec 30, 2024
1 parent 903f29a commit b1c5766
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions devpkg/injectablegen/injectable.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func init() {
}

type injectableGen struct {
publicProviderInterface *types.Interface
publicInitInterface *types.Interface
once sync.Once
publicInjectContextInterface *types.Interface
publicInitInterface *types.Interface
once sync.Once
}

func (*injectableGen) Name() string {
Expand All @@ -27,7 +27,7 @@ func (g *injectableGen) init(c gengo.Context) {
{
sig := c.Package("context").Function("Cause").Signature()

g.publicProviderInterface = types.NewInterfaceType([]*types.Func{
g.publicInjectContextInterface = types.NewInterfaceType([]*types.Func{
types.NewFunc(0, c.Package("context").Pkg(), "InjectContext",
types.NewSignatureType(nil, nil, nil,
types.NewTuple(sig.Params().At(0)),
Expand Down Expand Up @@ -175,6 +175,18 @@ ctx = @FieldType'InjectContext(ctx, p.@Field)
})
}
}

if !exists {
if g.hasPublicInjectContext(c, f.Type()) {
sw.Render(gengo.Snippet{
gengo.T: `
ctx = [email protected](ctx)
`,
"Field": gengo.ID(f.Name()),
})
continue
}
}
}
}

Expand Down Expand Up @@ -359,16 +371,20 @@ if err := v.afterInit(ctx); err != nil {
return nil
}

func (g *injectableGen) isInjectable(c gengo.Context, t types.Type) bool {
func (g *injectableGen) hasPublicInjectContext(c gengo.Context, t types.Type) bool {
switch x := t.(type) {
case *types.Pointer:
return g.isInjectable(c, x.Elem())
return g.hasPublicInjectContext(c, x.Elem())
case *types.Named:
_, isStruct := x.Underlying().(*types.Struct)
if !isStruct {
return false
}
tags, _ := c.Doc(x.Obj())
if _, ok := tags["gengo:injectable"]; ok {
return true
if _, ok := tags["gengo:injectable:provider"]; ok {
return ok
}
return types.Implements(x, g.publicProviderInterface) || types.Implements(types.NewPointer(x), g.publicProviderInterface)
return types.Implements(x, g.publicInjectContextInterface) || types.Implements(types.NewPointer(x), g.publicInjectContextInterface)
}

return false
Expand All @@ -379,10 +395,15 @@ func (g *injectableGen) hasPublicInit(c gengo.Context, t types.Type) bool {
case *types.Pointer:
return g.hasPublicInit(c, x.Elem())
case *types.Named:
_, isStruct := x.Underlying().(*types.Struct)
if !isStruct {
return false
}
tags, _ := c.Doc(x.Obj())
if _, ok := tags["gengo:injectable:provider"]; ok {
_, ok := x.Obj().Type().(*types.Struct)
return ok
_, injectable := tags["gengo:injectable"]
_, injectableProvider := tags["gengo:injectable:provider"]
if injectable || injectableProvider {
return true
}
return types.Implements(x, g.publicInitInterface) || types.Implements(types.NewPointer(x), g.publicInitInterface)
}
Expand Down

0 comments on commit b1c5766

Please sign in to comment.