-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface_http.go
41 lines (34 loc) · 907 Bytes
/
interface_http.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
/START2 OMIT
type MyEntityUnmarshaller interface {
UnmarshalHTTP(*http.Request) error
}
func GetEntity(r *http.Request, v MyEntityUnmarshaller) error {
return v.UnmarshalHTTP(r)
}
func (u *User) UnmarshalHTTP(r *http.Request) error {
// Implement on our User object this specific method that allows the
// User to describe how it would get itself out of an HTTP request
// ...
}
var u User
if err := GetEntity(req, &u); err != nil {
// ...
// This is safe because `var u User` will automatically
// zero the User struct
}
/END2 OMIT
/START1 OMIT
func PostEntityHandler(w http.ResponseWriter, r *http.Request) {
var b Banana
if err := GetEntity(req, &b); err != nil {
// ...
}
var ike Eisenhower
if err := GetEntity(req, &ike); err != nil {
// ...
}
// ...
}
...
router.POST("/api/v1/entity", PostEntityHandler)
/END1 OMIT