Skip to content

Commit

Permalink
gen: fix decoding for non-[]byte types when type-conversion needed
Browse files Browse the repository at this point in the history
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))
```
  • Loading branch information
pilebones authored and philhofer committed Oct 24, 2023
1 parent 6ac204f commit 4c45222
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gen/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4c45222

Please sign in to comment.