ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.87k stars 295 forks source link

Embedded pointers and StructToArray creates failure. #20

Closed aarondl closed 11 years ago

aarondl commented 11 years ago
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 commented 11 years ago

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.