-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.go
193 lines (181 loc) · 5.82 KB
/
converter.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package taqc
import (
"errors"
"fmt"
"net/url"
"reflect"
"strings"
"time"
"github.com/moznion/taqc/internal"
)
var (
ErrNilValueGiven = errors.New("given value is nil")
ErrQueryParameterNameIsEmpty = errors.New("query parameter name is empty in a tag")
ErrUnsupportedFieldType = errors.New("unsupported filed type has come")
ErrUnsupportedUnixTimeUnit = errors.New("unsupported unix time unit has given")
)
// ConvertToQueryParams converts given structure to the query parameters according to the custom tags.
//
// When a field of the structure has `taqc` tag, it converts a value of that field to query parameter.
// Currently, it supports the following field types: `string`, `int64`, `float64`, `bool`, `*string`, `*int64`, `*float64`, `*bool`, `[]string`, `[]int64`, `[]float64`, `time.Time`, `*time.Time`, and `[]time.Time`.
// If the bool field is `true`, the query parameter becomes `param_name=1`. Else, it omits the parameter.
// And when the pointer value is `nil`, it omits the parameter.
//
// This library supports the `time.Time` fields. By default, it encodes that timestamp by `Time#Unix()`.
// If you want to encode it by another unix time format, you can use `unixTimeUnit` custom tag value.
// For example:
//
// type Query struct {
// Foo time.Time `taqc:"foo, unixTimeUnit=millisec"`
// }
//
// in the case of the above example, it encodes the timestamp by `Time#UnixMilli()`.
//
// Currently `unixTimeUnit` supports the following values:
//
// - `sec`
// - `millisec`
// - `microsec`
// - `nanosec`
//
// It also supports encoding with arbitrary time layout by `timeLayout` custom tag value. e.g.
//
// type Query struct {
// Foo time.Time `taqc:"foo, timeLayout=2006-01-02T15:04:05Z07:00"` // RFC3339 layout
// }
//
// then, it encodes the timestamp by `Time#Format()` with given layout.
//
// NOTE: `timeLayout` takes priority over `unixTimeUnit`. This means it uses `timeLayout` option even if you put them together.
func ConvertToQueryParams(v interface{}) (url.Values, error) {
if v == nil {
return nil, ErrNilValueGiven
}
qp := url.Values{}
rv := reflect.ValueOf(v)
elem := rv.Elem()
for i := 0; i < elem.NumField(); i++ {
typeField := elem.Type().Field(i)
tag := typeField.Tag
tagValue, ok := tag.Lookup(internal.TagName)
if !ok { // nothing to do
continue
}
splitTagValues := strings.Split(tagValue, ",")
paramName := strings.TrimSpace(splitTagValues[0])
if paramName == "" {
return nil, ErrQueryParameterNameIsEmpty
}
timeLayout, unixTimeUnit := internal.ExtractTimeTag(splitTagValues[1:])
timeFormatter := func(t time.Time) string {
return fmt.Sprintf("%d", t.Unix())
}
if unixTimeUnit != "" {
unixTimeGetter, err := getUnixTimeGetter(unixTimeUnit)
if err != nil {
return nil, err
}
timeFormatter = func(t time.Time) string {
return fmt.Sprintf("%d", unixTimeGetter(t))
}
}
if timeLayout != "" { // higher priority
timeFormatter = func(t time.Time) string {
return t.Format(timeLayout)
}
}
field := elem.Field(i)
fieldKind := field.Kind()
switch fieldKind {
case reflect.String:
qp.Set(paramName, field.String())
case reflect.Int64:
qp.Set(paramName, fmt.Sprintf("%d", field.Int()))
case reflect.Float64:
qp.Set(paramName, fmt.Sprintf("%f", field.Float()))
case reflect.Bool:
if field.Bool() {
qp.Set(paramName, "1")
}
case reflect.Struct:
if field.Type().PkgPath() == "time" && field.Type().Name() == "Time" {
t := field.Interface().(time.Time)
qp.Set(paramName, timeFormatter(t))
} else {
return nil, fmt.Errorf("field type is %s: %w", fieldKind, ErrUnsupportedFieldType)
}
case reflect.Ptr:
if !field.IsNil() {
fieldElem := field.Elem()
switch fieldElem.Kind() {
case reflect.String:
qp.Set(paramName, fieldElem.String())
case reflect.Int64:
qp.Set(paramName, fmt.Sprintf("%d", fieldElem.Int()))
case reflect.Float64:
qp.Set(paramName, fmt.Sprintf("%f", fieldElem.Float()))
case reflect.Bool:
if fieldElem.Bool() {
qp.Set(paramName, "1")
}
case reflect.Struct:
if fieldElem.Type().PkgPath() == "time" && fieldElem.Type().Name() == "Time" {
t := fieldElem.Interface().(time.Time)
qp.Set(paramName, timeFormatter(t))
} else {
return nil, fmt.Errorf("field type is *%s: %w", fieldKind, ErrUnsupportedFieldType)
}
default:
return nil, fmt.Errorf("field type is *%s: %w", fieldKind, ErrUnsupportedFieldType)
}
}
case reflect.Slice:
l := field.Len()
for j := 0; j < l; j++ {
item := field.Index(j)
switch item.Kind() {
case reflect.String:
qp.Add(paramName, item.String())
case reflect.Int64:
qp.Add(paramName, fmt.Sprintf("%d", item.Int()))
case reflect.Float64:
qp.Add(paramName, fmt.Sprintf("%f", item.Float()))
case reflect.Struct:
if item.Type().PkgPath() == "time" && item.Type().Name() == "Time" {
t := item.Interface().(time.Time)
qp.Add(paramName, timeFormatter(t))
} else {
return nil, fmt.Errorf("field type is []%s: %w", fieldKind, ErrUnsupportedFieldType)
}
default:
return nil, fmt.Errorf("field type is []%s: %w", fieldKind, ErrUnsupportedFieldType)
}
}
default:
return nil, fmt.Errorf("field type is %s: %w", fieldKind, ErrUnsupportedFieldType)
}
}
return qp, nil
}
func getUnixTimeGetter(unixTimeUnit string) (func(t time.Time) int64, error) {
switch unixTimeUnit {
case "", "sec":
return func(t time.Time) int64 {
return t.Unix()
}, nil
case "millisec":
return func(t time.Time) int64 {
return t.UnixMilli()
}, nil
case "microsec":
return func(t time.Time) int64 {
return t.UnixMicro()
}, nil
case "nanosec":
return func(t time.Time) int64 {
return t.UnixNano()
}, nil
default:
return nil, fmt.Errorf("%s is unsupported: %w", unixTimeUnit, ErrUnsupportedUnixTimeUnit)
}
}