-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqparser.go
More file actions
56 lines (52 loc) · 1.64 KB
/
qparser.go
File metadata and controls
56 lines (52 loc) · 1.64 KB
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
47
48
49
50
51
52
53
54
55
56
// Package qparser provides a query parameter decoder for Go.
//
// It parses URL query parameters into user-defined structs using reflection,
// supporting nested structs, slices, pointer fields, numeric types, booleans,
// strings, and time.Time with multiple timestamp formats.
// The Parse, ParseRequest, and ParseURL functions all decode query parameters
// into a struct value provided by the caller.
package qparser
import (
"errors"
"net/http"
"net/url"
"reflect"
)
// Parse decodes the provided url.Values into the struct pointed to by dst.
//
// dst must be a pointer to a struct. Nested fields, slices, primitive types,
// pointer fields, and time.Time are supported.
//
// Example:
//
// var f Filter
// err := qparser.Parse(url.Values{"age": {"30"}}, &f)
func Parse(values url.Values, dst any) error {
rv := reflect.ValueOf(dst)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Struct {
return errors.New("dst must be a pointer to struct")
}
rv = rv.Elem()
rt := rv.Type()
return parseStruct(values, rv, rt)
}
// ParseRequest extracts the query parameters from an http.Request and
// decodes them into the struct pointed to by dst.
//
// Equivalent to calling Parse(r.URL.Query(), dst).
func ParseRequest(r *http.Request, dst any) error {
query := r.URL.Query()
return Parse(query, dst)
}
// ParseURL parses the query parameters from the provided URL string and
// decodes them into the struct pointed to by dst.
//
// Returns an error if the URL cannot be parsed.
func ParseURL(addr string, dst any) error {
urlObj, err := url.Parse(addr)
if err != nil {
return err
}
queryValues := urlObj.Query()
return Parse(queryValues, dst)
}