Skip to content

Commit

Permalink
Fixes #11 -- support initializing Array types from a slice of the value
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Gaynor committed Nov 8, 2019
1 parent f711de3 commit 0079108
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 73 deletions.
16 changes: 14 additions & 2 deletions aclitem_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func (dst *ACLItemArray) Set(src interface{}) error {
}
}

case []ACLItem:
if value == nil {
*dst = ACLItemArray{Status: Null}
} else if len(value) == 0 {
*dst = ACLItemArray{Status: Present}
} else {
*dst = ACLItemArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -124,7 +136,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 +212,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
18 changes: 15 additions & 3 deletions bool_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (dst *BoolArray) Set(src interface{}) error {
}
}

case []Bool:
if value == nil {
*dst = BoolArray{Status: Null}
} else if len(value) == 0 {
*dst = BoolArray{Status: Present}
} else {
*dst = BoolArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -168,7 +180,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 +237,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 +300,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
18 changes: 15 additions & 3 deletions bpchar_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (dst *BPCharArray) Set(src interface{}) error {
}
}

case []BPChar:
if value == nil {
*dst = BPCharArray{Status: Null}
} else if len(value) == 0 {
*dst = BPCharArray{Status: Present}
} else {
*dst = BPCharArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -168,7 +180,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 +237,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 +300,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
18 changes: 15 additions & 3 deletions bytea_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (dst *ByteaArray) Set(src interface{}) error {
}
}

case []Bytea:
if value == nil {
*dst = ByteaArray{Status: Null}
} else if len(value) == 0 {
*dst = ByteaArray{Status: Present}
} else {
*dst = ByteaArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -168,7 +180,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 +237,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 +300,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
18 changes: 15 additions & 3 deletions cidr_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func (dst *CIDRArray) Set(src interface{}) error {
}
}

case []CIDR:
if value == nil {
*dst = CIDRArray{Status: Null}
} else if len(value) == 0 {
*dst = CIDRArray{Status: Present}
} else {
*dst = CIDRArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -197,7 +209,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 +266,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 +329,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
18 changes: 15 additions & 3 deletions date_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func (dst *DateArray) Set(src interface{}) error {
}
}

case []Date:
if value == nil {
*dst = DateArray{Status: Null}
} else if len(value) == 0 {
*dst = DateArray{Status: Present}
} else {
*dst = DateArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -169,7 +181,7 @@ func (dst *DateArray) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

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

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

// Value implements the database/sql/driver Valuer interface.
func (src DateArray) Value() (driver.Value, error) {
func (src *DateArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
16 changes: 14 additions & 2 deletions enum_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func (dst *EnumArray) Set(src interface{}) error {
}
}

case []GenericText:
if value == nil {
*dst = EnumArray{Status: Null}
} else if len(value) == 0 {
*dst = EnumArray{Status: Present}
} else {
*dst = EnumArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -124,7 +136,7 @@ func (dst *EnumArray) DecodeText(ci *ConnInfo, src []byte) error {
return nil
}

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

// Value implements the database/sql/driver Valuer interface.
func (src EnumArray) Value() (driver.Value, error) {
func (src *EnumArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
18 changes: 15 additions & 3 deletions float4_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (dst *Float4Array) Set(src interface{}) error {
}
}

case []Float4:
if value == nil {
*dst = Float4Array{Status: Null}
} else if len(value) == 0 {
*dst = Float4Array{Status: Present}
} else {
*dst = Float4Array{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -168,7 +180,7 @@ func (dst *Float4Array) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

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

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

// Value implements the database/sql/driver Valuer interface.
func (src Float4Array) Value() (driver.Value, error) {
func (src *Float4Array) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
18 changes: 15 additions & 3 deletions float8_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (dst *Float8Array) Set(src interface{}) error {
}
}

case []Float8:
if value == nil {
*dst = Float8Array{Status: Null}
} else if len(value) == 0 {
*dst = Float8Array{Status: Present}
} else {
*dst = Float8Array{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Status: Present,
}
}
default:
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
Expand Down Expand Up @@ -168,7 +180,7 @@ func (dst *Float8Array) DecodeBinary(ci *ConnInfo, src []byte) error {
return nil
}

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

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

// Value implements the database/sql/driver Valuer interface.
func (src Float8Array) Value() (driver.Value, error) {
func (src *Float8Array) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0079108

Please sign in to comment.