Skip to content

Commit

Permalink
fallback option
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Nov 30, 2023
1 parent 92ac6fb commit 6233ecd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
37 changes: 37 additions & 0 deletions cmd/qdiimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
}
return false
})
fallbackParamName := util.GetUniqueName("fallback", func(nameExists string) bool {
for j := 0; j < iface.NumMethods(); j++ {
if iface.Method(j).Name() == nameExists {
return true
}
}
return false
})

// default interface generic types
codeObjectTypes := util.AddTypeParamsList(objNamedType.TypeParams(), false)
Expand Down Expand Up @@ -197,6 +205,7 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
group.Id("lock").Qual("sync", "Mutex")
}
group.Id("execCount").Map(String()).Int()
group.Id(fallbackParamName).Add(util.GetQualCode(obj.Type()).TypesFunc(codeObjectTypes))

// interface method impls
for j := 0; j < iface.NumMethods(); j++ {
Expand Down Expand Up @@ -292,6 +301,24 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
}
}).Block(
Do(func(s *Statement) {
s.If(Id("d").Dot("impl" + mtd.Name()).Op("==").Nil().
Op("&&").
Id("d").Dot(fallbackParamName).Op("!=").Nil()).BlockFunc(func(bgroup *Group) {
icall := Id("d").Dot(fallbackParamName).Dot(mtd.Name()).CallFunc(func(igroup *Group) {
for k := 0; k < sig.Params().Len(); k++ {
sigParam := sig.Params().At(k)
igroup.Id(util.ParamName(k, sigParam))
}
})
if sig.Results().Len() == 0 {
bgroup.Add(icall)
bgroup.Return()
} else {
bgroup.Add(Return(icall))
}
})
s.Line()

call := Id("d").Dot("impl" + mtd.Name()).CallFunc(func(cgroup *Group) {
cgroup.Id("d").Dot("createContext").Call(
Lit(mtd.Name()), Id("d").Dot("impl"+mtd.Name()).Op("==").Nil(),
Expand Down Expand Up @@ -407,6 +434,16 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
)
}

// WithFallback option
// # func WithQDTYPEFallback(fallback SOURCETYPE) QDTYPEOption {}
f.Func().Id("With" + objNameExported + util.InitialToUpper(fallbackParamName)).TypesFunc(codeObjectTypesWithType).Params(
Id("fallback").Add(util.GetQualCode(obj.Type()).TypesFunc(codeObjectTypes)),
).Params(Id(objOption).TypesFunc(codeObjectTypes)).Block(
Return(Func().Params(Id("d").Op("*").Id(objName).TypesFunc(codeObjectTypes)).Block(
Id("d").Dot(fallbackParamName).Op("=").Id("fallback"),
)),
)

// method options
for j := 0; j < iface.NumMethods(); j++ {
mtd := iface.Method(j)
Expand Down
53 changes: 44 additions & 9 deletions sample/complex/myinterface_qdii.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions sample/datatype/sampledata_qdii.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sample/package/reader_qdii.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6233ecd

Please sign in to comment.