Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,16 @@ func (id *ULID) Scan(src interface{}) error {
case string:
return id.UnmarshalText([]byte(x))
case []byte:
return id.UnmarshalBinary(x)
// Drivers often return text/varchar columns as []byte. Accept both
// the 16-byte binary form and the 26-character text encoding.
switch len(x) {
case len(*id):
return id.UnmarshalBinary(x)
case EncodedSize:
return id.UnmarshalText(x)
default:
return ErrDataSize
}
}

return ErrScanValue
Expand Down
1 change: 1 addition & 0 deletions ulid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ func TestScan(t *testing.T) {
}{
{"string", id.String(), id, nil},
{"bytes", id[:], id, nil},
{"text-as-bytes", []byte(id.String()), id, nil},
{"nil", nil, ulid.ULID{}, nil},
{"other", 44, ulid.ULID{}, ulid.ErrScanValue},
} {
Expand Down
Loading