Skip to content

Commit

Permalink
also support sql scan with array, not just slice
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Jan 11, 2025
1 parent 85f8901 commit dc156be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func (r *ID) Scan(src interface{}) error {
return r.UnmarshalBinary(src)
}
return r.UnmarshalText(src)
case [16]byte:
return r.UnmarshalBinary(src[:])
case string:
switch len(src) {
case 26, 32, 22:
Expand Down
16 changes: 14 additions & 2 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var (
func TestSQL(t *testing.T) {
t.Run("Value", testSQLValue)
t.Run("Scan", func(t *testing.T) {
t.Run("Binary", testSQLScanBinary)
t.Run("BinarySlice", testSQLScanBinarySlice)
t.Run("BinaryArray", testSQLScanBinaryArray)
t.Run("String", testSQLScanString)
t.Run("Text", testSQLScanText)
t.Run("Unsupported", testSQLScanUnsupported)
Expand All @@ -65,7 +66,7 @@ func testSQLValue(t *testing.T) {
)
}

func testSQLScanBinary(t *testing.T) {
func testSQLScanBinarySlice(t *testing.T) {
got := ID{}
err := got.Scan(codecTestData)
if err != nil {
Expand All @@ -76,6 +77,17 @@ func testSQLScanBinary(t *testing.T) {
}
}

func testSQLScanBinaryArray(t *testing.T) {
got := ID{}
err := got.Scan([16]byte(codecTestData))
if err != nil {
t.Fatal(err)
}
if !got.Equal(codecTestID) {
t.Errorf("Scan(%x): got %v, want %v", codecTestData, got, codecTestID)
}
}

func testSQLScanString(t *testing.T) {
s := "0r32b0yermw00sbjedjxe4yaz0"
got := ID{}
Expand Down

0 comments on commit dc156be

Please sign in to comment.