@@ -4,27 +4,48 @@ import (
44 "reflect"
55)
66
7+ // Encoder defines an interface for encoding values into bytes.
8+ // It provides methods to get the encoding type, calculate the byte size of a value,
9+ // and write the encoded value into a byte slice.
710type Encoder interface {
11+ // Code returns the unique code representing the encoder type.
812 Code () int8
13+
14+ // Type returns the reflect.Type of the value that the encoder handles.
915 Type () reflect.Type
16+
17+ // CalcByteSize calculates the number of bytes required to encode the given value.
18+ // Returns the size and an error if the calculation fails.
1019 CalcByteSize (value reflect.Value ) (int , error )
20+
21+ // WriteToBytes encodes the given value into a byte slice starting at the specified offset.
22+ // Returns the new offset after writing the bytes.
1123 WriteToBytes (value reflect.Value , offset int , bytes * []byte ) int
1224}
1325
26+ // EncoderCommon provides utility methods for encoding various types of values into bytes.
27+ // It includes methods to encode integers and unsigned integers of different sizes,
28+ // as well as methods to write raw byte slices into a target byte slice.
1429type EncoderCommon struct {
1530}
1631
32+ // SetByte1Int64 encodes a single byte from the given int64 value into the byte slice at the specified offset.
33+ // Returns the new offset after writing the byte.
1734func (c * EncoderCommon ) SetByte1Int64 (value int64 , offset int , d * []byte ) int {
1835 (* d )[offset ] = byte (value )
1936 return offset + 1
2037}
2138
39+ // SetByte2Int64 encodes the lower two bytes of the given int64 value into the byte slice at the specified offset.
40+ // Returns the new offset after writing the bytes.
2241func (c * EncoderCommon ) SetByte2Int64 (value int64 , offset int , d * []byte ) int {
2342 (* d )[offset + 0 ] = byte (value >> 8 )
2443 (* d )[offset + 1 ] = byte (value )
2544 return offset + 2
2645}
2746
47+ // SetByte4Int64 encodes the lower four bytes of the given int64 value into the byte slice at the specified offset.
48+ // Returns the new offset after writing the bytes.
2849func (c * EncoderCommon ) SetByte4Int64 (value int64 , offset int , d * []byte ) int {
2950 (* d )[offset + 0 ] = byte (value >> 24 )
3051 (* d )[offset + 1 ] = byte (value >> 16 )
@@ -33,6 +54,8 @@ func (c *EncoderCommon) SetByte4Int64(value int64, offset int, d *[]byte) int {
3354 return offset + 4
3455}
3556
57+ // SetByte8Int64 encodes all eight bytes of the given int64 value into the byte slice at the specified offset.
58+ // Returns the new offset after writing the bytes.
3659func (c * EncoderCommon ) SetByte8Int64 (value int64 , offset int , d * []byte ) int {
3760 (* d )[offset ] = byte (value >> 56 )
3861 (* d )[offset + 1 ] = byte (value >> 48 )
@@ -45,17 +68,23 @@ func (c *EncoderCommon) SetByte8Int64(value int64, offset int, d *[]byte) int {
4568 return offset + 8
4669}
4770
71+ // SetByte1Uint64 encodes a single byte from the given uint64 value into the byte slice at the specified offset.
72+ // Returns the new offset after writing the byte.
4873func (c * EncoderCommon ) SetByte1Uint64 (value uint64 , offset int , d * []byte ) int {
4974 (* d )[offset ] = byte (value )
5075 return offset + 1
5176}
5277
78+ // SetByte2Uint64 encodes the lower two bytes of the given uint64 value into the byte slice at the specified offset.
79+ // Returns the new offset after writing the bytes.
5380func (c * EncoderCommon ) SetByte2Uint64 (value uint64 , offset int , d * []byte ) int {
5481 (* d )[offset ] = byte (value >> 8 )
5582 (* d )[offset + 1 ] = byte (value )
5683 return offset + 2
5784}
5885
86+ // SetByte4Uint64 encodes the lower four bytes of the given uint64 value into the byte slice at the specified offset.
87+ // Returns the new offset after writing the bytes.
5988func (c * EncoderCommon ) SetByte4Uint64 (value uint64 , offset int , d * []byte ) int {
6089 (* d )[offset ] = byte (value >> 24 )
6190 (* d )[offset + 1 ] = byte (value >> 16 )
@@ -64,6 +93,8 @@ func (c *EncoderCommon) SetByte4Uint64(value uint64, offset int, d *[]byte) int
6493 return offset + 4
6594}
6695
96+ // SetByte8Uint64 encodes all eight bytes of the given uint64 value into the byte slice at the specified offset.
97+ // Returns the new offset after writing the bytes.
6798func (c * EncoderCommon ) SetByte8Uint64 (value uint64 , offset int , d * []byte ) int {
6899 (* d )[offset ] = byte (value >> 56 )
69100 (* d )[offset + 1 ] = byte (value >> 48 )
@@ -76,17 +107,23 @@ func (c *EncoderCommon) SetByte8Uint64(value uint64, offset int, d *[]byte) int
76107 return offset + 8
77108}
78109
110+ // SetByte1Int encodes a single byte from the given int value into the byte slice at the specified offset.
111+ // Returns the new offset after writing the byte.
79112func (c * EncoderCommon ) SetByte1Int (code , offset int , d * []byte ) int {
80113 (* d )[offset ] = byte (code )
81114 return offset + 1
82115}
83116
117+ // SetByte2Int encodes the lower two bytes of the given int value into the byte slice at the specified offset.
118+ // Returns the new offset after writing the bytes.
84119func (c * EncoderCommon ) SetByte2Int (value int , offset int , d * []byte ) int {
85120 (* d )[offset ] = byte (value >> 8 )
86121 (* d )[offset + 1 ] = byte (value )
87122 return offset + 2
88123}
89124
125+ // SetByte4Int encodes the lower four bytes of the given int value into the byte slice at the specified offset.
126+ // Returns the new offset after writing the bytes.
90127func (c * EncoderCommon ) SetByte4Int (value int , offset int , d * []byte ) int {
91128 (* d )[offset ] = byte (value >> 24 )
92129 (* d )[offset + 1 ] = byte (value >> 16 )
@@ -95,6 +132,8 @@ func (c *EncoderCommon) SetByte4Int(value int, offset int, d *[]byte) int {
95132 return offset + 4
96133}
97134
135+ // SetByte4Uint32 encodes the lower four bytes of the given uint32 value into the byte slice at the specified offset.
136+ // Returns the new offset after writing the bytes.
98137func (c * EncoderCommon ) SetByte4Uint32 (value uint32 , offset int , d * []byte ) int {
99138 (* d )[offset ] = byte (value >> 24 )
100139 (* d )[offset + 1 ] = byte (value >> 16 )
@@ -103,6 +142,8 @@ func (c *EncoderCommon) SetByte4Uint32(value uint32, offset int, d *[]byte) int
103142 return offset + 4
104143}
105144
145+ // SetBytes writes the given byte slice `bs` into the target byte slice at the specified offset.
146+ // Returns the new offset after writing the bytes.
106147func (c * EncoderCommon ) SetBytes (bs []byte , offset int , d * []byte ) int {
107148 for i := range bs {
108149 (* d )[offset + i ] = bs [i ]
0 commit comments