-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbind.go
46 lines (39 loc) · 990 Bytes
/
bind.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
42
43
44
45
46
// Copyright 2019 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pbgo
import (
"encoding/json"
"io"
"net/http"
"github.com/golang/protobuf/proto"
)
func BindQuery(r *http.Request, req proto.Message) error {
return PopulateQueryParametersEx(req, r.URL.Query(), true)
}
func BindForm(r *http.Request, req proto.Message) error {
r.ParseForm()
return PopulateQueryParametersEx(req, r.Form, true)
}
func BindBody(r *http.Request, req proto.Message) error {
err := json.NewDecoder(r.Body).Decode(req)
if err != nil && err != io.EOF {
return err
}
return nil
}
func BindNamedParams(
r *http.Request, req proto.Message,
params interface{ ByName(name string) string },
name ...string,
) error {
for _, fieldPath := range name {
err := PopulateFieldFromPath(req,
fieldPath, params.ByName(fieldPath),
)
if err != nil {
return err
}
}
return nil
}