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 95995d7
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 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 @@ -142,8 +142,9 @@ func @Type'InjectContext(ctx @contextContext, tpe @Type) (@contextContext) {
f := x.Field(i)
structTag := reflect.StructTag(x.Tag(i))

injectTag, exists := structTag.Lookup("provide")
if exists && injectTag != "-" {
_, injectExists := structTag.Lookup("inject")
injectTag, provideExists := structTag.Lookup("provide")
if provideExists && injectTag != "-" {
typ := f.Type()
for {
x, ok := typ.(*types.Pointer)
Expand Down Expand Up @@ -175,6 +176,18 @@ ctx = @FieldType'InjectContext(ctx, p.@Field)
})
}
}

if !injectExists && !provideExists {
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 +372,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 +396,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 95995d7

Please sign in to comment.