Exemple #1
0
func (e *EncoderSuite) TestStringEncoding(c *C) {
	buf := &bytes.Buffer{}
	for _, t := range stringEncodingTests {
		e := rdb.NewEncoder(buf)
		e.EncodeType(rdb.TypeString)
		e.EncodeString([]byte(t.str))
		e.EncodeDumpFooter()
		expected, _ := base64.StdEncoding.DecodeString(t.res)
		c.Assert(buf.Bytes(), DeepEquals, expected, Commentf("%s - expected: %x, actual: %x", t.str, expected, buf.Bytes()))
		buf.Reset()
	}
}
Exemple #2
0
func Dump(obj interface{}) ([]byte, error) {
	var buf bytes.Buffer

	e := rdb.NewEncoder(&buf)

	switch v := obj.(type) {
	case String:
		e.EncodeType(rdb.TypeString)
		e.EncodeString(v)
	case Hash:
		e.EncodeType(rdb.TypeHash)
		e.EncodeLength(uint32(len(v)))

		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i].Field)
			e.EncodeString(v[i].Value)
		}
	case List:
		e.EncodeType(rdb.TypeList)
		e.EncodeLength(uint32(len(v)))
		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i])
		}
	case Set:
		e.EncodeType(rdb.TypeSet)
		e.EncodeLength(uint32(len(v)))
		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i])
		}
	case ZSet:
		e.EncodeType(rdb.TypeZSet)
		e.EncodeLength(uint32(len(v)))
		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i].Member)
			e.EncodeFloat(v[i].Score)
		}
	default:
		return nil, fmt.Errorf("invalid dump type %T", obj)
	}

	e.EncodeDumpFooter()

	return buf.Bytes(), nil
}