1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

commands: Added marshalling to Response

This commit is contained in:
Matt Bell
2014-10-10 13:33:47 -07:00
committed by Juan Batiz-Benet
parent 95b0dd29f8
commit a3a843759b

View File

@ -1,5 +1,12 @@
package commands package commands
import (
"fmt"
"strings"
"encoding/json"
"encoding/xml"
)
type ErrorType uint type ErrorType uint
const ( const (
Normal ErrorType = iota // general errors Normal ErrorType = iota // general errors
@ -13,6 +20,19 @@ type Error struct {
code ErrorType 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 { type Response struct {
req *Request req *Request
Error error Error error
@ -29,6 +49,17 @@ func (r *Response) FormatError() Error {
return Error{ r.Error.Error(), r.ErrorType } 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)
}