Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "allownil" for byte slices #328

Merged
merged 1 commit into from
May 31, 2023

Conversation

klauspost
Copy link
Collaborator

@klauspost klauspost commented May 31, 2023

Since byte slices are considered a base element and not a slice in terms of parsing we must handle it separately.

Already 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
				}
			}

(similar for En/Decode)

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
				}
			}
```
@philhofer philhofer merged commit 1627683 into tinylib:master May 31, 2023
@klauspost klauspost deleted the allow-nil-byte-slices branch May 31, 2023 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants