diff --git a/codec_test.go b/codec_test.go index 9b08db9..9712bcb 100644 --- a/codec_test.go +++ b/codec_test.go @@ -149,21 +149,11 @@ func TestBasicSlice(t *testing.T) { b: hexMustDecode("05000000 11 22 33 44 55"), name: "byte slice", }, - { - v: [6]byte{0x11, 0x22, 0x33, 0x44, 0x55}, - b: hexMustDecode("06000000 11 22 33 44 55 00"), - name: "byte array", - }, { v: []uint16{0x11, 0x22}, b: hexMustDecode("02000000 1100 2200"), name: "uint16 slice", }, - { - v: [3]uint16{0x11, 0x22}, - b: hexMustDecode("03000000 1100 2200 0000"), - name: "uint16 array", - }, { v: "ሰማይ አይታረስ ንጉሥ አይከሰስ።", b: hexMustDecode("36000000E188B0E1889BE18BAD20E18AA0E18BADE189B3E188A8E188B520E18A95E18C89E188A520E18AA0E18BADE18AA8E188B0E188B5E18DA2"), @@ -296,6 +286,21 @@ func TestStructWithFixedLenMember(t *testing.T) { }) } +func TestArray(t *testing.T) { + runTest(t, []*testCase{ + { + v: [4]byte{0x11, 0x22, 0x33, 0x44}, + b: hexMustDecode("11223344"), + name: "byte array", + }, + { + v: [2]uint32{0x11, 0x22}, + b: hexMustDecode("11000000 22000000"), + name: "uint32 array", + }, + }) +} + func TestRecursiveStruct(t *testing.T) { type StructTag struct { Address []byte diff --git a/decode.go b/decode.go index ec917d6..22b12a8 100644 --- a/decode.go +++ b/decode.go @@ -170,6 +170,9 @@ func (d *Decoder) decodeArray(rv reflect.Value, enumVariants map[int32]reflect.T if !rv.CanSet() { return errors.New("array cannot set") } + if rv.Kind() == reflect.Array { + fixedLen = rv.Len() + } if rv.Type().Elem() == reflect.TypeOf(byte(0)) { var b []byte if b, err = d.decodeByteSlice(fixedLen); err != nil { diff --git a/encode.go b/encode.go index 4139c25..9e02e7d 100644 --- a/encode.go +++ b/encode.go @@ -58,7 +58,9 @@ func (e *Encoder) encode(rv reflect.Value, enumVariants map[reflect.Type]int32, } func (e *Encoder) encodeSlice(rv reflect.Value, enumVariants map[reflect.Type]int32, fixedLen int) (err error) { - if fixedLen == 0 { + if rv.Kind() == reflect.Array { + // ignore fixedLen + } else if fixedLen == 0 { if err = binary.Write(e.w, binary.LittleEndian, uint32(rv.Len())); err != nil { return err }