mirror of https://github.com/jackc/pgx.git
Convert point to Codec
parent
dcaf102f8e
commit
2b0afbb408
|
@ -303,7 +303,7 @@ func NewConnInfo() *ConnInfo {
|
|||
// ci.RegisterDataType(DataType{Value: &Numrange{}, Name: "numrange", OID: NumrangeOID})
|
||||
ci.RegisterDataType(DataType{Value: &OIDValue{}, Name: "oid", OID: OIDOID})
|
||||
ci.RegisterDataType(DataType{Value: &Path{}, Name: "path", OID: PathOID})
|
||||
ci.RegisterDataType(DataType{Value: &Point{}, Name: "point", OID: PointOID})
|
||||
ci.RegisterDataType(DataType{Name: "point", OID: PointOID, Codec: PointCodec{}})
|
||||
ci.RegisterDataType(DataType{Value: &Polygon{}, Name: "polygon", OID: PolygonOID})
|
||||
// ci.RegisterDataType(DataType{Value: &Record{}, Name: "record", OID: RecordOID})
|
||||
ci.RegisterDataType(DataType{Value: &Text{}, Name: "text", OID: TextOID})
|
||||
|
|
266
pgtype/point.go
266
pgtype/point.go
|
@ -17,33 +17,28 @@ type Vec2 struct {
|
|||
Y float64
|
||||
}
|
||||
|
||||
type PointScanner interface {
|
||||
ScanPoint(v Point) error
|
||||
}
|
||||
|
||||
type PointValuer interface {
|
||||
PointValue() (Point, error)
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
P Vec2
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func (dst *Point) Set(src interface{}) error {
|
||||
if src == nil {
|
||||
dst.Valid = false
|
||||
return nil
|
||||
}
|
||||
err := fmt.Errorf("cannot convert %v to Point", src)
|
||||
var p *Point
|
||||
switch value := src.(type) {
|
||||
case string:
|
||||
p, err = parsePoint([]byte(value))
|
||||
case []byte:
|
||||
p, err = parsePoint(value)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dst = *p
|
||||
func (p *Point) ScanPoint(v Point) error {
|
||||
*p = v
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p Point) PointValue() (Point, error) {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func parsePoint(src []byte) (*Point, error) {
|
||||
if src == nil || bytes.Compare(src, []byte("null")) == 0 {
|
||||
return &Point{}, nil
|
||||
|
@ -73,87 +68,6 @@ func parsePoint(src []byte) (*Point, error) {
|
|||
return &Point{P: Vec2{x, y}, Valid: true}, nil
|
||||
}
|
||||
|
||||
func (dst Point) Get() interface{} {
|
||||
if !dst.Valid {
|
||||
return nil
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func (src *Point) AssignTo(dst interface{}) error {
|
||||
return fmt.Errorf("cannot assign %v to %T", src, dst)
|
||||
}
|
||||
|
||||
func (dst *Point) DecodeText(ci *ConnInfo, src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Point{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(src) < 5 {
|
||||
return fmt.Errorf("invalid length for point: %v", len(src))
|
||||
}
|
||||
|
||||
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
|
||||
if len(parts) < 2 {
|
||||
return fmt.Errorf("invalid format for point")
|
||||
}
|
||||
|
||||
x, err := strconv.ParseFloat(parts[0], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
y, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Point{P: Vec2{x, y}, Valid: true}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Point) DecodeBinary(ci *ConnInfo, src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Point{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(src) != 16 {
|
||||
return fmt.Errorf("invalid length for point: %v", len(src))
|
||||
}
|
||||
|
||||
x := binary.BigEndian.Uint64(src)
|
||||
y := binary.BigEndian.Uint64(src[8:])
|
||||
|
||||
*dst = Point{
|
||||
P: Vec2{math.Float64frombits(x), math.Float64frombits(y)},
|
||||
Valid: true,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Point) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return append(buf, fmt.Sprintf(`(%s,%s)`,
|
||||
strconv.FormatFloat(src.P.X, 'f', -1, 64),
|
||||
strconv.FormatFloat(src.P.Y, 'f', -1, 64),
|
||||
)...), nil
|
||||
}
|
||||
|
||||
func (src Point) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
buf = pgio.AppendUint64(buf, math.Float64bits(src.P.X))
|
||||
buf = pgio.AppendUint64(buf, math.Float64bits(src.P.Y))
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Point) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
|
@ -163,11 +77,7 @@ func (dst *Point) Scan(src interface{}) error {
|
|||
|
||||
switch src := src.(type) {
|
||||
case string:
|
||||
return dst.DecodeText(nil, []byte(src))
|
||||
case []byte:
|
||||
srcCopy := make([]byte, len(src))
|
||||
copy(srcCopy, src)
|
||||
return dst.DecodeText(nil, srcCopy)
|
||||
return scanPlanTextAnyToPointScanner{}.Scan(nil, 0, TextFormatCode, []byte(src), dst)
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot scan %T", src)
|
||||
|
@ -175,7 +85,11 @@ func (dst *Point) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Point) Value() (driver.Value, error) {
|
||||
return EncodeValueText(src)
|
||||
buf, err := PointCodec{}.PlanEncode(nil, 0, TextFormatCode, src).Encode(src, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return string(buf), err
|
||||
}
|
||||
|
||||
func (src Point) MarshalJSON() ([]byte, error) {
|
||||
|
@ -198,3 +112,143 @@ func (dst *Point) UnmarshalJSON(point []byte) error {
|
|||
*dst = *p
|
||||
return nil
|
||||
}
|
||||
|
||||
type PointCodec struct{}
|
||||
|
||||
func (PointCodec) FormatSupported(format int16) bool {
|
||||
return format == TextFormatCode || format == BinaryFormatCode
|
||||
}
|
||||
|
||||
func (PointCodec) PreferredFormat() int16 {
|
||||
return BinaryFormatCode
|
||||
}
|
||||
|
||||
func (PointCodec) PlanEncode(ci *ConnInfo, oid uint32, format int16, value interface{}) EncodePlan {
|
||||
if _, ok := value.(PointValuer); !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch format {
|
||||
case BinaryFormatCode:
|
||||
return &encodePlanPointCodecBinary{}
|
||||
case TextFormatCode:
|
||||
return &encodePlanPointCodecText{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type encodePlanPointCodecBinary struct{}
|
||||
|
||||
func (p *encodePlanPointCodecBinary) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
point, err := value.(PointValuer).PointValue()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf = pgio.AppendUint64(buf, math.Float64bits(point.P.X))
|
||||
buf = pgio.AppendUint64(buf, math.Float64bits(point.P.Y))
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
type encodePlanPointCodecText struct{}
|
||||
|
||||
func (p *encodePlanPointCodecText) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
point, err := value.(PointValuer).PointValue()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return append(buf, fmt.Sprintf(`(%s,%s)`,
|
||||
strconv.FormatFloat(point.P.X, 'f', -1, 64),
|
||||
strconv.FormatFloat(point.P.Y, 'f', -1, 64),
|
||||
)...), nil
|
||||
}
|
||||
|
||||
func (PointCodec) PlanScan(ci *ConnInfo, oid uint32, format int16, target interface{}, actualTarget bool) ScanPlan {
|
||||
|
||||
switch format {
|
||||
case BinaryFormatCode:
|
||||
switch target.(type) {
|
||||
case PointScanner:
|
||||
return scanPlanBinaryPointToPointScanner{}
|
||||
}
|
||||
case TextFormatCode:
|
||||
switch target.(type) {
|
||||
case PointScanner:
|
||||
return scanPlanTextAnyToPointScanner{}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c PointCodec) DecodeDatabaseSQLValue(ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
|
||||
return codecDecodeToTextFormat(c, ci, oid, format, src)
|
||||
}
|
||||
|
||||
func (c PointCodec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src []byte) (interface{}, error) {
|
||||
if src == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var point Point
|
||||
err := codecScan(c, ci, oid, format, src, &point)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return point, nil
|
||||
}
|
||||
|
||||
type scanPlanBinaryPointToPointScanner struct{}
|
||||
|
||||
func (scanPlanBinaryPointToPointScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
scanner := (dst).(PointScanner)
|
||||
|
||||
if src == nil {
|
||||
return scanner.ScanPoint(Point{})
|
||||
}
|
||||
|
||||
if len(src) != 16 {
|
||||
return fmt.Errorf("invalid length for point: %v", len(src))
|
||||
}
|
||||
|
||||
x := binary.BigEndian.Uint64(src)
|
||||
y := binary.BigEndian.Uint64(src[8:])
|
||||
|
||||
return scanner.ScanPoint(Point{
|
||||
P: Vec2{math.Float64frombits(x), math.Float64frombits(y)},
|
||||
Valid: true,
|
||||
})
|
||||
}
|
||||
|
||||
type scanPlanTextAnyToPointScanner struct{}
|
||||
|
||||
func (scanPlanTextAnyToPointScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
scanner := (dst).(PointScanner)
|
||||
|
||||
if src == nil {
|
||||
return scanner.ScanPoint(Point{})
|
||||
}
|
||||
|
||||
if len(src) < 5 {
|
||||
return fmt.Errorf("invalid length for point: %v", len(src))
|
||||
}
|
||||
|
||||
parts := strings.SplitN(string(src[1:len(src)-1]), ",", 2)
|
||||
if len(parts) < 2 {
|
||||
return fmt.Errorf("invalid format for point")
|
||||
}
|
||||
|
||||
x, err := strconv.ParseFloat(parts[0], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
y, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return scanner.ScanPoint(Point{P: Vec2{x, y}, Valid: true})
|
||||
}
|
||||
|
|
|
@ -5,63 +5,25 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPointTranscode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "point", []interface{}{
|
||||
&pgtype.Point{P: pgtype.Vec2{1.234, 5.6789012345}, Valid: true},
|
||||
&pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Valid: true},
|
||||
&pgtype.Point{},
|
||||
func TestPointCodec(t *testing.T) {
|
||||
testPgxCodec(t, "point", []PgxTranscodeTestCase{
|
||||
{
|
||||
pgtype.Point{P: pgtype.Vec2{1.234, 5.6789012345}, Valid: true},
|
||||
new(pgtype.Point),
|
||||
isExpectedEq(pgtype.Point{P: pgtype.Vec2{1.234, 5.6789012345}, Valid: true}),
|
||||
},
|
||||
{
|
||||
pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Valid: true},
|
||||
new(pgtype.Point),
|
||||
isExpectedEq(pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Valid: true}),
|
||||
},
|
||||
{nil, new(pgtype.Point), isExpectedEq(pgtype.Point{})},
|
||||
})
|
||||
}
|
||||
|
||||
func TestPoint_Set(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg interface{}
|
||||
valid bool
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "first",
|
||||
arg: "(12312.123123,123123.123123)",
|
||||
valid: true,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "second",
|
||||
arg: "(1231s2.123123,123123.123123)",
|
||||
valid: false,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "third",
|
||||
arg: []byte("(122.123123,123.123123)"),
|
||||
valid: true,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "third",
|
||||
arg: nil,
|
||||
valid: false,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dst := &pgtype.Point{}
|
||||
if err := dst.Set(tt.arg); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if dst.Valid != tt.valid {
|
||||
t.Errorf("Expected status: %v; got: %v", tt.valid, dst.Valid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoint_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
Loading…
Reference in New Issue