提交 2a1116ce 作者: Matt Bell 提交者: Juan Batiz-Benet

commands: Allow overriding marshaller for any encoding type

上级 33ad56e6
......@@ -32,7 +32,7 @@ type Command struct {
Options []Option
Arguments []Argument
Run Function
Format Marshaller
Marshallers map[EncodingType]Marshaller
Type interface{}
Subcommands map[string]*Command
}
......
......@@ -53,18 +53,6 @@ var marshallers = map[EncodingType]Marshaller{
}
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,
......@@ -127,9 +115,12 @@ func (r *response) Marshal() ([]byte, error) {
}
encType := EncodingType(strings.ToLower(encStr))
marshaller, ok := marshallers[encType]
if !ok {
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
marshaller := r.req.Command().Marshallers[encType]
if marshaller == nil {
marshaller, ok = marshallers[encType]
if !ok {
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
}
}
return marshaller(r)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论