Conversation
… for handling well-defined request/response payloads
c2h5oh
left a comment
There was a problem hiding this comment.
I like it - I just have a couple of questions/suggestions.
We should also remove some of the hardcoded values before we merge this - like for example charset of json response
| return ContentTypeEventStream | ||
| default: | ||
| return ContentTypeUnknown | ||
| } |
There was a problem hiding this comment.
If we used a global map[string]ContentType var we could let people add their own - similar to https://golang.org/pkg/mime/#AddExtensionType
|
|
||
| return ContentTypePlainText // Default ContentType. | ||
| if contentType == ContentTypeUnknown { | ||
| contentType = ContentTypePlainText |
There was a problem hiding this comment.
Let's return ContentTypeUnknown instead of silently overwriting it
| func renderer(w http.ResponseWriter, r *http.Request, v Renderer) error { | ||
| rv := reflect.ValueOf(v) | ||
| if rv.Kind() == reflect.Ptr { | ||
| rv = rv.Elem() |
There was a problem hiding this comment.
My reflect knowledge is somewhat lacking: why not simply v = reflect.Indirect(reflect.ValueOf(v)) ?
| // itself. Effectively, allowing you to easily add your own logic to the package | ||
| // defaults. For example, maybe you want to impose a limit on the number of | ||
| // bytes allowed to be read from the request body. | ||
| var Decode = DefaultDecoder |
There was a problem hiding this comment.
Being able to set a Decoder per Binder (with fallback to DefaultDecoder) would be super useful for handling forms with something like http://www.gorillatoolkit.org/pkg/schema
| JSON(w, r, v) | ||
| } | ||
| type Binder interface { | ||
| Bind(r *http.Request) error |
Rewrite of chi/render pkg, introducing Binder and Renderer interfaces for handling well-defined request/response payloads. See _examples/rest/main.go in this branch for a feel of the API.
See #84
Feedback is welcome!