-
Notifications
You must be signed in to change notification settings - Fork 534
/
convert.go
194 lines (169 loc) · 4.94 KB
/
convert.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
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package queryparams
import (
"fmt"
"net/url"
"reflect"
"strings"
)
// Marshaler converts an object to a query parameter string representation
type Marshaler interface {
MarshalQueryParameter() (string, error)
}
// Unmarshaler converts a string representation to an object
type Unmarshaler interface {
UnmarshalQueryParameter(string) error
}
func jsonTag(field reflect.StructField) (string, bool) {
structTag := field.Tag.Get("json")
if len(structTag) == 0 {
return "", false
}
parts := strings.Split(structTag, ",")
tag := parts[0]
if tag == "-" {
tag = ""
}
omitempty := false
parts = parts[1:]
for _, part := range parts {
if part == "omitempty" {
omitempty = true
break
}
}
return tag, omitempty
}
func isPointerKind(kind reflect.Kind) bool {
return kind == reflect.Pointer
}
func isStructKind(kind reflect.Kind) bool {
return kind == reflect.Struct
}
func isValueKind(kind reflect.Kind) bool {
switch kind {
case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8,
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32,
reflect.Float64, reflect.Complex64, reflect.Complex128:
return true
default:
return false
}
}
func zeroValue(value reflect.Value) bool {
return reflect.DeepEqual(reflect.Zero(value.Type()).Interface(), value.Interface())
}
func customMarshalValue(value reflect.Value) (reflect.Value, bool) {
// Return unless we implement a custom query marshaler
if !value.CanInterface() {
return reflect.Value{}, false
}
marshaler, ok := value.Interface().(Marshaler)
if !ok {
if !isPointerKind(value.Kind()) && value.CanAddr() {
marshaler, ok = value.Addr().Interface().(Marshaler)
if !ok {
return reflect.Value{}, false
}
} else {
return reflect.Value{}, false
}
}
// Don't invoke functions on nil pointers
// If the type implements MarshalQueryParameter, AND the tag is not omitempty, AND the value is a nil pointer, "" seems like a reasonable response
if isPointerKind(value.Kind()) && zeroValue(value) {
return reflect.ValueOf(""), true
}
// Get the custom marshalled value
v, err := marshaler.MarshalQueryParameter()
if err != nil {
return reflect.Value{}, false
}
return reflect.ValueOf(v), true
}
func addParam(values url.Values, tag string, omitempty bool, value reflect.Value) {
if omitempty && zeroValue(value) {
return
}
val := ""
iValue := fmt.Sprintf("%v", value.Interface())
if iValue != "<nil>" {
val = iValue
}
values.Add(tag, val)
}
func addListOfParams(values url.Values, tag string, omitempty bool, list reflect.Value) {
for i := 0; i < list.Len(); i++ {
addParam(values, tag, omitempty, list.Index(i))
}
}
// Convert takes an object and converts it to a url.Values object using JSON tags as
// parameter names. Only top-level simple values, arrays, and slices are serialized.
// Embedded structs, maps, etc. will not be serialized.
func Convert(obj interface{}) (url.Values, error) {
result := url.Values{}
if obj == nil {
return result, nil
}
var sv reflect.Value
switch reflect.TypeOf(obj).Kind() {
case reflect.Pointer, reflect.Interface:
sv = reflect.ValueOf(obj).Elem()
default:
return nil, fmt.Errorf("expecting a pointer or interface")
}
st := sv.Type()
if !isStructKind(st.Kind()) {
return nil, fmt.Errorf("expecting a pointer to a struct")
}
// Check all object fields
convertStruct(result, st, sv)
return result, nil
}
func convertStruct(result url.Values, st reflect.Type, sv reflect.Value) {
for i := 0; i < st.NumField(); i++ {
field := sv.Field(i)
tag, omitempty := jsonTag(st.Field(i))
if len(tag) == 0 {
continue
}
ft := field.Type()
kind := ft.Kind()
if isPointerKind(kind) {
ft = ft.Elem()
kind = ft.Kind()
if !field.IsNil() {
field = reflect.Indirect(field)
// If the field is non-nil, it should be added to params
// and the omitempty should be overwite to false
omitempty = false
}
}
switch {
case isValueKind(kind):
addParam(result, tag, omitempty, field)
case kind == reflect.Array || kind == reflect.Slice:
if isValueKind(ft.Elem().Kind()) {
addListOfParams(result, tag, omitempty, field)
}
case isStructKind(kind) && !(zeroValue(field) && omitempty):
if marshalValue, ok := customMarshalValue(field); ok {
addParam(result, tag, omitempty, marshalValue)
} else {
convertStruct(result, ft, field)
}
}
}
}