-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.go
179 lines (169 loc) · 3.71 KB
/
number.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
package convpb
import (
"database/sql"
"fmt"
"strconv"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64
}
type NumberValuer interface {
Commoner[NumberValuer]
uintRefValuer
uintValuer
intRefValuer
intValuer
floatRefValuer
floatValuer
sqlValuer
}
type uintRefValuer interface {
ToUintRef() *uint
ToUint8Ref() *uint8
ToUint16Ref() *uint16
ToUint32Ref() *uint32
ToUint64Ref() *uint64
}
type uintValuer interface {
ToUint() uint
ToUint8() uint8
ToUint16() uint16
ToUint32() uint32
ToUint64() uint64
}
type intRefValuer interface {
ToIntRef() *int
ToInt8Ref() *int8
ToInt16Ref() *int16
ToInt32Ref() *int32
ToInt64Ref() *int64
}
type intValuer interface {
ToInt() int
ToInt8() int8
ToInt16() int16
ToInt32() int32
ToInt64() int64
}
type floatRefValuer interface {
ToFloat32Ref() *float32
ToFloat64Ref() *float64
}
type floatValuer interface {
ToFloat32() float32
ToFloat64() float64
}
type sqlValuer interface {
ToSQLNullInt16() sql.NullInt16
ToSQLNullInt32() sql.NullInt32
ToSQLNullInt64() sql.NullInt64
}
type NumberWrapper interface {
Commoner[NumberWrapper]
ToUInt32Value() *wrapperspb.UInt32Value
ToUInt64Value() *wrapperspb.UInt64Value
ToInt32Value() *wrapperspb.Int32Value
ToInt64Value() *wrapperspb.Int64Value
ToFloatValue() *wrapperspb.FloatValue
ToDoubleValue() *wrapperspb.DoubleValue
ToStringValue() *wrapperspb.StringValue
}
type numberWrapper[T number] struct {
value *T
}
func (w *numberWrapper[T]) IsNil() bool {
return w == nil || w.value == nil
}
func (w *numberWrapper[T]) Clone() NumberWrapper {
if w.IsNil() {
return &numberWrapper[T]{}
}
v := *w.value
return &numberWrapper[T]{value: &v}
}
func (w *numberWrapper[T]) EmptySetNil() NumberWrapper {
if w.IsNil() {
return w
}
if *w.value == 0 {
w.value = nil
}
return w
}
func (w *numberWrapper[T]) ToUInt32Value() *wrapperspb.UInt32Value {
if w.IsNil() {
return nil
}
return wrapperspb.UInt32(uint32(*w.value))
}
func (w *numberWrapper[T]) ToUInt64Value() *wrapperspb.UInt64Value {
if w.IsNil() {
return nil
}
return wrapperspb.UInt64(uint64(*w.value))
}
func (w *numberWrapper[T]) ToInt32Value() *wrapperspb.Int32Value {
if w.IsNil() {
return nil
}
return wrapperspb.Int32(int32(*w.value))
}
func (w *numberWrapper[T]) ToInt64Value() *wrapperspb.Int64Value {
if w.IsNil() {
return nil
}
return wrapperspb.Int64(int64(*w.value))
}
func (w *numberWrapper[T]) ToFloatValue() *wrapperspb.FloatValue {
if w.IsNil() {
return nil
}
return wrapperspb.Float(float32(*w.value))
}
func (w *numberWrapper[T]) ToDoubleValue() *wrapperspb.DoubleValue {
if w.IsNil() {
return nil
}
return wrapperspb.Double(float64(*w.value))
}
func (w *numberWrapper[T]) ToStringValue() *wrapperspb.StringValue {
if w.IsNil() {
return nil
}
return wrapperspb.String(numericToStr(*w.value))
}
func numericToStr(n any) string {
switch v := n.(type) {
case int:
return strconv.Itoa(v)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
case uintptr:
return strconv.FormatUint(uint64(v), 10)
case float32:
return strconv.FormatFloat(float64(v), 'g', -1, 32)
case float64:
return strconv.FormatFloat(v, 'g', -1, 64)
default:
return fmt.Sprintf("%v", v)
}
}