Commit e3b835a1 authored by Jonathan Pulfer's avatar Jonathan Pulfer

Merge upstream into my fork

parents bacb62b4 da8d1520
...@@ -114,7 +114,9 @@ func (e *Encoder) encodeBytes(byt []byte) { ...@@ -114,7 +114,9 @@ func (e *Encoder) encodeBytes(byt []byte) {
e.w.Write([]byte{opShortBinstring, byte(l)}) e.w.Write([]byte{opShortBinstring, byte(l)})
} else { } else {
e.w.Write([]byte{opBinstring}) e.w.Write([]byte{opBinstring})
binary.Write(e.w, binary.LittleEndian, int32(l)) var b [4]byte
binary.LittleEndian.PutUint32(b[:], uint32(l))
e.w.Write(b[:])
} }
e.w.Write(byt) e.w.Write(byt)
...@@ -124,7 +126,9 @@ func (e *Encoder) encodeFloat(f float64) { ...@@ -124,7 +126,9 @@ func (e *Encoder) encodeFloat(f float64) {
var u uint64 var u uint64
u = math.Float64bits(f) u = math.Float64bits(f)
e.w.Write([]byte{opBinfloat}) e.w.Write([]byte{opBinfloat})
binary.Write(e.w, binary.BigEndian, u) var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(u))
e.w.Write(b[:])
} }
func (e *Encoder) encodeInt(k reflect.Kind, i int64) { func (e *Encoder) encodeInt(k reflect.Kind, i int64) {
...@@ -138,31 +142,15 @@ func (e *Encoder) encodeInt(k reflect.Kind, i int64) { ...@@ -138,31 +142,15 @@ func (e *Encoder) encodeInt(k reflect.Kind, i int64) {
e.w.Write([]byte{opBinint2, byte(i), byte(i >> 8)}) e.w.Write([]byte{opBinint2, byte(i), byte(i >> 8)})
case i >= math.MinInt32 && i <= math.MaxInt32: case i >= math.MinInt32 && i <= math.MaxInt32:
e.w.Write([]byte{opBinint}) e.w.Write([]byte{opBinint})
binary.Write(e.w, binary.LittleEndian, int32(i)) var b [4]byte
binary.LittleEndian.PutUint32(b[:], uint32(i))
e.w.Write(b[:])
default: // int64, but as a string :/ default: // int64, but as a string :/
e.w.Write([]byte{opInt}) e.w.Write([]byte{opInt})
fmt.Fprintf(e.w, "%d\n", i) fmt.Fprintf(e.w, "%d\n", i)
} }
} }
func (e *Encoder) encodeScalar(rv reflect.Value) {
switch rv.Kind() {
case reflect.Array, reflect.Slice:
if rv.Type().Elem().Kind() == reflect.Uint8 {
e.encodeBytes(rv.Bytes())
} else {
e.encodeArray(rv)
}
case reflect.Map:
e.encodeMap(rv)
case reflect.Interface:
e.encodeScalar(rv.Elem())
default:
e.encode(rv)
}
}
func (e *Encoder) encodeMap(m reflect.Value) { func (e *Encoder) encodeMap(m reflect.Value) {
keys := m.MapKeys() keys := m.MapKeys()
......
...@@ -16,6 +16,10 @@ func TestEncode(t *testing.T) { ...@@ -16,6 +16,10 @@ func TestEncode(t *testing.T) {
"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"}},
}, },
{
"small types",
[]interface{}{int64(0), int64(1), int64(258), int64(65537), false, true},
},
} }
for _, tt := range tests { for _, tt := range tests {
......
...@@ -315,8 +315,12 @@ func (d *Decoder) loadInt() error { ...@@ -315,8 +315,12 @@ func (d *Decoder) loadInt() error {
// Push a four-byte signed int // Push a four-byte signed int
func (d *Decoder) loadBinInt() error { func (d *Decoder) loadBinInt() error {
var v int32 var b [4]byte
binary.Read(d.r, binary.LittleEndian, &v) _, err := io.ReadFull(d.r, b[:])
if err != nil {
return err
}
v := binary.LittleEndian.Uint32(b[:])
d.push(int64(v)) d.push(int64(v))
return nil return nil
} }
...@@ -365,12 +369,13 @@ func (d *Decoder) loadLong1() error { ...@@ -365,12 +369,13 @@ func (d *Decoder) loadLong1() error {
// Push a 2-byte unsigned int // Push a 2-byte unsigned int
func (d *Decoder) loadBinInt2() error { func (d *Decoder) loadBinInt2() error {
var u uint16 var b [2]byte
err := binary.Read(d.r, binary.LittleEndian, &u) _, err := io.ReadFull(d.r, b[:])
if err != nil { if err != nil {
return err return err
} }
d.push(int64(u)) v := binary.LittleEndian.Uint16(b[:])
d.push(int64(v))
return nil return nil
} }
...@@ -425,11 +430,12 @@ func (d *Decoder) loadString() error { ...@@ -425,11 +430,12 @@ func (d *Decoder) loadString() error {
} }
func (d *Decoder) loadBinString() error { func (d *Decoder) loadBinString() error {
var v int32 var b [4]byte
err := binary.Read(d.r, binary.LittleEndian, &v) _, err := io.ReadFull(d.r, b[:])
if err != nil { if err != nil {
return err return err
} }
v := binary.LittleEndian.Uint32(b[:])
s := make([]byte, v) s := make([]byte, v)
_, err = io.ReadFull(d.r, s) _, err = io.ReadFull(d.r, s)
if err != nil { if err != nil {
...@@ -574,8 +580,12 @@ func (d *Decoder) inst() error { ...@@ -574,8 +580,12 @@ func (d *Decoder) inst() error {
} }
func (d *Decoder) longBinGet() error { func (d *Decoder) longBinGet() error {
var v int32 var b [4]byte
binary.Read(d.r, binary.LittleEndian, &v) _, err := io.ReadFull(d.r, b[:])
if err != nil {
return err
}
v := binary.LittleEndian.Uint32(b[:])
d.push(d.memo[strconv.Itoa(int(v))]) d.push(d.memo[strconv.Itoa(int(v))])
return nil return nil
} }
...@@ -652,8 +662,12 @@ func (d *Decoder) loadSetItems() error { ...@@ -652,8 +662,12 @@ func (d *Decoder) loadSetItems() error {
} }
func (d *Decoder) binFloat() error { func (d *Decoder) binFloat() error {
var u uint64 var b [8]byte
binary.Read(d.r, binary.BigEndian, &u) _, err := io.ReadFull(d.r, b[:])
if err != nil {
return err
}
u := binary.BigEndian.Uint64(b[:])
d.stack = append(d.stack, math.Float64frombits(u)) d.stack = append(d.stack, math.Float64frombits(u))
return nil return nil
} }
......
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