pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Custom type

Open kaber2 opened this issue 1 year ago • 2 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!

kaber2 avatar Feb 18 '24 04:02 kaber2

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.

jackc avatar Feb 24 '24 00:02 jackc

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.

kaber2 avatar Feb 25 '24 16:02 kaber2