Skip to content

Commit

Permalink
Value, EncodeBinary, EncodeText, and MarshalJSON on T instead of *T
Browse files Browse the repository at this point in the history
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: jackc/pgx#538 for initial discussion.
  • Loading branch information
jackc committed Aug 28, 2019
1 parent b1e25e4 commit a8802b1
Show file tree
Hide file tree
Showing 66 changed files with 222 additions and 222 deletions.
4 changes: 2 additions & 2 deletions aclitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (dst *ACLItem) DecodeText(ci *ConnInfo, src []byte) error {
return nil
}

func (src *ACLItem) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src ACLItem) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -115,7 +115,7 @@ func (dst *ACLItem) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *ACLItem) Value() (driver.Value, error) {
func (src ACLItem) Value() (driver.Value, error) {
switch src.Status {
case Present:
return src.String, nil
Expand Down
4 changes: 2 additions & 2 deletions aclitem_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (dst *ACLItemArray) DecodeText(ci *ConnInfo, src []byte) error {
return nil
}

func (src *ACLItemArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src ACLItemArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -200,7 +200,7 @@ func (dst *ACLItemArray) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *ACLItemArray) Value() (driver.Value, error) {
func (src ACLItemArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (dst *ArrayHeader) DecodeBinary(ci *ConnInfo, src []byte) (int, error) {
return rp, nil
}

func (src *ArrayHeader) EncodeBinary(ci *ConnInfo, buf []byte) []byte {
func (src ArrayHeader) EncodeBinary(ci *ConnInfo, buf []byte) []byte {
buf = pgio.AppendInt32(buf, int32(len(src.Dimensions)))

var containsNull int32
Expand Down
8 changes: 4 additions & 4 deletions bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (dst *Bit) DecodeBinary(ci *ConnInfo, src []byte) error {
return (*Varbit)(dst).DecodeBinary(ci, src)
}

func (src *Bit) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*Varbit)(src).EncodeBinary(ci, buf)
func (src Bit) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Varbit)(src).EncodeBinary(ci, buf)
}

// Scan implements the database/sql Scanner interface.
Expand All @@ -32,6 +32,6 @@ func (dst *Bit) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *Bit) Value() (driver.Value, error) {
return (*Varbit)(src).Value()
func (src Bit) Value() (driver.Value, error) {
return (Varbit)(src).Value()
}
6 changes: 3 additions & 3 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (dst *Bool) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *Bool) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Bool) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand All @@ -113,7 +113,7 @@ func (src *Bool) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *Bool) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Bool) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -153,7 +153,7 @@ func (dst *Bool) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *Bool) Value() (driver.Value, error) {
func (src Bool) Value() (driver.Value, error) {
switch src.Status {
case Present:
return src.Bool, nil
Expand Down
6 changes: 3 additions & 3 deletions bool_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (dst *BoolArray) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *BoolArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src BoolArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -225,7 +225,7 @@ func (src *BoolArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *BoolArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src BoolArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -288,7 +288,7 @@ func (dst *BoolArray) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *BoolArray) Value() (driver.Value, error) {
func (src BoolArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (dst *Box) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *Box) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Box) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand All @@ -125,7 +125,7 @@ func (src *Box) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *Box) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Box) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -161,6 +161,6 @@ func (dst *Box) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *Box) Value() (driver.Value, error) {
func (src Box) Value() (driver.Value, error) {
return EncodeValueText(src)
}
16 changes: 8 additions & 8 deletions bpchar.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func (dst *BPChar) DecodeBinary(ci *ConnInfo, src []byte) error {
return (*Text)(dst).DecodeBinary(ci, src)
}

func (src *BPChar) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*Text)(src).EncodeText(ci, buf)
func (src BPChar) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Text)(src).EncodeText(ci, buf)
}

func (src *BPChar) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*Text)(src).EncodeBinary(ci, buf)
func (src BPChar) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Text)(src).EncodeBinary(ci, buf)
}

