Commit a3a84375 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands: Added marshalling to Response

parent 95b0dd29
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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment