Commit 2dfb2bb8 authored by Kamil Kisiel's avatar Kamil Kisiel

Merge pull request #15 from dgryski/encode-struct-extra-dict

Fix struct encoding
parents 56b35bc5 0ade5ddc
...@@ -185,10 +185,6 @@ func (e *Encoder) encodeStruct(st reflect.Value) { ...@@ -185,10 +185,6 @@ func (e *Encoder) encodeStruct(st reflect.Value) {
return return
} }
e.w.Write([]byte{opEmptyDict})
l := typ.NumField()
structTags := getStructTags(st) structTags := getStructTags(st)
e.w.Write([]byte{opEmptyDict, opMark}) e.w.Write([]byte{opEmptyDict, opMark})
...@@ -199,6 +195,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) { ...@@ -199,6 +195,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) {
e.encode(st.Field(i)) e.encode(st.Field(i))
} }
} else { } else {
l := typ.NumField()
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
fty := typ.Field(i) fty := typ.Field(i)
if fty.PkgPath != "" { if fty.PkgPath != "" {
......
...@@ -8,17 +8,30 @@ import ( ...@@ -8,17 +8,30 @@ import (
func TestEncode(t *testing.T) { func TestEncode(t *testing.T) {
type foo struct {
Foo string
Bar int32
}
tests := []struct { tests := []struct {
name string name string
input interface{} input interface{}
output interface{}
}{ }{
{ {
"graphite message", "graphite message",
[]interface{}{map[interface{}]interface{}{"values": []interface{}{float64(473), float64(497), float64(540), float64(1497), float64(1808), float64(1890), float64(2013), float64(1821), float64(1847), float64(2176), float64(2156), float64(1250), float64(2055), float64(1570), None{}, None{}}, "start": int64(1383782400), "step": int64(86400), "end": int64(1385164800), "name": "ZZZZ.UUUUUUUU.CCCCCCCC.MMMMMMMM.XXXXXXXXX.TTT"}}, []interface{}{map[interface{}]interface{}{"values": []interface{}{float64(473), float64(497), float64(540), float64(1497), float64(1808), float64(1890), float64(2013), float64(1821), float64(1847), float64(2176), float64(2156), float64(1250), float64(2055), float64(1570), None{}, None{}}, "start": int64(1383782400), "step": int64(86400), "end": int64(1385164800), "name": "ZZZZ.UUUUUUUU.CCCCCCCC.MMMMMMMM.XXXXXXXXX.TTT"}},
nil,
}, },
{ {
"small types", "small types",
[]interface{}{int64(0), int64(1), int64(258), int64(65537), false, true}, []interface{}{int64(0), int64(1), int64(258), int64(65537), false, true},
nil,
},
{
"array of struct types",
[]foo{{"Qux", 4}},
[]interface{}{map[interface{}]interface{}{"Foo": "Qux", "Bar": int64(4)}},
}, },
} }
...@@ -30,8 +43,13 @@ func TestEncode(t *testing.T) { ...@@ -30,8 +43,13 @@ func TestEncode(t *testing.T) {
d := NewDecoder(bytes.NewReader(p.Bytes())) d := NewDecoder(bytes.NewReader(p.Bytes()))
output, _ := d.Decode() output, _ := d.Decode()
if !reflect.DeepEqual(tt.input, output) { want := tt.output
t.Errorf("%s: got\n%q\n expected\n%q", tt.name, output, tt.input) if want == nil {
want = tt.input
}
if !reflect.DeepEqual(want, output) {
t.Errorf("%s: got\n%q\n expected\n%q", tt.name, output, want)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment