I think the issue is that the *consumer* of this object checks whether 
certain methods (or rather, interfaces) are present, and behaves 
differently depending whether they are or not.

There are a whole load of public interfaces defined here:
https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/database/sql/driver/driver.go
And the supplied object, which the OP is trying to wrap, could implement 
some arbitrary combination of these interfaces.

I think that for *some* of these interfaces, it would be OK to provide 
dummy implementations if there is no underlying method to call: e.g.
https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/database/sql/sql.go;l=552-554
https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/database/sql/sql.go;l=567-569
https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/database/sql/sql.go;l=834-838

But for others it's much less clear, and it may make a semantic difference 
whether the the object you provide implements these interfaces or not, e.g.
https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/database/sql/sql.go;l=1865-1867

For these, I can't think of a better implementation that the OP's. It 
doesn't seem like the greatest API design though.

On Sunday, 11 June 2023 at 08:10:01 UTC+1 Tamás Gulácsi wrote:

> As Far as I See, you check all the combinations of methods in wideness 
> order.
> Why not have a generic wrapper struct, that is filled with the underlying 
> driver.Conn's methods, 
> and use that if not nil, but use the generic implementation if not.
>
> Like
> ```
> type wrappedConn struct {
>   driver.Conn
>   queryContext func(...)
> }
> func (wc wrappedConn) QueryContext(...) ... {
>   if wc.queryContext != nil { return wc.queryContext(...) }
>   return wc.Conn.Query(...)
> }
> ```
>
> This way you only have to check for each method on driver.Conn, and fill 
> the wrappedConn's functions as they axist/not.
>
> Vasiliy Tolstov a következőt írta (2023. június 10., szombat, 12:16:34 
> UTC+2):
>
>> I have sql driver that wraps original driver.Driver, to be able to work 
>> with drivers that not have ExecerContext or QuerierContext i need to return 
>> wrapped to that supports only needed interfaces.
>> I'm to want to write all cases via handmade switch case and write 
>> generator that creates full combo list of methods and generate interfaces 
>> for this methods.
>> But this brings file that contains 20K lines 
>> https://git.unistack.org/unistack-org/micro-wrapper-sql/src/branch/master/wrap_gen.go
>> Does it possible to have smaller code that provides the same effect?
>> Or I'm miss something?
>>
>> -- 
>> Vasiliy Tolstov,
>> e-mail: v.to...@selfip.ru
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4dc775df-6627-4134-8b32-e9f659e48831n%40googlegroups.com.

Reply via email to