Skip to content

Commit

Permalink
Fix uint128
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Dec 11, 2022
1 parent a9aa037 commit 12ea719
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,16 @@ func (e *Encoder) WriteUint128(i Uint128, order binary.ByteOrder) (err error) {
zlog.Debug("encode: write uint128", zap.Stringer("hex", i), zap.Uint64("lo", i.Lo), zap.Uint64("hi", i.Hi))
}
buf := make([]byte, TypeSize.Uint128)
order.PutUint64(buf, i.Lo)
order.PutUint64(buf[TypeSize.Uint64:], i.Hi)
switch order {
case binary.LittleEndian:
order.PutUint64(buf[:8], i.Lo)
order.PutUint64(buf[8:], i.Hi)
case binary.BigEndian:
order.PutUint64(buf[:8], i.Hi)
order.PutUint64(buf[8:], i.Lo)
default:
return fmt.Errorf("invalid byte order: %v", order)
}
return e.toWriter(buf)
}

Expand All @@ -274,8 +282,16 @@ func (e *Encoder) WriteInt128(i Int128, order binary.ByteOrder) (err error) {
zlog.Debug("encode: write int128", zap.Stringer("hex", i), zap.Uint64("lo", i.Lo), zap.Uint64("hi", i.Hi))
}
buf := make([]byte, TypeSize.Uint128)
order.PutUint64(buf, i.Lo)
order.PutUint64(buf[TypeSize.Uint64:], i.Hi)
switch order {
case binary.LittleEndian:
order.PutUint64(buf[:8], i.Lo)
order.PutUint64(buf[8:], i.Hi)
case binary.BigEndian:
order.PutUint64(buf[:8], i.Hi)
order.PutUint64(buf[8:], i.Lo)
default:
return fmt.Errorf("invalid byte order: %v", order)
}
return e.toWriter(buf)
}

Expand Down
2 changes: 1 addition & 1 deletion encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ func TestEncoder_Uint128(t *testing.T) {
enc.WriteUint128(u, BE)

assert.Equal(t, []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
}, buf.Bytes())
}

Expand Down

0 comments on commit 12ea719

Please sign in to comment.