From 51082103ca3bb58779c02737e4ddb44f91c5c243 Mon Sep 17 00:00:00 2001 From: tidwall Date: Fri, 30 Oct 2020 06:53:21 -0700 Subject: [PATCH] Added Marshaler --- append.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/append.go b/append.go index 1795a62..a30038a 100644 --- a/append.go +++ b/append.go @@ -355,6 +355,13 @@ type SimpleString string // from an *Any call. 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. // nil -> null // error -> error (adds "ERR " when first word is not uppercase) @@ -366,6 +373,7 @@ type SimpleInt int // map -> array with key/value pairs // SimpleString -> string // SimpleInt -> integer +// Marshaler -> raw bytes // everything-else -> bulk-string representation using fmt.Sprint() func AppendAny(b []byte, v interface{}) []byte { switch v := v.(type) { @@ -377,7 +385,6 @@ func AppendAny(b []byte, v interface{}) []byte { b = AppendNull(b) case error: b = AppendError(b, prefixERRIfNeeded(v.Error())) - case string: b = AppendBulkString(b, v) case []byte: @@ -412,6 +419,8 @@ func AppendAny(b []byte, v interface{}) []byte { b = AppendBulkFloat(b, float64(v)) case float64: b = AppendBulkFloat(b, float64(v)) + case Marshaler: + b = append(b, v.MarshalRESP()...) default: vv := reflect.ValueOf(v) switch vv.Kind() {