-
Notifications
You must be signed in to change notification settings - Fork 2
/
optional_Uint64.go
302 lines (265 loc) · 6.23 KB
/
optional_Uint64.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 _Uint64 = time.Time{}
var __Uint64 = optional_scanner.ScanBool
// template type Optional(T,scan)
//swagger:type T
type Uint64 optionalUint64
type optionalUint64 []uint64
const (
valueKeyUint64 = iota
)
func scanValueUint64(input string) (val uint64, err error) {
v, err := optional_scanner.ScanUint(input)
return uint64(v), err
}
func maybeBlankUint64() bool {
var emptyVal uint64
switch reflect.ValueOf(emptyVal).Interface().(type) {
case string, []byte, bool:
return true
default:
return false
}
}
// Of wraps the value in an optional.
func OfUint64(value uint64) Uint64 {
return Uint64{valueKeyUint64: value}
}
func OfUint64Ptr(ptr *uint64) Uint64 {
if ptr == nil {
return EmptyUint64()
} else {
return OfUint64(*ptr)
}
}
// Empty returns an empty optional.
func EmptyUint64() Uint64 {
return nil
}
// Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.
func (o Uint64) Get() (value uint64, ok bool) {
o.If(func(v uint64) {
value = v
ok = true
})
return
}
func (o Uint64) IsNil() bool {
return o == nil || len(o) == 0
}
func (o Uint64) IsPresent() bool {
return !o.IsBlank()
}
func (o Uint64) IsBlank() bool {
if o.IsNil() {
return true
}
if !maybeBlankUint64() {
return false
}
var emptyVal uint64
if o.V() == emptyVal {
return true
}
return false
}
// If calls the function if there is a value wrapped by this optional.
func (o Uint64) If(f func(value uint64)) {
if !o.IsNil() {
f(o[valueKeyUint64])
}
}
func (o Uint64) ElseFunc(f func() uint64) (value uint64) {
if !o.IsNil() {
o.If(func(v uint64) { 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 Uint64) Else(elseValue uint64) (value uint64) {
return o.ElseFunc(func() uint64 { 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 Uint64) V() (value uint64) {
var zero uint64
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 Uint64) 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 Uint64) MarshalJSON() (data []byte, err error) {
if !o.IsNil() {
return json.Marshal(o[valueKeyUint64])
}
return []byte("null"), nil
}
// UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.
func (o *Uint64) UnmarshalJSON(data []byte) error {
//nothing todo if null value
if string(data) == "null" {
return nil
}
var v uint64
//empty string
if string(data) == "" || string(data) == "\"\"" || string(data) == "''" {
*o = OfUint64(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 = OfUint64(v)
return nil
}
func (o *Uint64) UnmarshalText(data []byte) error {
return o.Scan(string(data))
}
func (o *Uint64) 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 Uint64) 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 *Uint64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v uint64
err := d.DecodeElement(&v, &start)
if err != nil {
return err
}
*o = OfUint64(v)
return nil
}
func (c Uint64) Value() (driver.Value, error) {
v, ok := c.Get()
if ok {
return driver.DefaultParameterConverter.ConvertValue(v)
}
return driver.DefaultParameterConverter.ConvertValue(nil)
}
func (c *Uint64) 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 uint64
*c = OfUint64(zero)
return
}
if isvalid {
val, err := scanValueUint64(vv)
if err != nil {
return err
}
*c = OfUint64(val)
}
return
}
func (c Uint64) GormDataType() string {
var t uint64
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)"
}