From 1627683be442ebd97db0b9e07d9bd46c148aa568 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Wed, 31 May 2023 13:07:22 +0200 Subject: [PATCH] Support "allownil" for byte slices Since byte slices are considered a base element and not a slice in terms of parsing we must handle it separately. Tested with "AByteSlice" in `_generate/allownil.go`: ``` // string "abyteslice" o = append(o, 0xaa, 0x61, 0x62, 0x79, 0x74, 0x65, 0x73, 0x6c, 0x69, 0x63, 0x65) if z.AByteSlice == nil { // allownil: if nil o = msgp.AppendNil(o) } else { o = msgp.AppendBytes(o, z.AByteSlice) } ``` ``` case "abyteslice": if msgp.IsNil(bts) { bts = bts[1:] z.AByteSlice = nil } else { z.AByteSlice, bts, err = msgp.ReadBytesBytes(bts, z.AByteSlice) if err != nil { err = msgp.WrapError(err, "AByteSlice") return } } ``` --- gen/elem.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gen/elem.go b/gen/elem.go index 4162feba..ef2c3fb9 100644 --- a/gen/elem.go +++ b/gen/elem.go @@ -530,6 +530,8 @@ func (s *BaseElem) Alias(typ string) { } } +func (s *BaseElem) AllowNil() bool { return s.Value == Bytes } + func (s *BaseElem) SetVarname(a string) { // extensions whose parents // are not pointers need to