-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
157 lines (138 loc) · 3.61 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
155
156
157
package nse
import (
"errors"
"fmt"
"math"
"github.com/golang/glog"
)
func getStrField(
records map[string]interface{},
field string) (string, error) {
fieldInt, ok := records[field]
if !ok {
msg := fmt.Sprintf("Parsing OC records failed. Field %s not found.",
field)
glog.Error(msg)
return "", errors.New(msg)
}
value, ok := fieldInt.(string)
if !ok {
msg := fmt.Sprintf("Parsing OC records failed."+
"Field %s is not of string type.", field)
glog.Error(msg)
return "", errors.New(msg)
}
return value, nil
}
func getFloat64Field(
records map[string]interface{},
field string) (float64, error) {
fieldValue, ok := records[field]
if !ok {
msg := fmt.Sprintf("Parsing records failed. Field %s not found.", field)
glog.Error(msg)
return 0, errors.New(msg)
}
value, ok := fieldValue.(float64)
if !ok {
msg := fmt.Sprintf("Parsing records failed."+
"Field %s is not of float64 type.", field)
glog.Error(msg)
return 0, errors.New(msg)
}
return value, nil
}
func getIntField(
records map[string]interface{},
field string) (int, error) {
fieldValue, ok := records[field]
if !ok {
msg := fmt.Sprintf("Parsing records failed. Field %s not found.", field)
glog.Error(msg)
return 0, errors.New(msg)
}
value, ok := fieldValue.(int)
if !ok {
msg := fmt.Sprintf("Parsing field=%s failed. "+
"Expected int type found %T type.", field, fieldValue)
glog.Error(msg, value)
return 0, errors.New(msg)
}
return value, nil
}
func getArrayField(
records map[string]interface{},
field string) ([]interface{}, error) {
fieldInt, ok := records[field]
if !ok {
msg := fmt.Sprintf("Parsing OC records failed. Field %s not found.",
field)
return []interface{}{}, errors.New(msg)
}
value, ok := fieldInt.([]interface{})
if !ok {
msg := fmt.Sprintf("Parsing OC records failed."+
"Field %s is not of array type.", field)
return []interface{}{}, errors.New(msg)
}
return value, nil
}
func getStringToInterfaceMap(
records map[string]interface{},
field string) (result map[string]interface{}, err error) {
fieldInt, ok := records[field]
if !ok {
msg := fmt.Sprintf("Field %s not found in the records.", field)
glog.Error(msg)
return result, errors.New(msg)
}
result, ok = fieldInt.(map[string]interface{})
if !ok {
msg := fmt.Sprintf("Unexpected field %s type", field)
glog.Error(msg)
return result, errors.New(msg)
}
return result, nil
}
func convertToStringSlice(data []interface{}) []string {
result := make([]string, len(data))
for i, v := range data {
if str, ok := v.(string); ok {
result[i] = str
} else {
// Handle the case where the element is not a string
// You can choose to skip, ignore, or perform some other action
result[i] = ""
glog.Info(fmt.Sprintf("Value %v is not string.", v))
}
}
return result
}
func convertToIntSlice(data []interface{}) []int32 {
result := make([]int32, len(data))
for i, v := range data {
if str, ok := v.(int32); ok {
result[i] = str
} else {
// Handle the case where the element is not a string
// You can choose to skip, ignore, or perform some other action
result[i] = 0
glog.Info(fmt.Sprintf("Value %v is not integer.", v))
}
}
return result
}
func roundToStep(num float64, step int32) int32 {
// round the input number to the nearest integer
rounded := int32(math.Round(num))
// calculate the remainder when divided by 50
remainder := rounded % step
// calculate the difference from the nearest multiple of 50
diff := step - remainder
// adjust the rounded number based on the difference
if remainder <= (step / 2) {
return rounded - remainder
} else {
return rounded + diff
}
}