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