Skip to content

Commit 32c356a

Browse files
author
lening.zeng
committed
fixed []byte
1 parent f4d2c6e commit 32c356a

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

transform.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ func valuesFromModel(dstType reflect.Type, dstValue reflect.Value) []driver.Valu
5656
switch cv := columnValue.(type) {
5757
case driver.Valuer:
5858
v, _ := cv.Value()
59-
values = append(values, v)
59+
if strV, ok := v.(string); ok {
60+
values = append(values, []byte(strV))
61+
} else {
62+
values = append(values, v)
63+
}
6064
default:
6165
values = append(values, columnValue)
6266
}

transform_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func Test_ValueFromField(t *testing.T) {
6969
CreateTime: 1630248918,
7070
}
7171
value := valuesFromModel(reflect.TypeOf(model), reflect.ValueOf(model))
72-
assert.Equal(t, []driver.Value{uint64(10), "test", "1,2", uint32(1630248918)}, value)
72+
assert.Equal(t, []driver.Value{uint64(10), "test", []byte("1,2"), uint32(1630248918)}, value)
7373
}
7474

7575
func Test_SingleRow(t *testing.T) {
@@ -81,7 +81,7 @@ func Test_SingleRow(t *testing.T) {
8181
}
8282

8383
targetRows := sqlmock.NewRows([]string{"id", "name", "custom_field", "create_time"}).
84-
AddRow(uint64(12), "test_abc", "1,2", uint32(1630248920))
84+
AddRow(uint64(12), "test_abc", []byte("1,2"), uint32(1630248920))
8585

8686
rows := ModelToRows(model)
8787
assert.Equal(t, targetRows, rows)
@@ -106,8 +106,8 @@ func Test_MultiStructValueRows(t *testing.T) {
106106
}
107107

108108
targetRows := sqlmock.NewRows([]string{"id", "name", "custom_field", "create_time"}).
109-
AddRow(uint64(12), "test_abc", "1,2", uint32(1630248920)).
110-
AddRow(uint64(13), "test_efg", "", uint32(1630248922))
109+
AddRow(uint64(12), "test_abc", []byte("1,2"), uint32(1630248920)).
110+
AddRow(uint64(13), "test_efg", []byte(""), uint32(1630248922))
111111

112112
rows := ModelToRows(model)
113113
assert.Equal(t, targetRows, rows)
@@ -129,8 +129,8 @@ func Test_MultiStructPtrRows(t *testing.T) {
129129
}
130130

131131
targetRows := sqlmock.NewRows([]string{"id", "name", "custom_field", "create_time"}).
132-
AddRow(uint64(12), "test_abc", "", uint32(1630248920)).
133-
AddRow(uint64(13), "test_efg", "", uint32(1630248922))
132+
AddRow(uint64(12), "test_abc", []byte(""), uint32(1630248920)).
133+
AddRow(uint64(13), "test_efg", []byte(""), uint32(1630248922))
134134

135135
rows := ModelToRows(model)
136136
assert.Equal(t, targetRows, rows)

0 commit comments

Comments
 (0)