-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcore.go
206 lines (175 loc) · 4.09 KB
/
core.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
194
195
196
197
198
199
200
201
202
203
204
205
206
package core
import (
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
type CommonResponse struct {
StatusCode int
Code int `json:"code"`
Details []CommonResponseDetail `json:"details"`
Message string `json:"message"`
}
type CommonResponseDetail struct {
TypeUrl string `json:"typeUrl"`
Value string `json:"value"`
}
type Response struct {
CommonResponse
Data interface{}
}
func (r Response) CopyCommonResponse(rhs *CommonResponse) {
rhs.Code = r.Code
rhs.Details = r.Details
rhs.StatusCode = r.StatusCode
rhs.Message = r.Message
}
func getDefaultValues(v reflect.Value) error {
vValue := v.Elem()
vType := vValue.Type()
for i := 0; i < vType.NumField(); i++ {
field := vType.Field(i)
switch field.Type.Kind() {
case reflect.Slice:
for j := 0; j < vValue.Field(i).Len(); j++ {
// skip if slice type is primitive
if vValue.Field(i).Index(j).Kind() != reflect.Struct {
continue
}
// Attach any slices as query params
err := getDefaultValues(vValue.Field(i).Index(j).Addr())
if err != nil {
return err
}
}
case reflect.String:
isNil, err := isZero(vValue.Field(i).Addr())
if err != nil {
return err
}
if !isNil {
continue
}
defaultValue, ok := field.Tag.Lookup("default")
if !ok {
continue
}
vValue.Field(i).SetString(defaultValue)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
isNil, err := isZero(vValue.Field(i).Addr())
if err != nil {
return err
}
if !isNil {
continue
}
defaultValue, ok := field.Tag.Lookup("default")
if !ok {
continue
}
defaultValueInt, err := strconv.Atoi(defaultValue)
if err != nil {
return err
}
vValue.Field(i).SetInt(int64(defaultValueInt))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
isNil, err := isZero(vValue.Field(i).Addr())
if err != nil {
return err
}
if !isNil {
continue
}
defaultValue, ok := field.Tag.Lookup("default")
if !ok {
continue
}
defaultValueUint, err := strconv.ParseUint(defaultValue, 10, 64)
if err != nil {
return err
}
vValue.Field(i).SetUint(uint64(defaultValueUint))
case reflect.Struct:
err := getDefaultValues(vValue.Field(i).Addr())
if err != nil {
return err
}
case reflect.Ptr:
isNil, err := isZero(vValue.Field(i).Addr())
if err != nil {
return err
}
if isNil {
continue
}
if err := getDefaultValues(vValue.Field(i)); err != nil {
return err
}
default:
continue
}
}
return nil
}
func buildRawQuery(req *http.Request, v interface{}) (string, error) {
query := req.URL.Query()
if v == nil {
return query.Encode(), nil
}
err := getDefaultValues(reflect.ValueOf(v))
if err != nil {
return "", err
}
return query.Encode(), nil
}
func isZero(v reflect.Value) (bool, error) {
t := v.Elem().Type()
if !t.Comparable() {
return false, fmt.Errorf("type is not comparable: %v", t)
}
return reflect.Zero(t).Equal(v.Elem()), nil
}
func TimeFromString(t *testing.T, format, datetime string) time.Time {
dt, err := time.Parse(format, datetime)
if err != nil {
t.Errorf("error when parsing time: %s", err)
}
return dt
}
const ShortDateLayout = "2006-01-02"
// Do not use this structure for responses
// as there are no ways to unmarshal to any layout
// and leave nil if json field is null
type TimeFormat struct {
time.Time
layout string
}
func NewTimeFormat(t time.Time, layout string) *TimeFormat {
return &TimeFormat{
Time: t,
layout: layout,
}
}
func newTimeLayout(layout string) *TimeFormat {
return &TimeFormat{
layout: layout,
}
}
func (rd *TimeFormat) UnmarshalJSON(b []byte) error {
var err error
s := strings.Trim(string(b), `"`) // remove quotes
// Added for extra accuracy
// encoding/json won't invoke this method if field is null
if s == "null" {
return nil
}
rd.Time, err = time.Parse(rd.layout, s)
return err
}
func (rd *TimeFormat) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%q`, rd.Time.Format(rd.layout))), nil
}