From 4c452221aa96bb9e9b362e8a161430e457d2bac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20FRANC?= Date: Tue, 26 Jul 2022 14:59:32 +0200 Subject: [PATCH] gen: fix decoding for non-[]byte types when type-conversion needed For example for the annotation below: ``` //msgp:shim uuid.UUID as:[]byte using:uuidToBytes/bytesToUUID ``` When an UUID is an array of bytes (and not a slice of bytes). We need to use `uuidToBytes()` call and not hardcoded `[]byte()` cast from the generated func `DecodeMsg(dc *msgp.Reader) error` Ex: ``` func (z *Foobar) DecodeMsg(dc *msgp.Reader) (err error) { [...] case "Id": { var zb0002 []byte - zb0002, err = dc.ReadBytes([]byte(z.Id)) + zb0002, err = dc.ReadBytes(uuidToBytes(z.Id)) ``` --- gen/decode.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gen/decode.go b/gen/decode.go index aa845a1f..efa67899 100644 --- a/gen/decode.go +++ b/gen/decode.go @@ -155,7 +155,8 @@ func (d *decodeGen) gBase(b *BaseElem) { switch b.Value { case Bytes: if b.Convert { - d.p.printf("\n%s, err = dc.ReadBytes([]byte(%s))", tmp, vname) + lowered := b.ToBase() + "(" + vname + ")" + d.p.printf("\n%s, err = dc.ReadBytes(%s)", tmp, lowered) } else { d.p.printf("\n%s, err = dc.ReadBytes(%s)", vname, vname) }