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

Embedded pointers and StructToArray creates failure. #20

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

Embedded pointers and StructToArray creates failure. #20

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

Comments

@aarondl
Copy link

aarondl commented Nov 13, 2013

package main

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

type A struct {
    Aint int
}

type B struct {
    *A
    Bint int
}

type C struct {
    *A
    Cint int
}

type D struct {
    Structs map[string]*B
    Array []*C
}

func main() {
    obj := &D{
        Structs: map[string]*B{
            "test": &B{&A{5}, 6},
        },
    }

    var m codec.MsgpackHandle
    m.StructToArray = true

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

    decoder := codec.NewDecoder(&buf, &m)
    d := D{}
    if err := decoder.Decode(&d); err != nil {
        log.Fatal("Failed to decode:", err)
    }

    log.Println(d)
}

This code receives the error:
reflect: call of reflect.Value.Field on ptr Value

It runs correctly when the struct pointers are named and not embedded OR the StructToArray option is turned off.

@ugorji
Copy link
Owner

ugorji commented Nov 13, 2013

I knew I should have slept over it.

Structs can be decoded from an array or a map in the stream.

I did the fix for the map in the stream, but not for the array.

Now fix. Submitting right away.

@ugorji ugorji closed this as completed in bb8ac5b Nov 13, 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