ugorji / go

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

codecgen's decoder doesn't respect Selfers of type synonyms #142

Closed 2opremio closed 8 years ago

2opremio commented 8 years ago
type Foo struct {
        Columns []Column
}

type Column string

func (c *Column) CodecEncodeSelf(encoder *codec.Encoder) {
        in := map[string]string{"id": string(*c), "label": string(*c)}
        encoder.Encode(in)
}

func (c *Column) CodecDecodeSelf(decoder *codec.Decoder) {
        m := map[string]string{}
        decoder.Decode(&m)
        *c = Column(m["id"])
}

The decoder generated by codecgen for Foo assumes that Columns is a a slice of strings without considering its CodecDecodeSelf method.

Here's a self-contained repro:

wget http://filebin.ca/2XDJ0v4zrFGq/test.tgz 
tar -xvzf test.tgz -C $GOPATH/src/
cd $GOPATH/src/test/
go get github.com/ugorji/go/codec/codecgen
(cd foo; codecgen -o foo.codecgen.go foo.go)
go build .
./test
Decoding failed [pos 13]: json: expect char '"' but got char '{'
ugorji commented 8 years ago

Good catch.

Fix coming shortly.

ugorji commented 8 years ago

@2opremio Fixed: See 9f21f8ea305046b8d17a799e2c73069533a99d24 (unsafe string-to-bytes conversion) and 5d64d76d3ac4d5450009b0f5fa1530b790fccc71 (skip creating Selfer if already explicitly done).

Both of these changes should carry you over. Please test again and let me know.

2opremio commented 8 years ago

I will check tomorrow. Thanks for the fixes and the wonderful support!