diff --git a/encoder.go b/encoder.go index b9fb199..727477a 100644 --- a/encoder.go +++ b/encoder.go @@ -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) } @@ -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) } diff --git a/encoder_test.go b/encoder_test.go index e41df51..72bf966 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -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()) }