Skip to content

Commit

Permalink
sink: fix codec json encoding with non binary string (#2764) (#2781)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Sep 10, 2021
1 parent ee5ac46 commit c4e9321
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cdc/sink/codec/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ func (c *column) FromSinkColumn(col *model.Column) {
}
switch col.Type {
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar:
str := string(col.Value.([]byte))
var str string
switch col.Value.(type) {
case []byte:
str = string(col.Value.([]byte))
case string:
str = col.Value.(string)
default:
log.Panic("invalid column value, please report a bug", zap.Any("col", col))
}
if c.Flag.IsBinary() {
str = strconv.Quote(str)
str = str[1 : len(str)-1]
Expand Down
22 changes: 22 additions & 0 deletions cdc/sink/codec/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@ func (s *columnSuite) TestFormatCol(c *check.C) {
c.Assert(row2, check.DeepEquals, row)
}

func (s *columnSuite) TestNonBinaryStringCol(c *check.C) {
defer testleak.AfterTest(c)()
col := &model.Column{
Name: "test",
Type: mysql.TypeString,
Value: "value",
}
jsonCol := column{}
jsonCol.FromSinkColumn(col)
row := &mqMessageRow{Update: map[string]column{"test": jsonCol}}
rowEncode, err := row.Encode()
c.Assert(err, check.IsNil)
row2 := new(mqMessageRow)
err = row2.Decode(rowEncode)
c.Assert(err, check.IsNil)
c.Assert(row2, check.DeepEquals, row)
jsonCol2 := row2.Update["test"]
col2 := jsonCol2.ToSinkColumn("test")
col2.Value = string(col2.Value.([]byte))
c.Assert(col2, check.DeepEquals, col)
}

func (s *columnSuite) TestVarBinaryCol(c *check.C) {
defer testleak.AfterTest(c)()
col := &model.Column{
Expand Down

0 comments on commit c4e9321

Please sign in to comment.