Esempio n. 1
0
func FormatStringSlice(val []string) []byte {
	b := bytes.Buffer{}
	b.WriteByte('*')
	b.WriteString(util.Itoa(len(val)))
	b.WriteString("\r\n")
	for _, v := range val {
		b.WriteByte('$')
		b.WriteString(util.Itoa(len(v)))
		b.WriteString("\r\n")
		b.WriteString(v)
		b.WriteString("\r\n")
	}
	return b.Bytes()
}
Esempio n. 2
0
func FormatSlice(val []interface{}) []byte {
	b := bytes.Buffer{}
	b.WriteByte('*')
	b.WriteString(util.Itoa(len(val)))
	b.WriteString("\r\n")
	for _, v := range val {
		if v == nil {
			b.WriteString("$-1\r\n")
			continue
		}
		switch v.(type) {
		case int:
			d := formatInt(int64(v.(int)))
			b.WriteByte('$')
			b.WriteString(util.Itoa(len(d)))
			b.WriteString("\r\n")
			b.WriteString(d)
			b.WriteString("\r\n")
		case int64:
			d := formatInt(v.(int64))
			b.WriteByte('$')
			b.WriteString(util.Itoa(len(d)))
			b.WriteString("\r\n")
			b.WriteString(d)
			b.WriteString("\r\n")
		case string:
			d := v.(string)
			b.WriteByte('$')
			b.WriteString(util.Itoa(len(d)))
			b.WriteString("\r\n")
			b.WriteString(d)
			b.WriteString("\r\n")
		case float64:
			d := formatFloat(v.(float64))
			b.WriteByte('$')
			b.WriteString(util.Itoa(len(d)))
			b.WriteString("\r\n")
			b.WriteString(d)
			b.WriteString("\r\n")
		default:
			log.Warningf("got %T , expected string or int or float ", v)
			d := fmt.Sprintf("-%s\r\n", TypeAssertedErr.Error())
			return []byte(d)
		}
	}
	return b.Bytes()

}
Esempio n. 3
0
func FormatString(val string) []byte {
	b := bytes.Buffer{}
	b.WriteByte('$')
	b.WriteString(util.Itoa(len(val)))
	b.WriteString("\r\n")
	b.WriteString(val)
	b.WriteString("\r\n")
	return b.Bytes()
}
Esempio n. 4
0
func FormatFloat(val float64) []byte {
	b := bytes.Buffer{}
	b.WriteByte('$')
	d := formatFloat(val)
	b.WriteString(util.Itoa(len(d)))
	b.WriteString("\r\n")
	b.WriteString(d)
	b.WriteString("\r\n")
	return b.Bytes()
}