Encode byte array to CBOR as array of integer, not byte string.

This is structurally compatible with the JSON behavior.
This commit is contained in:
Ben Luddy 2024-06-10 11:30:03 -04:00
parent 921fb0b7c5
commit 037ba12551
No known key found for this signature in database
GPG Key ID: A6551E73A5974C30
3 changed files with 14 additions and 0 deletions

View File

@ -75,6 +75,10 @@ var Encode cbor.EncMode = func() cbor.EncMode {
// Encode struct field names to the byte string type rather than the text string
// type.
FieldName: cbor.FieldNameToByteString,
// Marshal Go byte arrays to CBOR arrays of integers (as in JSON) instead of byte
// strings.
ByteArray: cbor.ByteArrayToArray,
}.EncMode()
if err != nil {
panic(err)

View File

@ -64,6 +64,12 @@ func TestEncode(t *testing.T) {
}
}),
},
{
name: "byte array encodes to array of integers",
in: [3]byte{0x01, 0x02, 0x03},
want: []byte{0x83, 0x01, 0x02, 0x03}, // [1, 2, 3]
assertOnError: assertNilError,
},
} {
encModes := tc.modes
if len(encModes) == 0 {

View File

@ -47,6 +47,10 @@ func TestRoundtrip(t *testing.T) {
name: "nil slice",
obj: []interface{}(nil),
},
{
name: "byte array",
obj: [3]byte{0x01, 0x02, 0x03},
},
{
name: "nil map",
obj: map[string]interface{}(nil),