Skip to content

Commit

Permalink
Support "allownil" for byte slices
Browse files Browse the repository at this point in the history
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
				}
			}
```
  • Loading branch information
klauspost authored and philhofer committed May 31, 2023
1 parent 8a5e756 commit 1627683
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1627683

Please sign in to comment.