-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
76 lines (72 loc) · 1.91 KB
/
Copy pathquery.go
File metadata and controls
76 lines (72 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package flashduty
import (
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
)
// addQueryParams appends opt's `url`-tagged struct fields to path as query
// parameters. A field tagged `url:"name,omitempty"` is skipped when it holds
// its zero value; `url:"-"` and untagged fields are ignored. This is a small,
// dependency-free analogue of go-querystring used by the generated GET methods.
func addQueryParams(path string, opt any) (string, error) {
if opt == nil {
return path, nil
}
v := reflect.ValueOf(opt)
for v.Kind() == reflect.Pointer {
if v.IsNil() {
return path, nil
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return path, fmt.Errorf("query options must be a struct, got %s", v.Kind())
}
q := url.Values{}
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.IsExported() {
continue
}
tag := field.Tag.Get("url")
if tag == "" || tag == "-" {
continue
}
name, opts, _ := strings.Cut(tag, ",")
if name == "" {
continue
}
fv := v.Field(i)
if strings.Contains(opts, "omitempty") && fv.IsZero() {
continue
}
switch fv.Kind() {
case reflect.String:
q.Set(name, fv.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
q.Set(name, strconv.FormatInt(fv.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
q.Set(name, strconv.FormatUint(fv.Uint(), 10))
case reflect.Bool:
q.Set(name, strconv.FormatBool(fv.Bool()))
case reflect.Float32, reflect.Float64:
q.Set(name, strconv.FormatFloat(fv.Float(), 'g', -1, 64))
case reflect.Slice, reflect.Array:
for j := 0; j < fv.Len(); j++ {
q.Add(name, fmt.Sprint(fv.Index(j).Interface()))
}
default:
q.Set(name, fmt.Sprint(fv.Interface()))
}
}
if len(q) == 0 {
return path, nil
}
if strings.Contains(path, "?") {
return path + "&" + q.Encode(), nil
}
return path + "?" + q.Encode(), nil
}