diff --git a/codec_test.go b/codec_test.go index 146250b..9b08db9 100644 --- a/codec_test.go +++ b/codec_test.go @@ -252,6 +252,7 @@ func TestBasicStruct(t *testing.T) { func TestStructWithFixedLenMember(t *testing.T) { type MyStruct struct { + Str string `lcs:"len=2"` Bytes []byte `lcs:"len=4"` OptionalBytes []byte `lcs:"len=4,optional"` } @@ -259,25 +260,37 @@ func TestStructWithFixedLenMember(t *testing.T) { runTest(t, []*testCase{ { v: MyStruct{ + Str: "12", Bytes: []byte{0x11, 0x22}, }, errMarshal: errors.New("actual len not equal to fixed len"), - name: "struct with wrong fixed len", + name: "struct with wrong fixed len (bytes)", skipUnmarshal: true, }, { v: MyStruct{ + Str: "", Bytes: []byte{0x11, 0x22, 0x33, 0x44}, }, - b: hexMustDecode("11223344 00"), + errMarshal: errors.New("actual len not equal to fixed len"), + name: "struct with wrong fixed len (string)", + skipUnmarshal: true, + }, + { + v: MyStruct{ + Str: "12", + Bytes: []byte{0x11, 0x22, 0x33, 0x44}, + }, + b: hexMustDecode("31 32 11223344 00"), name: "struct with fixed len", }, { v: MyStruct{ + Str: "12", Bytes: []byte{0x11, 0x22, 0x33, 0x44}, OptionalBytes: []byte{0x55, 0x66, 0x77, 0x88}, }, - b: hexMustDecode("11223344 01 55667788"), + b: hexMustDecode("3132 11223344 01 55667788"), name: "struct with optional fixed len", }, }) diff --git a/decode.go b/decode.go index 0673e3f..ec917d6 100644 --- a/decode.go +++ b/decode.go @@ -285,7 +285,7 @@ func (d *Decoder) decodeStruct(rv reflect.Value) (err error) { } } fixedLen := 0 - if fixedLenStr, ok := tag["len"]; ok && fv.Kind() == reflect.Slice { + if fixedLenStr, ok := tag["len"]; ok && (fv.Kind() == reflect.Slice || fv.Kind() == reflect.String) { fixedLen, err = strconv.Atoi(fixedLenStr) if err != nil { return errors.New("tag len parse error: " + err.Error()) diff --git a/encode.go b/encode.go index 9cd64c3..4139c25 100644 --- a/encode.go +++ b/encode.go @@ -137,7 +137,7 @@ func (e *Encoder) encodeStruct(rv reflect.Value) (err error) { } } fixedLen := 0 - if fixedLenStr, ok := tag["len"]; ok && fv.Kind() == reflect.Slice { + if fixedLenStr, ok := tag["len"]; ok && (fv.Kind() == reflect.Slice || fv.Kind() == reflect.String) { fixedLen, err = strconv.Atoi(fixedLenStr) if err != nil { return errors.New("tag len parse error: " + err.Error())