mirror of https://github.com/jackc/pgx.git
support text scanner for binary format for uint32
parent
4f7e19d67d
commit
218c15a4eb
pgtype
|
@ -205,6 +205,8 @@ func (Uint32Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPl
|
|||
return scanPlanBinaryUint32ToUint32{}
|
||||
case Uint32Scanner:
|
||||
return scanPlanBinaryUint32ToUint32Scanner{}
|
||||
case TextScanner:
|
||||
return scanPlanBinaryTextToUint32Scanner{}
|
||||
}
|
||||
case TextFormatCode:
|
||||
switch target.(type) {
|
||||
|
@ -282,6 +284,27 @@ func (scanPlanBinaryUint32ToUint32Scanner) Scan(src []byte, dst any) error {
|
|||
return s.ScanUint32(Uint32{Uint32: n, Valid: true})
|
||||
}
|
||||
|
||||
type scanPlanBinaryTextToUint32Scanner struct{}
|
||||
|
||||
func (scanPlanBinaryTextToUint32Scanner) Scan(src []byte, dst any) error {
|
||||
s, ok := (dst).(TextScanner)
|
||||
if !ok {
|
||||
return ErrScanTargetTypeChanged
|
||||
}
|
||||
|
||||
if src == nil {
|
||||
return s.ScanText(Text{})
|
||||
}
|
||||
|
||||
if len(src) != 4 {
|
||||
return fmt.Errorf("invalid length for uint4: %v", len(src))
|
||||
}
|
||||
|
||||
n := uint64(binary.BigEndian.Uint32(src))
|
||||
|
||||
return s.ScanText(Text{String: strconv.FormatUint(n, 10), Valid: true})
|
||||
}
|
||||
|
||||
type scanPlanTextAnyToUint32Scanner struct{}
|
||||
|
||||
func (scanPlanTextAnyToUint32Scanner) Scan(src []byte, dst any) error {
|
||||
|
|
|
@ -17,5 +17,6 @@ func TestUint32Codec(t *testing.T) {
|
|||
},
|
||||
{pgtype.Uint32{}, new(pgtype.Uint32), isExpectedEq(pgtype.Uint32{})},
|
||||
{nil, new(pgtype.Uint32), isExpectedEq(pgtype.Uint32{})},
|
||||
{"1147", new(string), isExpectedEq("1147")},
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue