From 989a4835de30e552a5aec5f3d700aed30492c080 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 12 May 2022 17:13:49 -0500 Subject: [PATCH] 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". --- pgtype/enum_codec.go | 4 ---- pgtype/text.go | 31 ------------------------------- pgtype/text_test.go | 5 ----- 3 files changed, 40 deletions(-) diff --git a/pgtype/enum_codec.go b/pgtype/enum_codec.go index 93513111..3d23b12f 100644 --- a/pgtype/enum_codec.go +++ b/pgtype/enum_codec.go @@ -28,8 +28,6 @@ func (EnumCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodeP return encodePlanTextCodecString{} case []byte: return encodePlanTextCodecByteSlice{} - case rune: - return encodePlanTextCodecRune{} case TextValuer: return encodePlanTextCodecTextValuer{} } @@ -48,8 +46,6 @@ func (c *EnumCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanP return scanPlanAnyToNewByteSlice{} case TextScanner: return &scanPlanTextAnyToEnumTextScanner{codec: c} - case *rune: - return scanPlanTextAnyToRune{} } } diff --git a/pgtype/text.go b/pgtype/text.go index 0f9df7a6..7f779d11 100644 --- a/pgtype/text.go +++ b/pgtype/text.go @@ -4,7 +4,6 @@ import ( "database/sql/driver" "encoding/json" "fmt" - "unicode/utf8" ) type TextScanner interface { @@ -98,8 +97,6 @@ func (TextCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodeP return encodePlanTextCodecString{} case []byte: return encodePlanTextCodecByteSlice{} - case rune: - return encodePlanTextCodecRune{} case TextValuer: return encodePlanTextCodecTextValuer{} } @@ -124,14 +121,6 @@ func (encodePlanTextCodecByteSlice) Encode(value any, buf []byte) (newBuf []byte 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{} 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{} case TextScanner: return scanPlanTextAnyToTextScanner{} - case *rune: - return scanPlanTextAnyToRune{} } } @@ -223,24 +210,6 @@ func (scanPlanAnyToByteScanner) Scan(src []byte, dst any) error { 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{} func (scanPlanTextAnyToTextScanner) Scan(src []byte, dst any) error { diff --git a/pgtype/text_test.go b/pgtype/text_test.go index 1d717f49..eb5d005e 100644 --- a/pgtype/text_test.go +++ b/pgtype/text_test.go @@ -33,11 +33,6 @@ func TestTextCodec(t *testing.T) { {"foo", new(string), isExpectedEq("foo")}, {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'))}, - }) } }