Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support text scanner for binary format for uint32 #2112

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pgtype/uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -282,6 +284,22 @@ func (scanPlanBinaryUint32ToUint32Scanner) Scan(src []byte, dst any) error {
return s.ScanUint32(Uint32{Uint32: n, Valid: true})
}

type scanPlanBinaryTextToUint32Scanner struct{}
jennifersp marked this conversation as resolved.
Show resolved Hide resolved

func (scanPlanBinaryTextToUint32Scanner) Scan(src []byte, dst any) error {
s, ok := (dst).(TextScanner)
if !ok {
return ErrScanTargetTypeChanged
}

if src == nil {
return s.ScanText(Text{})
}

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 {
Expand Down
1 change: 1 addition & 0 deletions pgtype/uint32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
})
}