From a3a843759b4ed6db41ac8b2c316bc27938b0e11d Mon Sep 17 00:00:00 2001
From: Matt Bell <mappum@gmail.com>
Date: Fri, 10 Oct 2014 13:33:47 -0700
Subject: [PATCH] commands: Added marshalling to Response

---
 commands/response.go | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/commands/response.go b/commands/response.go
index a538814c5..85ce76fee 100644
--- a/commands/response.go
+++ b/commands/response.go
@@ -1,5 +1,12 @@
 package commands
 
+import (
+  "fmt"
+  "strings"
+  "encoding/json"
+  "encoding/xml"
+)
+
 type ErrorType uint
 const (
   Normal ErrorType = iota // general errors
@@ -13,6 +20,19 @@ type Error struct {
   code ErrorType
 }
 
+type EncodingType string
+const (
+  Json = "json"
+  Xml = "xml"
+  // TODO: support more encoding types
+)
+
+type Marshaller func(v interface{})([]byte, error)
+var marshallers = map[EncodingType]Marshaller{
+  Json: json.Marshal,
+  Xml: xml.Marshal,
+}
+
 type Response struct {
   req *Request
   Error error
@@ -29,6 +49,17 @@ func (r *Response) FormatError() Error {
   return Error{ r.Error.Error(), r.ErrorType }
 }
 
-/*func (r *Response) Encode() ([]byte, error) {
+func (r *Response) Marshal() ([]byte, error) {
+  enc := r.req.Option("enc")
+  if enc == nil {
+    return nil, fmt.Errorf("No encoding type was specified")
+  }
+  encType := EncodingType(strings.ToLower(enc.(string)))
 
-}*/
+  marshaller, ok := marshallers[encType]
+  if !ok {
+    return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
+  }
+
+  return marshaller(r.Value)
+}
-- 
GitLab