Remove rune to text conversion
Because rune is an alias for int32 this caused some very surprising results. e.g. inserting int32(65) into text would insert "A" instead of "65".
This commit is contained in:
@@ -28,8 +28,6 @@ func (EnumCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodeP
|
|||||||
return encodePlanTextCodecString{}
|
return encodePlanTextCodecString{}
|
||||||
case []byte:
|
case []byte:
|
||||||
return encodePlanTextCodecByteSlice{}
|
return encodePlanTextCodecByteSlice{}
|
||||||
case rune:
|
|
||||||
return encodePlanTextCodecRune{}
|
|
||||||
case TextValuer:
|
case TextValuer:
|
||||||
return encodePlanTextCodecTextValuer{}
|
return encodePlanTextCodecTextValuer{}
|
||||||
}
|
}
|
||||||
@@ -48,8 +46,6 @@ func (c *EnumCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanP
|
|||||||
return scanPlanAnyToNewByteSlice{}
|
return scanPlanAnyToNewByteSlice{}
|
||||||
case TextScanner:
|
case TextScanner:
|
||||||
return &scanPlanTextAnyToEnumTextScanner{codec: c}
|
return &scanPlanTextAnyToEnumTextScanner{codec: c}
|
||||||
case *rune:
|
|
||||||
return scanPlanTextAnyToRune{}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"unicode/utf8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextScanner interface {
|
type TextScanner interface {
|
||||||
@@ -98,8 +97,6 @@ func (TextCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodeP
|
|||||||
return encodePlanTextCodecString{}
|
return encodePlanTextCodecString{}
|
||||||
case []byte:
|
case []byte:
|
||||||
return encodePlanTextCodecByteSlice{}
|
return encodePlanTextCodecByteSlice{}
|
||||||
case rune:
|
|
||||||
return encodePlanTextCodecRune{}
|
|
||||||
case TextValuer:
|
case TextValuer:
|
||||||
return encodePlanTextCodecTextValuer{}
|
return encodePlanTextCodecTextValuer{}
|
||||||
}
|
}
|
||||||
@@ -124,14 +121,6 @@ func (encodePlanTextCodecByteSlice) Encode(value any, buf []byte) (newBuf []byte
|
|||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type encodePlanTextCodecRune struct{}
|
|
||||||
|
|
||||||
func (encodePlanTextCodecRune) Encode(value any, buf []byte) (newBuf []byte, err error) {
|
|
||||||
r := value.(rune)
|
|
||||||
buf = append(buf, string(r)...)
|
|
||||||
return buf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type encodePlanTextCodecStringer struct{}
|
type encodePlanTextCodecStringer struct{}
|
||||||
|
|
||||||
func (encodePlanTextCodecStringer) Encode(value any, buf []byte) (newBuf []byte, err error) {
|
func (encodePlanTextCodecStringer) Encode(value any, buf []byte) (newBuf []byte, err error) {
|
||||||
@@ -169,8 +158,6 @@ func (TextCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan
|
|||||||
return scanPlanAnyToByteScanner{}
|
return scanPlanAnyToByteScanner{}
|
||||||
case TextScanner:
|
case TextScanner:
|
||||||
return scanPlanTextAnyToTextScanner{}
|
return scanPlanTextAnyToTextScanner{}
|
||||||
case *rune:
|
|
||||||
return scanPlanTextAnyToRune{}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,24 +210,6 @@ func (scanPlanAnyToByteScanner) Scan(src []byte, dst any) error {
|
|||||||
return p.ScanBytes(src)
|
return p.ScanBytes(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
type scanPlanTextAnyToRune struct{}
|
|
||||||
|
|
||||||
func (scanPlanTextAnyToRune) Scan(src []byte, dst any) error {
|
|
||||||
if src == nil {
|
|
||||||
return fmt.Errorf("cannot scan null into %T", dst)
|
|
||||||
}
|
|
||||||
|
|
||||||
r, size := utf8.DecodeRune(src)
|
|
||||||
if size != len(src) {
|
|
||||||
return fmt.Errorf("cannot scan %v into %T: more than one rune received", src, dst)
|
|
||||||
}
|
|
||||||
|
|
||||||
p := (dst).(*rune)
|
|
||||||
*p = r
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type scanPlanTextAnyToTextScanner struct{}
|
type scanPlanTextAnyToTextScanner struct{}
|
||||||
|
|
||||||
func (scanPlanTextAnyToTextScanner) Scan(src []byte, dst any) error {
|
func (scanPlanTextAnyToTextScanner) Scan(src []byte, dst any) error {
|
||||||
|
|||||||
@@ -33,11 +33,6 @@ func TestTextCodec(t *testing.T) {
|
|||||||
{"foo", new(string), isExpectedEq("foo")},
|
{"foo", new(string), isExpectedEq("foo")},
|
||||||
{someFmtStringer{}, new(string), isExpectedEq("some fmt.Stringer")},
|
{someFmtStringer{}, new(string), isExpectedEq("some fmt.Stringer")},
|
||||||
})
|
})
|
||||||
|
|
||||||
// rune requires known OID because otherwise it is considered an int32.
|
|
||||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, pgTypeName, []pgxtest.ValueRoundTripTest{
|
|
||||||
{rune('R'), new(rune), isExpectedEq(rune('R'))},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user