jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom type

kaber2 opened this issue · comments

Hello,

I've adapted the shopspring decimal.Decimal type for uint256.Int (https://github.com/holiman/uint256).

Even though uint256.Int types can be used as non-pointer types, they are usually used as pointer types, so in TryWrapNumericScanPlan() I'm handling both *uint256.Int and **uint256.Int. I'm wondering however where the proper place to allocate a fresh uint256.Int for the **uint256.Int case would be.

I'm currently doing it in the scan plan Scan() method:

func (plan *wrapUint256ScanPlan) Scan(src []byte, dst interface{}) error {
        switch dst := dst.(type) {
        case *uint256.Int:
                return plan.next.Scan(src, (*Uint256)(dst))
        case **uint256.Int:
                *dst = new(uint256.Int)
                return plan.next.Scan(src, (*Uint256)(*dst))
        default:
                panic("")                                                                                
        }
}

This works fine, but I'm wondering if this is really the proper spot.

Thanks for any advice!

There's nothing exactly wrong with handling **T in a Scan function, but it should be unnecessary. pgx typically can handle pointer to pointer automatically. See pgtype.TryPointerPointerScanPlan. I would expect it to work without the **T case.

Thanks for the hint. It did not work automatically and caused segfaults, but I'll look into it again to find out what went wrong.