commands: Allow overriding marshaller for any encoding type
Matt Bell committed
Nov 3, 2014 at 23:17 UTC
2a1116cec8be8831546dd7c6346c06451da54475
2 files changed
+7
-16
commands/command.go
+1
-1
@@ -32,7 +32,7 @@ type Command struct {
32
Options []Option
33
Arguments []Argument
34
Run Function
35
- Format Marshaller
35
+ Marshallers map[EncodingType]Marshaller
36
Type interface{}
37
Subcommands map[string]*Command
38
}
commands/response.go
+6
-15
@@ -53,18 +53,6 @@ var marshallers = map[EncodingType]Marshaller{
53
}
54
return xml.Marshal(res.Output())
55
},
56
- Text: func(res Response) ([]byte, error) {
57
- format := res.Request().Command().Format
58
- if format == nil {
59
- return nil, ErrNoFormatter
60
- }
61
-
62
- bytes, err := format(res)
63
- if err != nil {
64
- return nil, err
65
- }
66
- return bytes, nil
67
- },
56
}
57
58
// Response is the result of a command request. Handlers write to the response,
@@ -127,9 +115,12 @@ func (r *response) Marshal() ([]byte, error) {
115
}
116
encType := EncodingType(strings.ToLower(encStr))
117
130
- marshaller, ok := marshallers[encType]
131
- if !ok {
132
- return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
118
+ marshaller := r.req.Command().Marshallers[encType]
119
+ if marshaller == nil {
120
+ marshaller, ok = marshallers[encType]
121
+ if !ok {
122
+ return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
123
+ }
124
}
125
126
return marshaller(r)