Skip to content

Commit 3028527

Browse files
committed
prepare for negative values
1 parent b55f86e commit 3028527

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

replication/event.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -582,34 +582,24 @@ func (e *GtidTaggedLogEvent) Decode(data []byte) error {
582582
},
583583
{
584584
Name: "immediate_commit_timestamp",
585-
Type: serialization.FieldIntVar{
586-
Unsigned: true,
587-
},
585+
Type: serialization.FieldUintVar{},
588586
},
589587
{
590-
Name: "original_commit_timestamp",
591-
Type: serialization.FieldIntVar{
592-
Unsigned: true,
593-
},
588+
Name: "original_commit_timestamp",
589+
Type: serialization.FieldUintVar{},
594590
Optional: true,
595591
},
596592
{
597593
Name: "transaction_length",
598-
Type: serialization.FieldIntVar{
599-
Unsigned: true,
600-
},
594+
Type: serialization.FieldUintVar{},
601595
},
602596
{
603597
Name: "immediate_server_version",
604-
Type: serialization.FieldIntVar{
605-
Unsigned: true,
606-
},
598+
Type: serialization.FieldUintVar{},
607599
},
608600
{
609-
Name: "original_server_version",
610-
Type: serialization.FieldIntVar{
611-
Unsigned: true,
612-
},
601+
Name: "original_server_version",
602+
Type: serialization.FieldUintVar{},
613603
Optional: true,
614604
},
615605
{
@@ -637,7 +627,7 @@ func (e *GtidTaggedLogEvent) Dump(w io.Writer) {
637627
}
638628

639629
if v, ok := f.Type.(serialization.FieldIntVar); ok {
640-
fmt.Printf("Immediate server version: %d\n",v.Value)
630+
fmt.Printf("Immediate server version: %d\n", v.Value)
641631
}
642632
}
643633

serialization/serialization.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,21 @@ func (f FieldIntFixed) String() string {
8484
}
8585

8686
type FieldIntVar struct {
87-
Value uint64
88-
Unsigned bool
87+
Value int64
8988
}
9089

9190
func (f FieldIntVar) String() string {
9291
return fmt.Sprintf("%d", f.Value)
9392
}
9493

94+
type FieldUintVar struct {
95+
Value uint64
96+
}
97+
98+
func (f FieldUintVar) String() string {
99+
return fmt.Sprintf("%d", f.Value)
100+
}
101+
95102
type FieldString struct {
96103
Value string
97104
}
@@ -154,11 +161,27 @@ func Unmarshal(data []byte, v interface{}) error {
154161
return err
155162
}
156163
m.Fields[i].Type = f
164+
case FieldUintVar:
165+
val, err := decodeVar(r, true)
166+
if err != nil {
167+
return err
168+
}
169+
if uintval, ok := val.(uint64); ok {
170+
f.Value = uintval
171+
} else {
172+
return errors.New("unexpected type, expecting uint64")
173+
}
174+
m.Fields[i].Type = f
157175
case FieldIntVar:
158-
f.Value, err = decodeVar(r, f.Unsigned)
176+
val, err := decodeVar(r, false)
159177
if err != nil {
160178
return err
161179
}
180+
if intval, ok := val.(int64); ok {
181+
f.Value = intval
182+
} else {
183+
return errors.New("unexpected type, expecting int64")
184+
}
162185
m.Fields[i].Type = f
163186
case FieldString:
164187
f.Value, err = decodeString(r)
@@ -227,7 +250,7 @@ func decodeFixed(r io.Reader, len int) ([]byte, error) {
227250
return b.Bytes(), nil
228251
}
229252

