Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback option #2

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.