-
Notifications
You must be signed in to change notification settings - Fork 2
/
optional_Uint.go
302 lines (265 loc) · 6.1 KB
/
optional_Uint.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Code generated by gotemplate. DO NOT EDIT.
package optional
import (
"database/sql/driver"
"encoding/json"
"encoding/xml"
"fmt"
"reflect"
"strconv"
"time"
"github.com/imiskolee/optional/optional_scanner"
)
var _Uint = time.Time{}
var __Uint = optional_scanner.ScanBool
// template type Optional(T,scan)
//swagger:type T
type Uint optionalUint
type optionalUint []uint
const (
valueKeyUint = iota
)
func scanValueUint(input string) (val uint, err error) {
v, err := optional_scanner.ScanUint(input)
return uint(v), err
}
func maybeBlankUint() bool {
var emptyVal uint
switch reflect.ValueOf(emptyVal).Interface().(type) {
case string, []byte, bool:
return true
default:
return false
}
}
// Of wraps the value in an optional.
func OfUint(value uint) Uint {
return Uint{valueKeyUint: value}
}
func OfUintPtr(ptr *uint) Uint {
if ptr == nil {
return EmptyUint()
} else {
return OfUint(*ptr)
}
}
// Empty returns an empty optional.
func EmptyUint() Uint {
return nil
}
// Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.
func (o Uint) Get() (value uint, ok bool) {
o.If(func(v uint) {
value = v
ok = true
})
return
}
func (o Uint) IsNil() bool {
return o == nil || len(o) == 0
}
func (o Uint) IsPresent() bool {
return !o.IsBlank()
}
func (o Uint) IsBlank() bool {
if o.IsNil() {
return true
}
if !maybeBlankUint() {
return false
}
var emptyVal uint
if o.V() == emptyVal {
return true
}
return false
}
// If calls the function if there is a value wrapped by this optional.
func (o Uint) If(f func(value uint)) {
if !o.IsNil() {
f(o[valueKeyUint])
}
}
func (o Uint) ElseFunc(f func() uint) (value uint) {
if !o.IsNil() {
o.If(func(v uint) { value = v })
return
} else {
return f()
}
}
// Else returns the value wrapped by this optional, or the value passed in if
// there is no value wrapped by this optional.
func (o Uint) Else(elseValue uint) (value uint) {
return o.ElseFunc(func() uint { return elseValue })
}
// V returns the value wrapped by this optional, or the zero value of
// the type wrapped if there is no value wrapped by this optional.
func (o Uint) V() (value uint) {
var zero uint
return o.Else(zero)
}
// String returns the string representation of the wrapped value, or the string
// representation of the zero value of the type wrapped if there is no value
// wrapped by this optional.
func (o Uint) String() string {
if !o.IsNil() {
return fmt.Sprintf("%v", o.V())
}
return fmt.Sprintf("%v", nil)
}
// MarshalJSON marshals the value being wrapped to JSON. If there is no vale
// being wrapped, the zero value of its type is marshaled.
func (o Uint) MarshalJSON() (data []byte, err error) {
if !o.IsNil() {
return json.Marshal(o[valueKeyUint])
}
return []byte("null"), nil
}
// UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.
func (o *Uint) UnmarshalJSON(data []byte) error {
//nothing todo if null value
if string(data) == "null" {
return nil
}
var v uint
//empty string
if string(data) == "" || string(data) == "\"\"" || string(data) == "''" {
*o = OfUint(v)
return nil
}
switch reflect.TypeOf(v).Kind() {
case reflect.Bool:
if len(data) > 2 {
if data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}
}
d, err := strconv.ParseBool(string(data))
if err != nil {
return err
}
if d {
data = []byte("true")
} else {
data = []byte("false")
}
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
d, err := strconv.ParseBool(string(data))
if err == nil {
if d {
data = []byte("1")
} else {
data = []byte("0")
}
}
}
err := json.Unmarshal(data, &v)
//Try unmarshal string numbers with quote
if err != nil && len(data) > 2 {
if data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}
if data[0] == '\'' && data[len(data)-1] == '\'' {
data = data[1 : len(data)-1]
}
err = json.Unmarshal(data, &v)
}
//for number to string
if err != nil {
d := fmt.Sprintf(`"%s"`, string(data))
err = json.Unmarshal([]byte(d), &v)
}
if err != nil {
return err
}
*o = OfUint(v)
return nil
}
func (o *Uint) UnmarshalText(data []byte) error {
return o.Scan(string(data))
}
func (o *Uint) MarshalText() ([]byte, error) {
if o == nil {
return []byte(""), nil
}
return []byte(o.String()), nil
}
// MarshalXML marshals the value being wrapped to XML. If there is no vale
// being wrapped, the zero value of its type is marshaled.
func (o Uint) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(o.V(), start)
}
// UnmarshalXML unmarshals the XML into a value wrapped by this optional.
func (o *Uint) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v uint
err := d.DecodeElement(&v, &start)
if err != nil {
return err
}
*o = OfUint(v)
return nil
}
func (c Uint) Value() (driver.Value, error) {
v, ok := c.Get()
if ok {
return driver.DefaultParameterConverter.ConvertValue(v)
}
return driver.DefaultParameterConverter.ConvertValue(nil)
}
func (c *Uint) Scan(input interface{}) (err error) {
var vv string
var isvalid = true
switch reflect.ValueOf(input).Kind() {
case reflect.Ptr, reflect.Map, reflect.Interface, reflect.Slice:
if reflect.ValueOf(input).IsNil() {
isvalid = false
}
}
if isvalid {
switch value := input.(type) {
case string:
vv = value
case []byte:
if value != nil {
vv = string(value)
} else {
isvalid = false
}
default:
vv = fmt.Sprint(input)
}
}
//for empty string
if vv == "" {
var zero uint
*c = OfUint(zero)
return
}
if isvalid {
val, err := scanValueUint(vv)
if err != nil {
return err
}
*c = OfUint(val)
}
return
}
func (c Uint) GormDataType() string {
var t uint
var it interface{}
it = t
switch it.(type) {
case bool:
return "TINYINT(1)"
case uint8, uint16, uint32, int, int8, int16, int32:
return "INT"
case uint64, int64:
return "BIGINT(20)"
case time.Time, *time.Time:
return "DATETIME"
case string, []byte:
return "CHAR(255)"
}
return "VARCHAR(255)"
}