1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 19:19:06 +08:00

commands: Allow overriding marshaller for any encoding type

This commit is contained in:
Matt Bell
2014-11-03 23:17:39 -08:00
committed by Juan Batiz-Benet
parent 33ad56e6d0
commit 2a1116cec8
2 changed files with 7 additions and 16 deletions

View File

@ -32,7 +32,7 @@ type Command struct {
Options []Option Options []Option
Arguments []Argument Arguments []Argument
Run Function Run Function
Format Marshaller Marshallers map[EncodingType]Marshaller
Type interface{} Type interface{}
Subcommands map[string]*Command Subcommands map[string]*Command
} }

View File

@ -53,18 +53,6 @@ var marshallers = map[EncodingType]Marshaller{
} }
return xml.Marshal(res.Output()) return xml.Marshal(res.Output())
}, },
Text: func(res Response) ([]byte, error) {
format := res.Request().Command().Format
if format == nil {
return nil, ErrNoFormatter
}
bytes, err := format(res)
if err != nil {
return nil, err
}
return bytes, nil
},
} }
// Response is the result of a command request. Handlers write to the response, // Response is the result of a command request. Handlers write to the response,
@ -127,9 +115,12 @@ func (r *response) Marshal() ([]byte, error) {
} }
encType := EncodingType(strings.ToLower(encStr)) encType := EncodingType(strings.ToLower(encStr))
marshaller, ok := marshallers[encType] marshaller := r.req.Command().Marshallers[encType]
if !ok { if marshaller == nil {
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc) marshaller, ok = marshallers[encType]
if !ok {
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
}
} }
return marshaller(r) return marshaller(r)