-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.go
38 lines (32 loc) · 835 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
package hyper
import (
"context"
"errors"
"net/http"
"github.com/infiniteloopcloud/log"
)
var (
ErrBindMissingBody = errors.New("request bind error, missing request body")
ErrBind = "request bind error, "
)
var (
MissingRequestBody = "Missing request body"
InvalidRequest = "Invalid request"
)
// Bind is binding the request body into v variable
func Bind(ctx context.Context, r *http.Request, v interface{}) error {
if r.Body == nil {
return BadRequest(ctx, MissingRequestBody, ErrBindMissingBody)
}
if err := jsonEncoder.Decode(r.Body, &v); err != nil {
return BadRequest(ctx, InvalidRequest, errors.New(ErrBind+err.Error()))
}
return nil
}
func GetCorrelationID(ctx context.Context) string {
ctxVal := ctx.Value(log.CorrelationID)
if v, ok := ctxVal.(string); ok {
return v
}
return ""
}