-
Notifications
You must be signed in to change notification settings - Fork 3
/
int32.go
136 lines (126 loc) · 2.83 KB
/
int32.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) nilToInt32(Converter, NilValue, *int32) error {
return nil
}
func (stdRecipes) intToInt32(c Converter, in int, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) int8ToInt32(c Converter, in int8, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) int16ToInt32(c Converter, in int16, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) int32ToInt32(c Converter, in int32, out *int32) error {
*out = in
return nil
}
func (stdRecipes) int64ToInt32(c Converter, in int64, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) uintToInt32(c Converter, in uint, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) uint8ToInt32(c Converter, in uint8, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) uint16ToInt32(c Converter, in uint16, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) uint32ToInt32(c Converter, in uint32, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) uint64ToInt32(c Converter, in uint64, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) boolToInt32(c Converter, in bool, out *int32) error {
switch in {
case true:
*out = 1
default:
*out = 0
}
return nil
}
func (stdRecipes) float32ToInt32(c Converter, in float32, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) float64ToInt32(c Converter, in float64, out *int32) error {
*out = int32(in)
return nil
}
func (stdRecipes) stringToInt32(c Converter, in string, out *int32) error {
if in == "" {
*out = 0
return nil
}
i, err := strconv.ParseInt(in, 0, 32)
if err != nil {
return err
}
*out = int32(i)
return nil
}
func (stdRecipes) timeToInt32(c Converter, in time.Time, out *int32) error {
*out = int32(in.Unix())
return nil
}
func (s stdRecipes) structToInt32(c Converter, in StructValue, out *int32) error {
err := s.baseStructToInt32(c, in.Value, out)
if err == nil {
return err
}
// test for *struct.Int32()
v := reflect.New(in.Type())
v.Elem().Set(in.Value)
if s.baseStructToInt32(c, v, out) == nil {
return nil
}
return err
}
type toInt32 interface {
Int32() int32
}
type toInt32WithErr interface {
Int32() (int32, error)
}
func (s stdRecipes) baseStructToInt32(_ Converter, in reflect.Value, out *int32) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
// check for struct.Int32()
if i, ok := in.Interface().(toInt32); ok {
*out = i.Int32()
return nil
}
if i, ok := in.Interface().(toInt32WithErr); ok {
var err error
*out, err = i.Int32()
return err
}
if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = int32(i)
return nil
}
return fmt.Errorf("%s has no Int32() function", in.Type().String())
}