Skip to content

Commit

Permalink
method not implemented callback
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Nov 5, 2024
1 parent 37b435a commit 4d16142
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 34 deletions.
35 changes: 35 additions & 0 deletions cmd/qdiimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
}
return false
})
onMethodNotImplementedParamName := util.GetUniqueName("onMethodNotImplemented", 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 @@ -220,6 +228,13 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
}
group.Id("execCount").Map(String()).Int()
group.Id(fallbackParamName).Add(util.GetQualCode(obj.Type()).TypesFunc(codeObjectTypes))
group.Id(onMethodNotImplementedParamName).Add(Func().
Params(
Id("qdCtx").Op("*").Id(objContext),
).
Params(
Error(),
))

// interface method impls
for j := 0; j < iface.NumMethods(); j++ {
Expand Down Expand Up @@ -460,6 +475,10 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
Error(),
).
BlockFunc(func(bgroup *Group) {
bgroup.If(Id("d").Dot(onMethodNotImplementedParamName).Op("!=").Nil()).
Block(
Return(Id("d").Dot(onMethodNotImplementedParamName).Call(Id("qdCtx"))),
)
bgroup.Return(Qual("fmt", "Errorf").
Call(Lit(fmt.Sprintf("[%s] method '%%s' not implemented", objName)), Id("qdCtx").Dot("MethodName")))
})
Expand All @@ -478,6 +497,7 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
Id("d").Dot(dataParamName).Op("=").Id("data"),
)),
)
f.Line()
}

// WithFallback option
Expand All @@ -489,6 +509,21 @@ func gen(outputName string, obj types.Object, iface *types.Interface) error {
Id("d").Dot(fallbackParamName).Op("=").Id("fallback"),
)),
)
f.Line()

// WithOnMethodNotImplemented option
// # func WithOnMethodNotImplemented(m func (qdCtx *TYPEContext) error) TYPEOption {}
f.Func().Id("With" + objOptionPrefix + util.InitialToUpper(onMethodNotImplementedParamName)).TypesFunc(codeObjectTypesWithType).
Params(
Id("m").Func().Params(Id("qdCtx").Op("*").Id(objContext)).Params(Error()),
).
Params(Id(objOption).TypesFunc(codeObjectTypes)).
Block(
Return(Func().Params(Id("d").Op("*").Id(objName).TypesFunc(codeObjectTypes)).Block(
Id("d").Dot(onMethodNotImplementedParamName).Op("=").Id("m"),
)),
)
f.Line()

// method options
for j := 0; j < iface.NumMethods(); j++ {
Expand Down
12 changes: 12 additions & 0 deletions sample/complex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func main() {
*d++
return fmt.Sprintf("a%v", *d), nil
}),
WithOnMethodNotImplemented[string, IIImpl](func(qdCtx *QDMyInterfaceContext) error {
return fmt.Errorf("the method '%s' was not implemented", qdCtx.MethodName)
}),
)

v, err := x.Get(context.Background(), "a")
Expand All @@ -36,4 +39,13 @@ func main() {
panic(err)
}
fmt.Println(v)

func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Recovered from panic. Error:\n%v", r)
}
}()
_ = x.Set(context.Background(), "c", "d")
}()
}
71 changes: 47 additions & 24 deletions sample/complex/myinterface_qdii.go

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

19 changes: 15 additions & 4 deletions sample/datatype/sampledata_qdii.go

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

33 changes: 27 additions & 6 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 4d16142

Please sign in to comment.