// Scan implements the database/sql Scanner interface.
Expand All @@ -55,12 +55,12 @@ func (dst *BPChar) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *BPChar) Value() (driver.Value, error) {
return (*Text)(src).Value()
func (src BPChar) Value() (driver.Value, error) {
return (Text)(src).Value()
}

func (src *BPChar) MarshalJSON() ([]byte, error) {
return (*Text)(src).MarshalJSON()
func (src BPChar) MarshalJSON() ([]byte, error) {
return (Text)(src).MarshalJSON()
}

func (dst *BPChar) UnmarshalJSON(b []byte) error {
Expand Down
6 changes: 3 additions & 3 deletions bpchar_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (dst *BPCharArray) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *BPCharArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src BPCharArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -225,7 +225,7 @@ func (src *BPCharArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *BPCharArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src BPCharArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -288,7 +288,7 @@ func (dst *BPCharArray) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *BPCharArray) Value() (driver.Value, error) {
func (src BPCharArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions bytea.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (dst *Bytea) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *Bytea) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Bytea) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand All @@ -113,7 +113,7 @@ func (src *Bytea) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *Bytea) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Bytea) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -145,7 +145,7 @@ func (dst *Bytea) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *Bytea) Value() (driver.Value, error) {
func (src Bytea) Value() (driver.Value, error) {
switch src.Status {
case Present:
return src.Bytes, nil
Expand Down
6 changes: 3 additions & 3 deletions bytea_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (dst *ByteaArray) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *ByteaArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src ByteaArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -225,7 +225,7 @@ func (src *ByteaArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *ByteaArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src ByteaArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -288,7 +288,7 @@ func (dst *ByteaArray) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *ByteaArray) Value() (driver.Value, error) {
func (src ByteaArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (dst *CID) DecodeBinary(ci *ConnInfo, src []byte) error {
return (*pguint32)(dst).DecodeBinary(ci, src)
}

func (src *CID) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*pguint32)(src).EncodeText(ci, buf)
func (src CID) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (pguint32)(src).EncodeText(ci, buf)
}

func (src *CID) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*pguint32)(src).EncodeBinary(ci, buf)
func (src CID) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (pguint32)(src).EncodeBinary(ci, buf)
}

// Scan implements the database/sql Scanner interface.
Expand All @@ -56,6 +56,6 @@ func (dst *CID) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *CID) Value() (driver.Value, error) {
return (*pguint32)(src).Value()
func (src CID) Value() (driver.Value, error) {
return (pguint32)(src).Value()
}
8 changes: 4 additions & 4 deletions cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func (dst *CIDR) DecodeBinary(ci *ConnInfo, src []byte) error {
return (*Inet)(dst).DecodeBinary(ci, src)
}

func (src *CIDR) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*Inet)(src).EncodeText(ci, buf)
func (src CIDR) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Inet)(src).EncodeText(ci, buf)
}

func (src *CIDR) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (*Inet)(src).EncodeBinary(ci, buf)
func (src CIDR) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
return (Inet)(src).EncodeBinary(ci, buf)
}
6 changes: 3 additions & 3 deletions cidr_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (dst *CIDRArray) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *CIDRArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src CIDRArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -254,7 +254,7 @@ func (src *CIDRArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *CIDRArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src CIDRArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -317,7 +317,7 @@ func (dst *CIDRArray) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *CIDRArray) Value() (driver.Value, error) {
func (src CIDRArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions circle.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (dst *Circle) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *Circle) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Circle) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand All @@ -112,7 +112,7 @@ func (src *Circle) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return buf, nil
}

func (src *Circle) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Circle) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -146,6 +146,6 @@ func (dst *Circle) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *Circle) Value() (driver.Value, error) {
func (src Circle) Value() (driver.Value, error) {
return EncodeValueText(src)
}
6 changes: 3 additions & 3 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (dst *Date) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

func (src *Date) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Date) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand All @@ -147,7 +147,7 @@ func (src *Date) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
return append(buf, s...), nil
}

func (src *Date) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
func (src Date) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
switch src.Status {
case Null:
return nil, nil
Expand Down Expand Up @@ -195,7 +195,7 @@ func (dst *Date) Scan(src interface{}) error {
}

// Value implements the database/sql/driver Valuer interface.
func (src *Date) Value() (driver.Value, error) {
func (src Date) Value() (driver.Value, error) {
switch src.Status {
case Present:
if src.InfinityModifier != None {
Expand Down
Loading

0 comments on commit a8802b1

Please sign in to comment.