Added Marshaler

This commit is contained in:
tidwall 2020-10-30 06:53:21 -07:00
parent 2797057b75
commit 51082103ca
1 changed files with 10 additions and 1 deletions

View File

@ -355,6 +355,13 @@ type SimpleString string
// from an *Any call. // from an *Any call.
type SimpleInt int type SimpleInt int
// Marshaler is the interface implemented by types that
// can marshal themselves into a Redis response type from an *Any call.
// The return value is not check for validity.
type Marshaler interface {
MarshalRESP() []byte
}
// AppendAny appends any type to valid Redis type. // AppendAny appends any type to valid Redis type.
// nil -> null // nil -> null
// error -> error (adds "ERR " when first word is not uppercase) // error -> error (adds "ERR " when first word is not uppercase)
@ -366,6 +373,7 @@ type SimpleInt int
// map -> array with key/value pairs // map -> array with key/value pairs
// SimpleString -> string // SimpleString -> string
// SimpleInt -> integer // SimpleInt -> integer
// Marshaler -> raw bytes
// everything-else -> bulk-string representation using fmt.Sprint() // everything-else -> bulk-string representation using fmt.Sprint()
func AppendAny(b []byte, v interface{}) []byte { func AppendAny(b []byte, v interface{}) []byte {
switch v := v.(type) { switch v := v.(type) {
@ -377,7 +385,6 @@ func AppendAny(b []byte, v interface{}) []byte {
b = AppendNull(b) b = AppendNull(b)
case error: case error:
b = AppendError(b, prefixERRIfNeeded(v.Error())) b = AppendError(b, prefixERRIfNeeded(v.Error()))
case string: case string:
b = AppendBulkString(b, v) b = AppendBulkString(b, v)
case []byte: case []byte:
@ -412,6 +419,8 @@ func AppendAny(b []byte, v interface{}) []byte {
b = AppendBulkFloat(b, float64(v)) b = AppendBulkFloat(b, float64(v))
case float64: case float64:
b = AppendBulkFloat(b, float64(v)) b = AppendBulkFloat(b, float64(v))
case Marshaler:
b = append(b, v.MarshalRESP()...)
default: default:
vv := reflect.ValueOf(v) vv := reflect.ValueOf(v)
switch vv.Kind() { switch vv.Kind() {