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

Arrays cause encoding failure. #21

Closed
aarondl opened this issue Nov 14, 2013 · 1 comment
Closed

Arrays cause encoding failure. #21

aarondl opened this issue Nov 14, 2013 · 1 comment
Labels

Comments

@aarondl
Copy link

aarondl commented Nov 14, 2013

It appears that arrays are not able to be serialized.

package main

import (
    "github.com/ugorji/go/codec"
    "bytes"
    "log"
)

type bstruct struct {
    B [3]byte
}

func main() {
    b := bstruct{
        [3]byte{5, 7, 8},
    }

    log.Println(b)

    var m codec.MsgpackHandle

    var buf bytes.Buffer
    encoder := codec.NewEncoder(&buf, &m)
    if err := encoder.Encode(b); err != nil {
        log.Fatal("Failed to encode:", err)
    }
}

This snippet produces:
2013/11/14 09:01:41 Failed to encode:reflect.Value.Slice: slice of unaddressable array

Are arrays not able to be serialized?

@ugorji
Copy link
Owner

ugorji commented Nov 14, 2013

This one is kinda messed up.

The short answer is that I can support it, but it will involve duplicating code. Which is clearly a better option that not supporting.

The long answer:

  • Not all arrays are addressable.
  • An array which is a field in a non-addressable value is not addressable.

Consequently, in your example above, encoder.Encode(&b) will work, but encoder.Encode(b) will not.

The error happened because I wanted to eliminate code duplication, so when it was time to encode an array, I converted it to a slice and sent it to the kSlice method.

Clearly this is not robust, so I'd do the duplication and try to reduce code size in both.

Fix coming soon.

I don't have tests for non-addressable arrays (unfortunately). I'd add one.

@ugorji ugorji closed this as completed in eb2067e Nov 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants