-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantity.go
More file actions
320 lines (295 loc) · 6.82 KB
/
quantity.go
File metadata and controls
320 lines (295 loc) · 6.82 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package quant
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
)
// Quantity stores a physical quantity in the base unit for its dimension.
//
// The dimension type parameter is a phantom type used only for compile-time
// safety.
type Quantity[D any] struct {
value float64
}
// New constructs a quantity from a value expressed in the provided unit.
func New[D any, U Unit[D]](v float64, u U) Quantity[D] {
return Quantity[D]{value: u.toBase(v)}
}
// To converts the quantity to the provided unit and returns the scalar value.
func (q Quantity[D]) To(u Unit[D]) float64 {
return u.fromBase(q.value)
}
// Value converts the quantity to the provided unit and returns the scalar value.
func (q Quantity[D]) Value(u Unit[D]) float64 {
return q.To(u)
}
// MarshalJSON encodes the quantity as its scalar value in the base unit for
// the dimension.
func (q Quantity[D]) MarshalJSON() ([]byte, error) {
return json.Marshal(q.value)
}
// UnmarshalJSON decodes a quantity from a scalar value in the base unit for
// the dimension.
func (q *Quantity[D]) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &q.value)
}
// MarshalText encodes the quantity as a base-unit scalar value.
func (q Quantity[D]) MarshalText() ([]byte, error) {
return []byte(strconv.FormatFloat(q.value, 'g', 12, 64)), nil
}
// UnmarshalText decodes a base-unit scalar value into the quantity.
func (q *Quantity[D]) UnmarshalText(text []byte) error {
v, err := strconv.ParseFloat(string(text), 64)
if err != nil {
return err
}
q.value = v
return nil
}
// Format formats the quantity in the provided unit.
func (q Quantity[D]) Format(u Unit[D]) string {
value := strconv.FormatFloat(q.To(u), 'g', 12, 64)
symbol := unitSymbol(u)
if symbol == "" {
return value
}
return value + " " + symbol
}
// SQLQuantity wraps a quantity for database/sql interop using base-unit scalar values.
type SQLQuantity[D any] struct {
Quantity[D]
}
// SQL wraps a quantity in a database-friendly adapter.
func SQL[D any](q Quantity[D]) SQLQuantity[D] {
return SQLQuantity[D]{Quantity: q}
}
// Value implements database/sql/driver.Valuer using the base-unit scalar value.
func (q SQLQuantity[D]) Value() (driver.Value, error) {
return q.Quantity.value, nil
}
// Scan implements database/sql.Scanner using a base-unit scalar value.
func (q *SQLQuantity[D]) Scan(src any) error {
v, err := scanFloat64(src)
if err != nil {
return err
}
q.Quantity.value = v
return nil
}
// String formats the quantity in the base unit for its dimension.
func (q Quantity[D]) String() string {
u := baseUnit[D]()
if u == nil {
return strconv.FormatFloat(q.value, 'g', 12, 64)
}
return q.Format(u)
}
// Add adds two quantities of the same dimension.
func (q Quantity[D]) Add(other Quantity[D]) Quantity[D] {
return Quantity[D]{value: q.value + other.value}
}
// Sub subtracts another quantity of the same dimension.
func (q Quantity[D]) Sub(other Quantity[D]) Quantity[D] {
return Quantity[D]{value: q.value - other.value}
}
func baseUnitSymbol[D any]() string {
return unitSymbol(baseUnit[D]())
}
func baseUnit[D any]() Unit[D] {
switch any(*new(D)).(type) {
case Mass:
return any(Kilogram).(Unit[D])
case Length:
return any(Meter).(Unit[D])
case Area:
return any(SquareMeter).(Unit[D])
case Volume:
return any(CubicMeter).(Unit[D])
case VolumeFlowRate:
return any(CubicMeterPerSecond).(Unit[D])
case Temperature:
return any(Kelvin).(Unit[D])
case Time:
return any(Second).(Unit[D])
case Frequency:
return any(Hertz).(Unit[D])
case Speed:
return any(MeterPerSecond).(Unit[D])
case Torque:
return any(NewtonMeter).(Unit[D])
case Pace:
return any(SecondPerMeter).(Unit[D])
case Pressure:
return any(Pascal).(Unit[D])
case Digital:
return any(Bit).(Unit[D])
case Illuminance:
return any(Lux).(Unit[D])
case PartsPer:
return any(Ratio).(Unit[D])
case Voltage:
return any(Volt).(Unit[D])
case Current:
return any(Ampere).(Unit[D])
case Power:
return any(Watt).(Unit[D])
case ApparentPower:
return any(VoltAmpere).(Unit[D])
case ReactivePower:
return any(VoltAmpereReactive).(Unit[D])
case Energy:
return any(Joule).(Unit[D])
case ReactiveEnergy:
return any(VoltAmpereReactiveHour).(Unit[D])
case Angle:
return any(Radian).(Unit[D])
case Charge:
return any(Coulomb).(Unit[D])
case Force:
return any(Newton).(Unit[D])
case Acceleration:
return any(MeterPerSecondSquared).(Unit[D])
case Pieces:
return any(Piece).(Unit[D])
default:
var zero Unit[D]
return zero
}
}
func unitSymbol(u any) string {
switch any(u) {
case any(Microgram):
return "mcg"
case any(Milligram):
return "mg"
case any(Gram):
return "g"
case any(Kilogram):
return "kg"
case any(Ounce):
return "oz"
case any(Pound):
return "lb"
case any(Stone):
return "st"
case any(MetricTon):
return "mt"
case any(Tonne):
return "t"
case any(Nanometer):
return "nm"
case any(Micrometer):
return "um"
case any(Millimeter):
return "mm"
case any(Centimeter):
return "cm"
case any(Meter):
return "m"
case any(Inch):
return "in"
case any(Yard):
return "yd"
case any(USFoot):
return "ft-us"
case any(Foot):
return "ft"
case any(Fathom):
return "fathom"
case any(Kilometer):
return "km"
case any(Mile):
return "mi"
case any(NauticalMile):
return "nMi"
case any(Celsius):
return "C"
case any(Fahrenheit):
return "F"
case any(Kelvin):
return "K"
case any(Rankine):
return "R"
case any(Second):
return "s"
case any(Minute):
return "min"
case any(Hour):
return "h"
case any(Liter):
return "l"
case any(LiterPerSecond):
return "l/s"
case any(LiterPerMinute):
return "l/min"
case any(KilometerPerHour):
return "km/h"
case any(MilePerHour):
return "mph"
case any(MeterPerSecond):
return "m/s"
case any(NewtonMeter):
return "Nm"
case any(PoundForceFoot):
return "lbf-ft"
case any(Pascal):
return "Pa"
case any(Bar):
return "bar"
case any(PSI):
return "psi"
case any(Bit):
return "bit"
case any(Byte):
return "byte"
case any(Kibibyte):
return "KiB"
case any(Watt):
return "W"
case any(Kilowatt):
return "kW"
case any(Joule):
return "J"
case any(Radian):
return "rad"
case any(Degree):
return "deg"
case any(Coulomb):
return "C"
case any(Newton):
return "N"
case any(MeterPerSecondSquared):
return "m/s2"
case any(GForce):
return "g"
case any(Piece):
return "pcs"
case any(Ratio):
return "ratio"
default:
return ""
}
}
func scanFloat64(src any) (float64, error) {
switch v := src.(type) {
case nil:
return 0, nil
case float64:
return v, nil
case float32:
return float64(v), nil
case int64:
return float64(v), nil
case int32:
return float64(v), nil
case int:
return float64(v), nil
case []byte:
return strconv.ParseFloat(string(v), 64)
case string:
return strconv.ParseFloat(v, 64)
default:
return 0, fmt.Errorf("quant: unsupported scan type %T", src)
}
}