/
params.go
41 lines (33 loc) · 1019 Bytes
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package api
import (
"net/http"
"github.com/gorilla/schema"
"github.com/qri-io/qri/lib"
)
// decoder maps HTTP requests to input structs
var decoder = schema.NewDecoder()
func init() {
// TODO(arqu): once APIs have a strict mapping to Params this line
// should be removed and should error out on unknown keys
decoder.IgnoreUnknownKeys(true)
}
// RequestUnmarshaller is an interface for deserializing from an HTTP request
type RequestUnmarshaller interface {
UnmarshalFromRequest(r *http.Request) error
}
// UnmarshalParams deserializes a lib req params stuct pointer from an HTTP
// request. Only used for handlers that do not use lib.NewHTTPMethodRequest.
func UnmarshalParams(r *http.Request, p interface{}) error {
defer func() {
if defSetter, ok := p.(lib.NZDefaultSetter); ok {
defSetter.SetNonZeroDefaults()
}
}()
if ru, ok := p.(RequestUnmarshaller); ok {
return ru.UnmarshalFromRequest(r)
}
if err := r.ParseForm(); err != nil {
return err
}
return decoder.Decode(p, r.Form)
}