@cryptotaxi247 / kubo / commits / 31ae17807

commands: Support outputting <-chan interface{}

Matt Bell committed Jan 20, 2015 at 12:38 UTC 31ae178078279ca31e762d8b551f4168817fe8e9
6 files changed +18 -4
commands/command.go
+9
@@ -125,6 +125,15 @@ func (c *Command) Call(req Request) Response {
125 isChan = actualType.Kind() == reflect.Chan
126 }
127
128 + if isChan {
129 + if ch, ok := output.(<-chan interface{}); ok {
130 + output = ch
131 +
132 + } else if ch, ok := output.(chan interface{}); ok {
133 + output = (<-chan interface{})(ch)
134 + }
135 + }
136 +
137 // If the command specified an output type, ensure the actual value returned is of that type
138 if cmd.Type != nil && !isChan {
139 expectedType := reflect.TypeOf(cmd.Type)
commands/http/client.go
+1 -1
@@ -167,7 +167,7 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
167 }
168 }()
169
170 - res.SetOutput(outChan)
170 + res.SetOutput((<-chan interface{})(outChan))
171 return res, nil
172 }
173
commands/http/handler.go
+4
@@ -112,6 +112,10 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
112 // if output is a channel and user requested streaming channels,
113 // use chunk copier for the output
114 _, isChan := res.Output().(chan interface{})
115 + if !isChan {
116 + _, isChan = res.Output().(<-chan interface{})
117 + }
118 +
119 streamChans, _, _ := req.Option("stream-channels").Bool()
120 if isChan && streamChans {
121 // w.WriteString(transferEncodingHeader + ": chunked\r\n")
commands/response.go
+2 -1
@@ -50,7 +50,8 @@ func marshalJson(value interface{}) (io.Reader, error) {
50
51 var marshallers = map[EncodingType]Marshaler{
52 JSON: func(res Response) (io.Reader, error) {
53 - if ch, ok := res.Output().(chan interface{}); ok {
53 + ch, ok := res.Output().(<-chan interface{})
54 + if ok {
55 return &ChannelMarshaler{
56 Channel: ch,
57 Marshaler: marshalJson,
core/commands/add.go
+1 -1
@@ -72,7 +72,7 @@ remains to be implemented.
72 },
73 Marshalers: cmds.MarshalerMap{
74 cmds.Text: func(res cmds.Response) (io.Reader, error) {
75 - outChan, ok := res.Output().(chan interface{})
75 + outChan, ok := res.Output().(<-chan interface{})
76 if !ok {
77 return nil, u.ErrCast()
78 }
core/commands/ping.go
+1 -1
@@ -44,7 +44,7 @@ trip latency information.
44 },
45 Marshalers: cmds.MarshalerMap{
46 cmds.Text: func(res cmds.Response) (io.Reader, error) {
47 - outChan, ok := res.Output().(chan interface{})
47 + outChan, ok := res.Output().(<-chan interface{})
48 if !ok {
49 return nil, u.ErrCast()
50 }