-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnil_able.go
210 lines (203 loc) · 4.29 KB
/
nil_able.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
// Copyright 2020 io.xream.sqlxb
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 sqlxb
import "strconv"
func Bool(b bool) *bool {
return &b
}
func Int(v int) *int {
return &v
}
func Int64(v int64) *int64 {
return &v
}
func Int32(v int32) *int32 {
return &v
}
func Int16(v int16) *int16 {
return &v
}
func Int8(v int8) *int8 {
return &v
}
func Byte(b byte) *byte {
return &b
}
func Float64(f float64) *float64 {
return &f
}
func Float32(f float32) *float32 {
return &f
}
func Uint64(v uint64) *uint64 {
return &v
}
func Uint(v uint) *uint {
return &v
}
func Np2s(p interface{}) (string, bool) {
switch p.(type) {
case *uint64:
if np := p.(*uint64); np != nil {
return strconv.FormatUint(*np, 10), true
}
case *uint:
if np := p.(*uint); np != nil {
return strconv.FormatUint(uint64(*np), 10), true
}
case *int64:
if np := p.(*int64); np != nil {
return strconv.FormatInt(*np, 10), true
}
case *int:
if np := p.(*int); np != nil {
return strconv.Itoa(*np), true
}
case *int32:
if np := p.(*int32); np != nil {
return strconv.Itoa(int(*np)), true
}
case *int16:
if np := p.(*int16); np != nil {
return strconv.Itoa(int(*np)), true
}
case *int8:
if np := p.(*int8); np != nil {
return strconv.Itoa(int(*np)), true
}
case *byte:
if np := p.(*byte); np != nil {
return strconv.Itoa(int(*np)), true
}
case *float64:
if np := p.(*float64); np != nil {
return strconv.FormatFloat(*np, 'f', -1, 64), true
}
case *float32:
if np := p.(*float32); np != nil {
return strconv.FormatFloat(float64(*np), 'f', -1, 32), true
}
}
return "", false
}
func N2s(p interface{}) string {
switch p.(type) {
case uint64:
return strconv.FormatUint(p.(uint64), 10)
case uint:
return strconv.FormatUint(uint64(p.(uint)), 10)
case int64:
return strconv.FormatInt(p.(int64), 10)
case int:
return strconv.Itoa(p.(int))
case int32:
return strconv.Itoa(int(p.(int32)))
case int16:
return strconv.Itoa(int(p.(int16)))
case int8:
return strconv.Itoa(int(p.(int8)))
case byte:
return strconv.Itoa(int(p.(byte)))
case float64:
return strconv.FormatFloat(p.(float64), 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(p.(float32)), 'f', -1, 32)
}
return ""
}
func NilOrNumber(p interface{}) (bool, interface{}) {
switch p.(type) {
case *uint64:
vp := p.(*uint64)
if vp == nil {
return true, nil
} else {
return false, *p.(*uint64)
}
case *uint:
vp := p.(*uint)
if vp == nil {
return true, nil
} else {
return false, *p.(*uint)
}
case *int64:
vp := p.(*int64)
if vp == nil {
return true, nil
} else {
return false, *p.(*int64)
}
case *int:
vp := p.(*int)
if vp == nil {
return true, nil
} else {
return false, *p.(*int)
}
case *int32:
vp := p.(*int32)
if vp == nil {
return true, nil
} else {
return false, *p.(*int32)
}
case *int16:
vp := p.(*int16)
if vp == nil {
return true, nil
} else {
return false, *p.(*int16)
}
case *int8:
vp := p.(*int8)
if vp == nil {
return true, nil
} else {
return false, *p.(*int8)
}
case *byte:
vp := p.(*byte)
if vp == nil {
return true, nil
} else {
return false, *p.(*byte)
}
case *float64:
vp := p.(*float64)
if vp == nil {
return true, nil
} else {
return false, *p.(*float64)
}
case *float32:
vp := p.(*float32)
if vp == nil {
return true, nil
} else {
return false, *p.(*float32)
}
case *bool:
vp := p.(*bool)
if vp == nil {
return true, nil
} else {
return false, *p.(*bool)
}
}
panic("NOT SUPPORT THE TYPE POINTER")
}