Skip to content

Commit

Permalink
fix tests, use map of interface instead
Browse files Browse the repository at this point in the history
  • Loading branch information
s.kamardin committed Nov 19, 2016
2 parents 66e724c + 9b93c50 commit 8eedca8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
1 change: 0 additions & 1 deletion gen/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (u *unmarshalGen) nextTypeAndCheck(name string) {
return
}
u.p.printf("\n%s = msgp.NextType(bts)", name)
u.p.print(errcheck)
}

func (u *unmarshalGen) skipAndCheck() {
Expand Down
7 changes: 3 additions & 4 deletions msgp/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,6 @@ func (m *Reader) ReadMapIntfIntf(mp map[interface{}]interface{}) (err error) {
for key := range mp {
delete(mp, key)
}

for i := uint32(0); i < sz; i++ {
var key interface{}
var val interface{}
Expand Down Expand Up @@ -1139,7 +1138,7 @@ func (m *Reader) ReadTime() (t time.Time, err error) {

// ReadIntf reads out the next object as a raw interface{}.
// Arrays are decoded as []interface{}, and maps are decoded
// as map[string]interface{}. Integers are decoded as int64
// as map[interface{}]interface{}. Integers are decoded as int64
// and unsigned integers are decoded as uint64.
func (m *Reader) ReadIntf() (i interface{}, err error) {
var t Type
Expand Down Expand Up @@ -1200,8 +1199,8 @@ func (m *Reader) ReadIntf() (i interface{}, err error) {
return

case MapType:
mp := make(map[string]interface{})
err = m.ReadMapStrIntf(mp)
mp := make(map[interface{}]interface{})
err = m.ReadMapIntfIntf(mp)
i = mp
return

Expand Down
27 changes: 25 additions & 2 deletions msgp/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package msgp

import (
"bytes"
"fmt"
"io"
"math"
"math/rand"
Expand Down Expand Up @@ -31,11 +32,15 @@ func TestReadIntf(t *testing.T) {
time.Now(),
"hello!",
[]byte("hello!"),
map[string]interface{}{
map[interface{}]interface{}{
"thing-1": "thing-1-value",
"thing-2": int64(800),
"thing-3": []byte("some inner bytes..."),
"thing-4": false,
int64(1): "thing-1-value",
int64(2): int64(800),
int64(3): []byte("some inner bytes..."),
int64(4): false,
},
}

Expand All @@ -60,12 +65,30 @@ func TestReadIntf(t *testing.T) {
t.Errorf("Test case: %d: %s", i, err)
}
if !reflect.DeepEqual(v, ts) {
t.Errorf("%v in; %v out", ts, v)
// if v and ts are maps
if m, ok := v.(map[interface{}]interface{}); ok {
t.Errorf(
"\n%s\n%s\n",
dumpMap("v", m),
dumpMap("ts", ts.(map[interface{}]interface{})),
)
} else {
t.Errorf("in: %#v; out: %#v", ts, v)
}
}
}

}

func dumpMap(label string, m map[interface{}]interface{}) string {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "map %q contents:\n", label)
for k, v := range m {
fmt.Fprintf(buf, "%#v (%t) -> %#v (%t)\n", k, k, v, v)
}
return buf.String()
}

func TestReadMapHeader(t *testing.T) {
tests := []struct {
Sz uint32
Expand Down
23 changes: 22 additions & 1 deletion msgp/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func (mw *Writer) WriteMapStrStr(mp map[string]string) (err error) {
return nil
}

// WriteMapStrIntf writes a map[string]interface to the writer
// WriteMapStrIntf writes a map[string]interface{} to the writer.
func (mw *Writer) WriteMapStrIntf(mp map[string]interface{}) (err error) {
err = mw.WriteMapHeader(uint32(len(mp)))
if err != nil {
Expand All @@ -577,6 +577,25 @@ func (mw *Writer) WriteMapStrIntf(mp map[string]interface{}) (err error) {
return
}

// WriteMapIntfIntf writes a map[interface{}]interface{} to the writer.
func (mw *Writer) WriteMapIntfIntf(mp map[interface{}]interface{}) (err error) {
err = mw.WriteMapHeader(uint32(len(mp)))
if err != nil {
return
}
for key, val := range mp {
err = mw.WriteIntf(key)
if err != nil {
return
}
err = mw.WriteIntf(val)
if err != nil {
return
}
}
return
}

// WriteTime writes a time.Time object to the wire.
//
// Time is encoded as Unix time, which means that
Expand Down Expand Up @@ -665,6 +684,8 @@ func (mw *Writer) WriteIntf(v interface{}) error {
return mw.WriteMapStrStr(v)
case map[string]interface{}:
return mw.WriteMapStrIntf(v)
case map[interface{}]interface{}:
return mw.WriteMapIntfIntf(v)
case time.Time:
return mw.WriteTime(v)
}
Expand Down

0 comments on commit 8eedca8

Please sign in to comment.