pgtype icon indicating copy to clipboard operation
pgtype copied to clipboard

EnumArray does not implement ValueTranscoder

Open drewthor opened this issue 3 years ago • 3 comments

Not sure if this is on purpose or was just an oversight (if it is purpose please feel free to close this), but EnumArray does not implement ValueTranscoder so users must implement their own custom type with func's EncodeText and DecodeText utilizing EnumArray::DecodeText and EnumArray::EncodeText under the hood. This makes it much more difficult to do binary encoding/decoding (user's must fully implement everything for their custom types) or register data types in pgx simply using a line like: conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: pgtype.NewArrayType("pg_enum_typname", 1234, func() pgtype.ValueTranscoder { return &pgtype.EnumArray{} }), Name: "pg_enum_typname", OID: oid})

drewthor avatar May 31 '21 17:05 drewthor

I don't remember why EnumArray doesn't implement the binary format because the binary format for an array requires knowing the OID of the element.

I think if you want to register an enum array type you would use NewArrayType, but with GenericText as the element. You don't need an array of arrays.

jackc avatar Jun 12 '21 18:06 jackc

Ahh, that makes sense. So the ValueTranscoder is meant to operate on a singular element of the array vs the entire array. Unfortunately it looks like GenericText can't be used directly as a ValueTranscoder as it also doesn't implement the binary format.

drewthor avatar Jun 12 '21 20:06 drewthor

Unfortunately it looks like GenericText can't be used directly as a ValueTranscoder as it also doesn't implement the binary format.

Ah, true. Hmmm, the regular Text type does implement the binary format. Could try that.

jackc avatar Jun 19 '21 14:06 jackc