Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit e9a3b9a

Browse files
committed
treat arrays as fixed length
1 parent 78712d4 commit e9a3b9a

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

codec_test.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,11 @@ func TestBasicSlice(t *testing.T) {
149149
b: hexMustDecode("05000000 11 22 33 44 55"),
150150
name: "byte slice",
151151
},
152-
{
153-
v: [6]byte{0x11, 0x22, 0x33, 0x44, 0x55},
154-
b: hexMustDecode("06000000 11 22 33 44 55 00"),
155-
name: "byte array",
156-
},
157152
{
158153
v: []uint16{0x11, 0x22},
159154
b: hexMustDecode("02000000 1100 2200"),
160155
name: "uint16 slice",
161156
},
162-
{
163-
v: [3]uint16{0x11, 0x22},
164-
b: hexMustDecode("03000000 1100 2200 0000"),
165-
name: "uint16 array",
166-
},
167157
{
168158
v: "ሰማይ አይታረስ ንጉሥ አይከሰስ።",
169159
b: hexMustDecode("36000000E188B0E1889BE18BAD20E18AA0E18BADE189B3E188A8E188B520E18A95E18C89E188A520E18AA0E18BADE18AA8E188B0E188B5E18DA2"),
@@ -296,6 +286,21 @@ func TestStructWithFixedLenMember(t *testing.T) {
296286
})
297287
}
298288

289+
func TestArray(t *testing.T) {
290+
runTest(t, []*testCase{
291+
{
292+
v: [4]byte{0x11, 0x22, 0x33, 0x44},
293+
b: hexMustDecode("11223344"),
294+
name: "byte array",
295+
},
296+
{
297+
v: [2]uint32{0x11, 0x22},
298+
b: hexMustDecode("11000000 22000000"),
299+
name: "uint32 array",
300+
},
301+
})
302+
}
303+
299304
func TestRecursiveStruct(t *testing.T) {
300305
type StructTag struct {
301306
Address []byte

decode.go

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ func (d *Decoder) decodeArray(rv reflect.Value, enumVariants map[int32]reflect.T
170170
if !rv.CanSet() {
171171
return errors.New("array cannot set")
172172
}
173+
if rv.Kind() == reflect.Array {
174+
fixedLen = rv.Len()
175+
}
173176
if rv.Type().Elem() == reflect.TypeOf(byte(0)) {
174177
var b []byte
175178
if b, err = d.decodeByteSlice(fixedLen); err != nil {

encode.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func (e *Encoder) encode(rv reflect.Value, enumVariants map[reflect.Type]int32,
5858
}
5959

6060
func (e *Encoder) encodeSlice(rv reflect.Value, enumVariants map[reflect.Type]int32, fixedLen int) (err error) {
61-
if fixedLen == 0 {
61+
if rv.Kind() == reflect.Array {
62+
// ignore fixedLen
63+
} else if fixedLen == 0 {
6264
if err = binary.Write(e.w, binary.LittleEndian, uint32(rv.Len())); err != nil {
6365
return err
6466
}

0 commit comments

Comments
 (0)