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

Commit

Permalink
treat arrays as fixed length
Browse files Browse the repository at this point in the history
  • Loading branch information
the729 committed Dec 15, 2019
1 parent 78712d4 commit e9a3b9a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
25 changes: 15 additions & 10 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e9a3b9a

Please sign in to comment.