230-
func decodeVar(r io.ReadSeeker, unsigned bool) (uint64, error) {
253+
func decodeVar(r io.ReadSeeker, unsigned bool) (interface{}, error) {
231254
firstByte := make([]byte, 1)
232255
_, err := r.Read(firstByte)
233256
if err != nil {
@@ -243,7 +266,7 @@ func decodeVar(r io.ReadSeeker, unsigned bool) (uint64, error) {
243266
if err != nil {
244267
return 0, err
245268
}
246-
if n != tb+1{
269+
if n != tb+1 {
247270
return 0, fmt.Errorf("only read %d bytes, expected %d", n, tb+1)
248271
}
249272
var tNum uint64
@@ -270,9 +293,9 @@ func decodeVar(r io.ReadSeeker, unsigned bool) (uint64, error) {
270293
tNum = binary.LittleEndian.Uint64(fieldBytes)
271294
}
272295
if unsigned {
273-
return (tNum >> (tb + 2) * 2)+1, nil
296+
return (tNum >> (tb + 2) * 2) + 1, nil
274297
}
275-
return tNum >> (tb + 2), nil
298+
return int64(tNum >> (tb + 2)), nil
276299
}
277300

278301
func trailingOneBitCount(b byte) (count int) {

serialization/serialization_test.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func TestDecodeFixed(t *testing.T) {
7575
} else {
7676
require.ErrorContains(t, err, tc.err)
7777
}
78-
7978
}
8079
}
8180

@@ -120,10 +119,10 @@ func TestDecodeString(t *testing.T) {
120119

121120
func TestDecodeVar(t *testing.T) {
122121
testcases := []struct {
123-
input []byte
122+
input []byte
124123
unsigned bool
125-
result uint64
126-
err string
124+
result interface{}
125+
err string
127126
}{
128127
{
129128
[]byte{},
@@ -140,68 +139,68 @@ func TestDecodeVar(t *testing.T) {
140139
{
141140
[]byte{0x4},
142141
false,
143-
1,
142+
int64(1),
144143
"",
145144
},
146145
{
147146
[]byte{0xd9, 0x03},
148147
false,
149-
123,
148+
int64(123),
150149
"",
151150
},
152151
// {
153152
// []byte{0xc3, 02, 0x0b},
154153
// true,
155-
// 90200,
154+
// uint64(90200),
156155
// "",
157156
// },
158157
{
159158
// From the example on https://dev.mysql.com/doc/dev/mysql-server/latest/PageLibsMysqlSerialization.html
160159
// But converted to LE
161160
[]byte{0b11111011, 0b11111111, 0b00000111},
162161
true,
163-
65535,
162+
uint64(65535),
164163
"",
165164
},
166165
{
167166
// From the example on https://dev.mysql.com/doc/dev/mysql-server/latest/PageLibsMysqlSerialization.html
168167
// But converted to LE
169168
[]byte{0b11111011, 0b11111111, 0b00001111},
170169
false,
171-
65535,
170+
int64(65535),
172171
"",
173172
},
174173
// {
175174
// // From the example on https://dev.mysql.com/doc/dev/mysql-server/latest/PageLibsMysqlSerialization.html
176175
// // But converted to LE
177176
// []byte{0b11101011, 0b11111111, 0b00001111},
178177
// false,
179-
// -65535,
178+
// int64(-65535),
180179
// "",
181180
// },
182181
// {
183182
// // From the example on https://dev.mysql.com/doc/dev/mysql-server/latest/PageLibsMysqlSerialization.html
184183
// // But converted to LE
185184
// []byte{0b11111011, 0b11111111, 0b00001111},
186185
// false,
187-
// -65536,
186+
// int64(-65536),
188187
// "",
189188
// },
190189
{
191190
[]byte{0x5d, 0x03},
192191
true,
193-
215,
192+
uint64(215),
194193
"",
195194
},
196195
{
197196
[]byte{0x7f, 0x39, 0x7d, 0x89, 0x70, 0xdb, 0x2d, 0x06},
198197
true,
199-
1739270369410361,
198+
uint64(1739270369410361),
200199
"",
201200
},
202201
}
203202

204-
for _, tc := range testcases{
203+
for _, tc := range testcases {
205204
r, err := decodeVar(bytes.NewReader(tc.input), tc.unsigned)
206205
if tc.err == "" {
207206
require.NoError(t, err)

0 commit comments

Comments
 (0)