-
Notifications
You must be signed in to change notification settings - Fork 3
/
uint32.go
136 lines (126 loc) · 2.9 KB
/
uint32.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
package convert
import (
"errors"
"fmt"
"reflect"
"strconv"
"time"
)
func (stdRecipes) nilToUint32(Converter, NilValue, *uint32) error {
return nil
}
func (stdRecipes) intToUint32(c Converter, in int, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) int8ToUint32(c Converter, in int8, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) int16ToUint32(c Converter, in int16, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) int32ToUint32(c Converter, in int32, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) int64ToUint32(c Converter, in int64, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) uintToUint32(c Converter, in uint, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) uint8ToUint32(c Converter, in uint8, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) uint16ToUint32(c Converter, in uint16, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) uint32ToUint32(c Converter, in uint32, out *uint32) error {
*out = in
return nil
}
func (stdRecipes) uint64ToUint32(c Converter, in uint64, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) boolToUint32(c Converter, in bool, out *uint32) error {
switch in {
case true:
*out = 1
default:
*out = 0
}
return nil
}
func (stdRecipes) float32ToUint32(c Converter, in float32, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) float64ToUint32(c Converter, in float64, out *uint32) error {
*out = uint32(in)
return nil
}
func (stdRecipes) stringToUint32(c Converter, in string, out *uint32) error {
if in == "" {
*out = 0
return nil
}
i, err := strconv.ParseUint(in, 0, 32)
if err != nil {
return err
}
*out = uint32(i)
return nil
}
func (stdRecipes) timeToUint32(c Converter, in time.Time, out *uint32) error {
*out = uint32(in.Unix())
return nil
}
func (s stdRecipes) structToUint32(c Converter, in StructValue, out *uint32) error {
err := s.baseStructToUint32(c, in.Value, out)
if err == nil {
return err
}
// test for *struct.Uint32()
v := reflect.New(in.Type())
v.Elem().Set(in.Value)
if s.baseStructToUint32(c, v, out) == nil {
return nil
}
return err
}
type toUint32 interface {
Uint32() uint32
}
type toUint32WithErr interface {
Uint32() (uint32, error)
}
func (s stdRecipes) baseStructToUint32(_ Converter, in reflect.Value, out *uint32) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
// check for struct.Uint32()
if i, ok := in.Interface().(toUint32); ok {
*out = i.Uint32()
return nil
}
if i, ok := in.Interface().(toUint32WithErr); ok {
var err error
*out, err = i.Uint32()
return err
}
if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = uint32(i)
return nil
}
return fmt.Errorf("%s has no Uint32() function", in.Type().String())
}