-
Notifications
You must be signed in to change notification settings - Fork 3
/
uint16.go
136 lines (126 loc) · 2.9 KB
/
uint16.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) nilToUint16(Converter, NilValue, *uint16) error {
return nil
}
func (stdRecipes) intToUint16(c Converter, in int, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) int8ToUint16(c Converter, in int8, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) int16ToUint16(c Converter, in int16, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) int32ToUint16(c Converter, in int32, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) int64ToUint16(c Converter, in int64, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) uintToUint16(c Converter, in uint, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) uint8ToUint16(c Converter, in uint8, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) uint16ToUint16(c Converter, in uint16, out *uint16) error {
*out = in
return nil
}
func (stdRecipes) uint32ToUint16(c Converter, in uint32, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) uint64ToUint16(c Converter, in uint64, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) boolToUint16(c Converter, in bool, out *uint16) error {
switch in {
case true:
*out = 1
default:
*out = 0
}
return nil
}
func (stdRecipes) float32ToUint16(c Converter, in float32, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) float64ToUint16(c Converter, in float64, out *uint16) error {
*out = uint16(in)
return nil
}
func (stdRecipes) stringToUint16(c Converter, in string, out *uint16) error {
if in == "" {
*out = 0
return nil
}
i, err := strconv.ParseUint(in, 0, 16)
if err != nil {
return err
}
*out = uint16(i)
return nil
}
func (stdRecipes) timeToUint16(c Converter, in time.Time, out *uint16) error {
*out = uint16(in.Unix())
return nil
}
func (s stdRecipes) structToUint16(c Converter, in StructValue, out *uint16) error {
err := s.baseStructToUint16(c, in.Value, out)
if err == nil {
return err
}
// test for *struct.Uint16()
v := reflect.New(in.Type())
v.Elem().Set(in.Value)
if s.baseStructToUint16(c, v, out) == nil {
return nil
}
return err
}
type toUint16 interface {
Uint16() uint16
}
type toUint16WithErr interface {
Uint16() (uint16, error)
}
func (s stdRecipes) baseStructToUint16(_ Converter, in reflect.Value, out *uint16) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
// check for struct.Uint16()
if i, ok := in.Interface().(toUint16); ok {
*out = i.Uint16()
return nil
}
if i, ok := in.Interface().(toUint16WithErr); ok {
var err error
*out, err = i.Uint16()
return err
}
if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = uint16(i)
return nil
}
return fmt.Errorf("%s has no Uint16() function", in.Type().String())
}