From 5c929077b7736e77ec0ddaca3d92732c49af2eac Mon Sep 17 00:00:00 2001 From: addshore Date: Thu, 6 Jun 2024 21:43:15 +0100 Subject: [PATCH] Add a test for slice of struct in struct This actually shows that https://github.com/mixcode/binarystruct/issues/2 is already possible, just not really detailed in the docs how to achieve it, at least there will now be an example in tests :) Fixes #2 --- marshal_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/marshal_test.go b/marshal_test.go index ba2472c..a1ef2a7 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -556,6 +556,32 @@ func TestStruct(test *testing.T) { decodeCompareLE(exp, &out, &in) }() + // slice of struct in a struct + func() { + type st2 struct { + I2 int16 + B2 byte `binary:"int8"` + } + type st1 struct { + I1 int16 + B1 byte `binary:"int8"` + ST2 []st2 `binary:"[I1]struct"` + } + + in := st1{ + 2, + 1, + []st2{ + {2, 4}, + {3, 5}, + }, + } + exp := []byte{0x02, 0x00, 0x01, 0x02, 0x00, 0x04, 0x03, 0x00, 0x05} + encodeCompareLE(in, exp) + out := st1{} + decodeCompareLE(exp, &out, &in) + }() + // some complex structure func() { type t struct {