-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilt_in_fields_uints.go
141 lines (127 loc) · 4.28 KB
/
built_in_fields_uints.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
package croconf //nolint:dupl
import "strconv"
func uintValHelper(sources []UintValueBinder, bitSize int, saveToDest func(uint64)) func(sourceNum int) Binding {
return func(sourceNum int) Binding {
var val uint64
binding := sources[sourceNum].BindUintValueTo(&val)
return wrapBinding(binding, func() error {
if err := binding.Apply(); err != nil {
return err
}
if err := checkUintBitsize(val, bitSize); err != nil {
return err
}
saveToDest(val)
return nil
})
}
}
func NewUintField(dest *uint, sources ...UintValueBinder) Field {
return newField(dest, len(sources), uintValHelper(sources, strconv.IntSize, func(val uint64) {
*dest = uint(val) // this is safe, uintValHelper checks val against bitSize
}))
}
func NewUint8Field(dest *uint8, sources ...UintValueBinder) Field {
return newField(dest, len(sources), uintValHelper(sources, 8, func(val uint64) {
*dest = uint8(val) // this is safe, uintValHelper checks val against bitSize
}))
}
func NewUint16Field(dest *uint16, sources ...UintValueBinder) Field {
return newField(dest, len(sources), uintValHelper(sources, 16, func(val uint64) {
*dest = uint16(val) // this is safe, uintValHelper checks val against bitSize
}))
}
func NewUint32Field(dest *uint32, sources ...UintValueBinder) Field {
return newField(dest, len(sources), uintValHelper(sources, 32, func(val uint64) {
*dest = uint32(val) // this is safe, uintValHelper checks val against bitSize
}))
}
func NewUint64Field(dest *uint64, sources ...UintValueBinder) Field {
return newField(dest, len(sources), func(sourceNum int) Binding {
return sources[sourceNum].BindUintValueTo(dest)
})
}
func uintSliceHandler(newTypedSlice func(int) (add func(uint64) error, save func())) arrayHandler {
return func(arrLength int, getElement func(int) LazySingleValueBinder) error {
add, save := newTypedSlice(arrLength)
for i := 0; i < arrLength; i++ {
var val uint64
elBinding := getElement(i).BindUintValueTo(&val)
if err := elBinding.Apply(); err != nil {
return err
}
if err := add(val); err != nil {
return err
}
}
save()
return nil
}
}
func NewUintSliceField(dest *[]uint, sources ...ArrayValueBinder) Field {
return newArrayField(dest, sources, uintSliceHandler(func(arrLength int) (func(uint64) error, func()) {
newArr := make([]uint, 0, arrLength)
add := func(val uint64) error {
if err := checkUintBitsize(val, strconv.IntSize); err != nil {
return err
}
newArr = append(newArr, uint(val)) // this is safe
return nil
}
save := func() { *dest = newArr }
return add, save
}))
}
func NewUint8SliceField(dest *[]uint8, sources ...ArrayValueBinder) Field {
return newArrayField(dest, sources, uintSliceHandler(func(arrLength int) (func(uint64) error, func()) {
newArr := make([]uint8, 0, arrLength)
add := func(val uint64) error {
if err := checkUintBitsize(val, 8); err != nil {
return err
}
newArr = append(newArr, uint8(val)) // this is safe
return nil
}
save := func() { *dest = newArr }
return add, save
}))
}
func NewUint16SliceField(dest *[]uint16, sources ...ArrayValueBinder) Field {
return newArrayField(dest, sources, uintSliceHandler(func(arrLength int) (func(uint64) error, func()) {
newArr := make([]uint16, 0, arrLength)
add := func(val uint64) error {
if err := checkUintBitsize(val, 16); err != nil {
return err
}
newArr = append(newArr, uint16(val)) // this is safe
return nil
}
save := func() { *dest = newArr }
return add, save
}))
}
func NewUint32SliceField(dest *[]uint32, sources ...ArrayValueBinder) Field {
return newArrayField(dest, sources, uintSliceHandler(func(arrLength int) (func(uint64) error, func()) {
newArr := make([]uint32, 0, arrLength)
add := func(val uint64) error {
if err := checkUintBitsize(val, 32); err != nil {
return err
}
newArr = append(newArr, uint32(val)) // this is safe
return nil
}
save := func() { *dest = newArr }
return add, save
}))
}
func NewUint64SliceField(dest *[]uint64, sources ...ArrayValueBinder) Field {
return newArrayField(dest, sources, uintSliceHandler(func(arrLength int) (func(uint64) error, func()) {
newArr := make([]uint64, 0, arrLength)
add := func(val uint64) error {
newArr = append(newArr, val)
return nil
}
save := func() { *dest = newArr }
return add, save
}))
}