Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

simplify httpclient.Stream invocation #728

Merged
merged 1 commit into from
Jan 13, 2015
Merged

Conversation

warpfork
Copy link
Contributor

This makes creating http client streams significantly terser at the cost of a tiny bit more reflection, by automatically producing message objects based on the output channel's element type.

stream := stream.New()
chanValue := reflect.ValueOf(outputCh)
stopChanValue := reflect.ValueOf(stream.StopCh)
msgType := reflect.TypeOf(outputCh).Elem().Elem()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just call chanValue.Type

@@ -34,7 +33,7 @@ func Stream(res *http.Response, msgFactory func() interface{}, outputCh interfac
r := bufio.NewReader(res.Body)
dec := sse.NewDecoder(r)
for {
msg := msgFactory()
msg := reflect.New(msgType).Interface()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you save the reflect.Value instead of throwing it away, there's no need for the ValueOf call below.

This makes creating http client streams significantly terser at the cost of a tiny bit more reflection, by automatically producing message objects based on the output channel's element type.

Signed-off-by: Eric Myhre <eric@flynn.io>
@titanous
Copy link
Contributor

LGTM

warpfork added a commit that referenced this pull request Jan 13, 2015
simplify httpclient.Stream invocation
@warpfork warpfork merged commit 12275a0 into master Jan 13, 2015
@warpfork warpfork deleted the simplify-http-stream-params branch January 13, 2015 04:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants