Skip to content

Commit

Permalink
[MySQL] Extending Binary converter to handle string in addition to …
Browse files Browse the repository at this point in the history
…`[]bytes` (#614)
  • Loading branch information
Tang8330 authored Dec 19, 2024
1 parent f122726 commit 54879bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/mysql/schema/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ func ConvertValue(value any, colType DataType, opts *Opts) (any, error) {
}
return value, nil
case Binary, Varbinary, Blob:
// Types we expect as a byte array
_, ok := value.([]byte)
if !ok {
return nil, fmt.Errorf("expected []byte got %T for value: %v", value, value)
switch castedValue := value.(type) {
case string:
return []byte(castedValue), nil
case []byte:
return castedValue, nil
default:
return nil, fmt.Errorf("expected []byte or string got %T for value: %v", value, value)
}
return value, nil
case Date:
// MySQL supports 0000-00-00 for dates so we can't use time.Time
return asString(value)
Expand Down
4 changes: 2 additions & 2 deletions lib/mysql/schema/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func TestConvertValue(t *testing.T) {
{
name: "binary - malformed",
dataType: Binary,
value: "bad binary",
expectedErr: "expected []byte got string for value",
value: true,
expectedErr: "expected []byte or string got bool for value",
},
{
name: "varbinary",
Expand Down

0 comments on commit 54879bd

Please sign in to comment.