Value, EncodeBinary, EncodeText, and MarshalJSON on T instead of *T
Methods defined on T are also available on *T. This change makes Value consistent with database/sql Value implementations. It also makes Value, EncodeBinary, and EncodeText more convenient to use because you can pass T or *T as an argument to a query. The MarshalJSON change is even more significant because without it json.Marshal would generate the "%v" format instead of the implemented MarshalJSON. Thought this technically changes the interface, because *T will be automatically dereferenced as needed it shouldn't be a breaking change. See: https://github.com/jackc/pgx/issues/538 for initial discussion.
This commit is contained in:
@@ -117,7 +117,7 @@ func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src *UUID) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
func (src UUID) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Null:
|
||||
return nil, nil
|
||||
@@ -128,7 +128,7 @@ func (src *UUID) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
return append(buf, src.UUID.String()...), nil
|
||||
}
|
||||
|
||||
func (src *UUID) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
func (src UUID) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Null:
|
||||
return nil, nil
|
||||
@@ -157,6 +157,6 @@ func (dst *UUID) Scan(src interface{}) error {
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *UUID) Value() (driver.Value, error) {
|
||||
func (src UUID) Value() (driver.Value, error) {
|
||||
return pgtype.EncodeValueText(src)
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ func (dst *Numeric) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src *Numeric) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
func (src Numeric) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Null:
|
||||
return nil, nil
|
||||
@@ -268,7 +268,7 @@ func (src *Numeric) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error)
|
||||
return append(buf, src.Decimal.String()...), nil
|
||||
}
|
||||
|
||||
func (src *Numeric) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
func (src Numeric) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Null:
|
||||
return nil, nil
|
||||
@@ -306,7 +306,7 @@ func (dst *Numeric) Scan(src interface{}) error {
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Numeric) Value() (driver.Value, error) {
|
||||
func (src Numeric) Value() (driver.Value, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Present:
|
||||
return src.Decimal.Value()
|
||||
|
||||
Reference in New Issue
Block a user