@@ -3,6 +3,7 @@ package replication
33import (
44 "fmt"
55 "math"
6+ "strconv"
67
78 "github.com/go-mysql-org/go-mysql/mysql"
89 "github.com/go-mysql-org/go-mysql/utils"
5253 JsonDiffOperation byte
5354)
5455
56+ type FloatWithTrailingZero float64
57+
5558const (
5659 // The JSON value in the given path is replaced with a new value.
5760 //
@@ -96,6 +99,14 @@ func (jd *JsonDiff) String() string {
9699 return fmt .Sprintf ("json_diff(op:%s path:%s value:%s)" , jd .Op , jd .Path , jd .Value )
97100}
98101
102+ func (f FloatWithTrailingZero ) MarshalJSON () ([]byte , error ) {
103+ if float64 (f ) == float64 (int (f )) {
104+ return []byte (strconv .FormatFloat (float64 (f ), 'f' , 1 , 64 )), nil
105+ }
106+
107+ return []byte (strconv .FormatFloat (float64 (f ), 'f' , - 1 , 64 )), nil
108+ }
109+
99110func jsonbGetOffsetSize (isSmall bool ) int {
100111 if isSmall {
101112 return jsonbSmallOffsetSize
@@ -124,8 +135,9 @@ func jsonbGetValueEntrySize(isSmall bool) int {
124135// the common JSON encoding data.
125136func (e * RowsEvent ) decodeJsonBinary (data []byte ) ([]byte , error ) {
126137 d := jsonBinaryDecoder {
127- useDecimal : e .useDecimal ,
128- ignoreDecodeErr : e .ignoreJSONDecodeErr ,
138+ useDecimal : e .useDecimal ,
139+ useFloatWithTrailingZero : e .useFloatWithTrailingZero ,
140+ ignoreDecodeErr : e .ignoreJSONDecodeErr ,
129141 }
130142
131143 if d .isDataShort (data , 1 ) {
@@ -141,9 +153,10 @@ func (e *RowsEvent) decodeJsonBinary(data []byte) ([]byte, error) {
141153}
142154
143155type jsonBinaryDecoder struct {
144- useDecimal bool
145- ignoreDecodeErr bool
146- err error
156+ useDecimal bool
157+ useFloatWithTrailingZero bool
158+ ignoreDecodeErr bool
159+ err error
147160}
148161
149162func (d * jsonBinaryDecoder ) decodeValue (tp byte , data []byte ) interface {} {
@@ -175,6 +188,9 @@ func (d *jsonBinaryDecoder) decodeValue(tp byte, data []byte) interface{} {
175188 case JSONB_UINT64 :
176189 return d .decodeUint64 (data )
177190 case JSONB_DOUBLE :
191+ if d .useFloatWithTrailingZero {
192+ return d .decodeDoubleWithTrailingZero (data )
193+ }
178194 return d .decodeDouble (data )
179195 case JSONB_STRING :
180196 return d .decodeString (data )
@@ -395,6 +411,11 @@ func (d *jsonBinaryDecoder) decodeDouble(data []byte) float64 {
395411 return v
396412}
397413
414+ func (d * jsonBinaryDecoder ) decodeDoubleWithTrailingZero (data []byte ) FloatWithTrailingZero {
415+ v := d .decodeDouble (data )
416+ return FloatWithTrailingZero (v )
417+ }
418+
398419func (d * jsonBinaryDecoder ) decodeString (data []byte ) string {
399420 if d .err != nil {
400421 return ""
0 commit comments