-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataval.go
More file actions
executable file
·289 lines (264 loc) · 4.65 KB
/
Copy pathdataval.go
File metadata and controls
executable file
·289 lines (264 loc) · 4.65 KB
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
package repmeta
import (
"database/sql"
"fmt"
"time"
)
type DataValPtr interface{}
type DataValText string
type DataValType int
const (
DVNone DataValType = iota
DVText
DVInt
DVFloat
DVCurrency
DVBoolean
DVDate
)
type DataVal struct {
Typ DataValType
Ptr DataValPtr
}
func NewDVNone() *DataVal {
dv := DataVal{Typ: DVNone}
return &dv
}
func NewDVBoolean(v ...bool) *DataVal {
var val bool
if len(v) > 0 {
val = v[0]
}
dv := DataVal{Typ: DVBoolean, Ptr: &val}
return &dv
}
func NewDVText(v ...string) *DataVal {
nullStr := sql.NullString {}
if len(v) > 0 {
nullStr.String = v[0]
nullStr.Valid = true
}
dv := DataVal{Typ: DVText, Ptr: &nullStr}
return &dv
}
func NewDVInt(v ...int64) *DataVal {
var val int64
if len(v) > 0 {
val = v[0]
}
dv := DataVal{Typ: DVInt, Ptr: &val}
return &dv
}
func NewDVCurrency(v ...int64) *DataVal {
var val int64
if len(v) > 0 {
val = v[0]
}
dv := DataVal{Typ: DVInt, Ptr: &val}
return &dv
}
func NewDVFloat(v ...float64) *DataVal {
var val float64
if len(v) > 0 {
val = v[0]
}
dv := DataVal{Typ: DVFloat, Ptr: &val}
return &dv
}
func NewDVDate(v ...time.Time) *DataVal {
nullStr := sql.NullString{}
if len(v) > 0 {
nullStr.String = v[0].Format("2006-01-01")
nullStr.Valid = true
}
dv := DataVal{Typ: DVDate, Ptr: &nullStr}
return &dv
}
func (dv *DataVal) ResetAll() {
switch dv.Typ {
case DVNone:
return
case DVDate:
dv.ToDate()
case DVCurrency:
dv.ToCurrency()
case DVText:
dv.ToText()
case DVInt:
dv.ToInt()
case DVFloat:
dv.ToFloat()
case DVBoolean:
dv.ToBool()
}
return
}
func (dv *DataVal) ResetNumerics() {
switch dv.Typ {
case DVNone:
return
case DVDate:
return
case DVCurrency:
dv.ToCurrency()
case DVText:
return
case DVInt:
dv.ToInt()
case DVFloat:
dv.ToFloat()
case DVBoolean:
dv.ToBool()
}
return
}
func (dv *DataVal) ToNone() {
dv.Typ = DVNone
dv.Ptr = nil
}
func (dv *DataVal) ToText(v ...*string) {
dv.ToNone()
dv.Typ = DVText
nullStr := sql.NullString{}
if len(v) > 0 {
nullStr.Valid = true
nullStr.String = *v[0]
}
dv.Ptr = &nullStr
}
func (dv *DataVal) ToInt(v ...*int64) {
dv.ToNone()
dv.Typ = DVInt
if len(v) == 0 {
dv.Ptr = new(int64)
} else {
dv.Ptr = v[0]
}
}
func (dv *DataVal) ToCurrency(v ...*int64) {
dv.ToNone()
dv.Typ = DVCurrency
if len(v) == 0 {
dv.Ptr = new(int64)
} else {
dv.Ptr = v[0]
}
}
func (dv *DataVal) ToFloat(v ...*float64) {
dv.ToNone()
dv.Typ = DVFloat
if len(v) == 0 {
dv.Ptr = new(float64)
} else {
dv.Ptr = v[0]
}
}
func (dv *DataVal) ToBool(v ...*bool) {
dv.ToNone()
dv.Typ = DVBoolean
if len(v) == 0 {
dv.Ptr = new(bool)
} else {
dv.Ptr = v[0]
}
}
func (dv *DataVal) ToDate(v ...*time.Time) {
dv.ToNone()
dv.Typ = DVDate
nullStr := sql.NullString{}
if len(v) > 0 {
pTime := *v[0]
nullStr.Valid = true
nullStr.String = pTime.Format("2006-01-02")
}
dv.Ptr = &nullStr
}
func (dv *DataVal) String() string {
switch dv.Typ {
case DVNone:
return ""
case DVDate:
nullStr := *dv.Ptr.(*sql.NullString)
if nullStr.Valid {
return nullStr.String
}
return ""
case DVCurrency:
pennies := *dv.Ptr.(*int64)
dollars := pennies / 100
cents := pennies % 100
return fmt.Sprintf("%d.%2.2d", dollars, cents)
case DVText:
nullStr := *dv.Ptr.(*sql.NullString)
if nullStr.Valid {
return nullStr.String
}
return ""
case DVInt:
return fmt.Sprintf("%d", *dv.Ptr.(*int64))
case DVFloat:
return fmt.Sprintf("%.2f", *dv.Ptr.(*float64))
case DVBoolean:
return fmt.Sprintf("%t", *dv.Ptr.(*bool))
}
return ""
}
func ToDataValType(s string) DataValType {
switch s {
case "text":
return DVText
case "int":
return DVInt
case "date":
return DVDate
case "currency":
return DVCurrency
case "float":
return DVFloat
case "boolean":
return DVBoolean
}
return DVNone
}
func (dv *DataVal) FromDataVal() string {
switch dv.Typ {
case DVNone:
return "DVNone"
case DVDate:
return "DVDate"
case DVCurrency:
return "DVCurrency"
case DVText:
return "DVText"
case DVInt:
return "DVInt"
case DVFloat:
return "DVFloat"
case DVBoolean:
return "DVBoolean"
}
return ""
}
func (dv *DataVal) GetPointer() DataValPtr {
return dv.Ptr
}
func (dv *DataVal) DidAccumulate(other *DataVal) bool {
thisType := dv.Typ
if thisType != other.Typ {
return false
}
didAccumulate := true
switch thisType {
case DVInt, DVCurrency:
thisVal := *dv.Ptr.(*int64)
otherVal := *other.Ptr.(*int64)
*dv.Ptr.(*int64) = thisVal + otherVal
case DVFloat:
thisVal := *dv.Ptr.(*float64)
otherVal := *other.Ptr.(*float64)
*dv.Ptr.(*float64) = thisVal + otherVal
default:
didAccumulate = false
}
return didAccumulate
}