-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
154 lines (135 loc) · 3.63 KB
/
utils.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package pagser
import (
"encoding/json"
"fmt"
"reflect"
"github.com/spf13/cast"
)
// toInt32Slice casts an interface to a []int type.
func toInt32Slice(i interface{}) []int32 {
v, _ := toInt32SliceE(i)
return v
}
// toInt32SliceE casts an interface to a []int type.
func toInt32SliceE(i interface{}) ([]int32, error) {
if i == nil {
return []int32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
switch v := i.(type) {
case []int32:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]int32, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := cast.ToInt32E(s.Index(j).Interface())
if err != nil {
return []int32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
a[j] = val
}
return a, nil
default:
return []int32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
}
// toInt64Slice casts an interface to a []int type.
func toInt64Slice(i interface{}) []int64 {
v, _ := toInt64SliceE(i)
return v
}
// toInt64SliceE casts an interface to a []int type.
func toInt64SliceE(i interface{}) ([]int64, error) {
if i == nil {
return []int64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
switch v := i.(type) {
case []int64:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]int64, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := cast.ToInt64E(s.Index(j).Interface())
if err != nil {
return []int64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
a[j] = val
}
return a, nil
default:
return []int64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
}
// toFloat32Slice casts an interface to a []int type.
func toFloat32Slice(i interface{}) []float32 {
v, _ := toFloat32SliceE(i)
return v
}
// toFloat32SliceE casts an interface to a []int type.
func toFloat32SliceE(i interface{}) ([]float32, error) {
if i == nil {
return []float32{}, fmt.Errorf("unable to cast %#v of type %T to []float32", i, i)
}
switch v := i.(type) {
case []float32:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]float32, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := cast.ToFloat32E(s.Index(j).Interface())
if err != nil {
return []float32{}, fmt.Errorf("unable to cast %#v of type %T to []float32", i, i)
}
a[j] = val
}
return a, nil
default:
return []float32{}, fmt.Errorf("unable to cast %#v of type %T to []float32", i, i)
}
}
// toFloat64Slice casts an interface to a []int type.
func toFloat64Slice(i interface{}) []float64 {
v, _ := toFloat64SliceE(i)
return v
}
// toFloat64SliceE casts an interface to a []int type.
func toFloat64SliceE(i interface{}) ([]float64, error) {
if i == nil {
return []float64{}, fmt.Errorf("unable to cast %#v of type %T to []float64", i, i)
}
switch v := i.(type) {
case []float64:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]float64, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := cast.ToFloat64E(s.Index(j).Interface())
if err != nil {
return []float64{}, fmt.Errorf("unable to cast %#v of type %T to []float64", i, i)
}
a[j] = val
}
return a, nil
default:
return []float64{}, fmt.Errorf("unable to cast %#v of type %T to []float64", i, i)
}
}
func prettyJson(v interface{}) string {
bytes, _ := json.MarshalIndent(v, "", "\t")
return string(bytes)
}