-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
35 lines (29 loc) · 903 Bytes
/
query.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
package binder
import (
"errors"
"fmt"
"net/http"
)
// BindQuery binds the passed v pointer to the request.
// It uses the query string for binding.
// `v` param should be a pointer to a struct with `query“ tags.
// Implements the binder.BinderFunc interface.
func BindQuery(r *http.Request, v interface{}) error {
// Check if the request method is GET, HEAD or DELETE
if !isGetHeadOptionDelete(r) {
return fmt.Errorf("%w: %s", ErrInvalidMethod, r.Method)
}
// Validate v pointer before decoding query into it
if !isPointer(v) {
return errors.Join(ErrInvalidInput, ErrTargetMustBeAPointer)
}
// Check if the request query is empty
if r.URL.RawQuery == "" {
return ErrEmptyQuery
}
// Decode the request query into the v pointer and handle decoding errors
if err := queryDecoder.Decode(v, r.URL.Query()); err != nil {
return errors.Join(ErrDecodeQuery, err)
}
return nil
}