jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.83k stars 845 forks source link

Update TimestampCodec to be equal to TextCodec for pointer reference #2156

Closed mesmerx closed 4 weeks ago

mesmerx commented 4 weeks ago

working with https://github.com/manniwood/pgx-protobuf-timestamp i receive
cannot use TimestampCodec{} (value of type TimestampCodec) as pgtype.Codec value in struct literal: TimestampCodec does not implement pgtype.Codec (method DecodeDatabaseSQLValue has pointer receiver)

and i found that is because of timestamp have pointers where textecodec(athat works) dont have, this is to normalize the both

sems that is introduced in https://github.com/jackc/pgx/commit/8649231bb3bc00b4b9c180ce557a54ae41c28ce2 idk if its the better approach, but it need to be fixed for

type TextCodec struct {
    pgtype.TextCodec
}

...

// TimestampCodec embeds pgtype.TimestampCodec, which implements pgtype.Codec interface
type TimestampCodec struct {
    pgtype.TimestampCodec
}

// Register registers the github.com/gofrs/uuid integration with a pgtype.Map.
func Register(tm *pgtype.Map) {
    tm.TryWrapEncodePlanFuncs = append(
        []pgtype.TryWrapEncodePlanFunc{TryWrapTimestampEncodePlan},
        tm.TryWrapEncodePlanFuncs...)
    tm.TryWrapScanPlanFuncs = append(
        []pgtype.TryWrapScanPlanFunc{TryWrapTimestampScanPlan},
        tm.TryWrapScanPlanFuncs...)

    tm.RegisterType(&pgtype.Type{
        Name:  "timestamp",
        OID:   pgtype.TimestampOID,
        Codec: TimestampCodec{},
    })
}

to work

i believe that i can overwrite some functions to make work in my side, but i think we need to set this here

mesmerx commented 4 weeks ago

usage of

 tm.RegisterType(&pgtype.Type{
        Name:  "timestamp",
        OID:   pgtype.TimestampOID,
        Codec: &TimestampCodec{},
    })

seems to fix the issue too, but i will test more here

mesmerx commented 4 weeks ago

closing because

 tm.RegisterType(&pgtype.Type{
        Name:  "timestamp",
        OID:   pgtype.TimestampOID,
        Codec: &TimestampCodec{},
    })

fix without messing pointers