Last active
August 29, 2015 14:11
-
-
Save harlow/e45277d9ad1522c461e9 to your computer and use it in GitHub Desktop.
Factor out friction (http://talks.golang.org/2014/readability.slide#1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (h *RESTHandler) finishReq(op *Operation, req *http.Request, w http.ResponseWriter) { | |
| result, complete := op.StatusOrResult() | |
| obj := result.Object | |
| if complete { | |
| status := http.StatusOK | |
| if result.Created { | |
| status = http.StatusCreated | |
| } | |
| switch stat := obj.(type) { | |
| case *api.Status: | |
| if stat.Code != 0 { | |
| status = stat.Code | |
| } | |
| } | |
| writeJSON(status, h.codec, obj, w) | |
| } else { | |
| writeJSON(http.StatusAccepted, h.codec, obj, w) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func finishStatus(r Result, complete bool) int { | |
| if !complete { | |
| return http.StatusAccepted | |
| } | |
| if stat, ok := r.Object.(*api.Status); ok && stat.Code != 0 { | |
| return stat.Code | |
| } | |
| if r.Created { | |
| return http.StatusCreated | |
| } | |
| return http.StatusOK | |
| } | |
| func (h *RESTHandler) finishReq(op *Operation, w http.ResponseWriter, req *http.Request) { | |
| result, complete := op.StatusOrResult() | |
| status := finishStatus(result, complete) | |
| writeJSON(status, h.codec, result.Object, w) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func BrowserHeightBucket(s *session.Event) string { | |
| browserSize := sizeFromSession(s) | |
| if h := browserSize.GetHeight(); h > 0 { | |
| browserHeight := int(h) | |
| if browserHeight <= 480 { | |
| return "small" | |
| } else if browserHeight <= 640 { | |
| return "medium" | |
| } else { | |
| return "large" | |
| } | |
| } else { | |
| return "null" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func BrowserHeightBucket(s *session.Event) string { | |
| size := sizeFromSession(s) | |
| h := size.GetHeight() | |
| switch { | |
| case h <= 0: | |
| return "null" | |
| case h <= 480: | |
| return "small" | |
| case h <= 640: | |
| return "medium" | |
| default: | |
| return "large" | |
| } | |
| } |
Author
harlow
commented
Dec 14, 2014
Author
// Package math provides basic constants and mathematical functions.
package math
// A Request represents a request to run a command.
type Request struct { ..
// Encode writes the JSON encoding of req to w.
func Encode(w io.Writer, req *Request) {
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment