-
Notifications
You must be signed in to change notification settings - Fork 3
/
uint8.go
137 lines (126 loc) · 2.84 KB
/
uint8.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
package convert
import (
"errors"
"fmt"
"reflect"
"strconv"
"time"
)
func (stdRecipes) nilToUint8(Converter, NilValue, *uint8) error {
return nil
}
func (stdRecipes) intToUint8(c Converter, in int, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) int8ToUint8(c Converter, in int8, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) int16ToUint8(c Converter, in int16, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) int32ToUint8(c Converter, in int32, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) int64ToUint8(c Converter, in int64, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) uintToUint8(c Converter, in uint, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) uint8ToUint8(c Converter, in uint8, out *uint8) error {
*out = in
return nil
}
func (stdRecipes) uint16ToUint8(c Converter, in uint16, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) uint32ToUint8(c Converter, in uint32, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) uint64ToUint8(c Converter, in uint64, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) boolToUint8(c Converter, in bool, out *uint8) error {
switch in {
case true:
*out = 1
default:
*out = 0
}
return nil
}
func (stdRecipes) float32ToUint8(c Converter, in float32, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) float64ToUint8(c Converter, in float64, out *uint8) error {
*out = uint8(in)
return nil
}
func (stdRecipes) stringToUint8(c Converter, in string, out *uint8) error {
if in == "" {
*out = 0
return nil
}
i, err := strconv.ParseUint(in, 0, 8)
if err != nil {
return err
}
*out = uint8(i)
return nil
}
func (stdRecipes) timeToUint8(c Converter, in time.Time, out *uint8) error {
*out = uint8(in.Unix())
return nil
}
func (s stdRecipes) structToUint8(c Converter, in StructValue, out *uint8) error {
err := s.baseStructToUint8(c, in.Value, out)
if err == nil {
return err
}
// test for *struct.Uint8()
v := reflect.New(in.Type())
v.Elem().Set(in.Value)
if s.baseStructToUint8(c, v, out) == nil {
return nil
}
return err
}
type toUint8 interface {
Uint8() uint8
}
type toUint8WithErr interface {
Uint8() (uint8, error)
}
func (s stdRecipes) baseStructToUint8(_ Converter, in reflect.Value, out *uint8) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
// check for struct.Uint8()
if i, ok := in.Interface().(toUint8); ok {
*out = i.Uint8()
return nil
}
if i, ok := in.Interface().(toUint8WithErr); ok {
var err error
*out, err = i.Uint8()
return err
}
if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = uint8(i)
return nil
}
return fmt.Errorf("%s has no Uint8() function", in.Type().String())
}