-
Notifications
You must be signed in to change notification settings - Fork 3
/
int16.go
136 lines (126 loc) · 2.83 KB
/
int16.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) nilToInt16(Converter, NilValue, *int16) error {
return nil
}
func (stdRecipes) intToInt16(c Converter, in int, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) int8ToInt16(c Converter, in int8, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) int16ToInt16(c Converter, in int16, out *int16) error {
*out = in
return nil
}
func (stdRecipes) int32ToInt16(c Converter, in int32, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) int64ToInt16(c Converter, in int64, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) uintToInt16(c Converter, in uint, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) uint8ToInt16(c Converter, in uint8, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) uint16ToInt16(c Converter, in uint16, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) uint32ToInt16(c Converter, in uint32, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) uint64ToInt16(c Converter, in uint64, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) boolToInt16(c Converter, in bool, out *int16) error {
switch in {
case true:
*out = 1
default:
*out = 0
}
return nil
}
func (stdRecipes) float32ToInt16(c Converter, in float32, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) float64ToInt16(c Converter, in float64, out *int16) error {
*out = int16(in)
return nil
}
func (stdRecipes) stringToInt16(c Converter, in string, out *int16) error {
if in == "" {
*out = 0
return nil
}
i, err := strconv.ParseInt(in, 0, 16)
if err != nil {
return err
}
*out = int16(i)
return nil
}
func (stdRecipes) timeToInt16(c Converter, in time.Time, out *int16) error {
*out = int16(in.Unix())
return nil
}
func (s stdRecipes) structToInt16(c Converter, in StructValue, out *int16) error {
err := s.baseStructToInt16(c, in.Value, out)
if err == nil {
return err
}
// test for *struct.Int16()
v := reflect.New(in.Type())
v.Elem().Set(in.Value)
if s.baseStructToInt16(c, v, out) == nil {
return nil
}
return err
}
type toInt16 interface {
Int16() int16
}
type toInt16WithErr interface {
Int16() (int16, error)
}
func (s stdRecipes) baseStructToInt16(_ Converter, in reflect.Value, out *int16) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
// check for struct.Int16()
if i, ok := in.Interface().(toInt16); ok {
*out = i.Int16()
return nil
}
if i, ok := in.Interface().(toInt16WithErr); ok {
var err error
*out, err = i.Int16()
return err
}
if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = int16(i)
return nil
}
return fmt.Errorf("%s has no Int16() function", in.Type().String())
}