-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeveloper_field.go
More file actions
77 lines (67 loc) · 1.38 KB
/
developer_field.go
File metadata and controls
77 lines (67 loc) · 1.38 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
package fit
import (
"math"
"reflect"
)
// DeveloperField holds the value of a developer field and associated metadata to allow for dynamic parsing.
type DeveloperField struct {
DeveloperDataIndex uint8
FieldDefinitionNumber uint8
BaseTypeId FitBaseType
FieldName string
Units string
value reflect.Value
}
func first(v reflect.Value) reflect.Value {
switch v.Kind() {
case reflect.Slice,
reflect.Array:
if v.Len() > 0 {
return v.Index(0)
}
}
return v
}
func getUint64(v reflect.Value) uint64 {
switch v.Kind() {
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
return v.Uint()
default:
return math.MaxUint64
}
}
func getFloat64(v reflect.Value) float64 {
switch v.Kind() {
case reflect.Float32,
reflect.Float64:
return v.Float()
default:
return math.NaN()
}
}
func (f *DeveloperField) Uint64() uint64 {
return getUint64(first(f.value))
}
func (f *DeveloperField) Float64() float64 {
return getFloat64(first(f.value))
}
func (f *DeveloperField) Uint64Slice() []uint64 {
var values []uint64
switch f.value.Kind() {
case reflect.Slice:
for i := 0; i < f.value.Len(); i++ {
v := getUint64(f.value.Index(i))
values = append(values, v)
}
default:
v := getUint64(f.value)
if v != math.MaxUint64 {
values = append(values, v)
}
}
return values
}