forked from skydive-project/skydive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
336 lines (298 loc) · 7.3 KB
/
types.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package common
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"reflect"
"strconv"
"strings"
"time"
)
var (
// ErrCantCompareInterface error can't compare inteface
ErrCantCompareInterface = errors.New("Can't compare interface")
// ErrFieldNotFound error field not found
ErrFieldNotFound = errors.New("Field not found")
// ErrFieldWrongType error field has wrong type
ErrFieldWrongType = errors.New("Field has wrong type")
)
// SortOrder describes ascending or descending order
type SortOrder string
const (
// SortAscending sorting order
SortAscending SortOrder = "ASC"
// SortDescending sorting order
SortDescending SortOrder = "DESC"
)
// ToInt64 Convert all number like type to int64
func ToInt64(i interface{}) (int64, error) {
switch v := i.(type) {
case json.Number:
if i, err := v.Int64(); err == nil {
return i, nil
}
if f, err := v.Float64(); err == nil {
return int64(f), nil
}
case string:
return strconv.ParseInt(v, 10, 64)
case int:
return int64(v), nil
case uint:
return int64(v), nil
case int32:
return int64(v), nil
case uint32:
return int64(v), nil
case int64:
return v, nil
case uint64:
return int64(v), nil
case float32:
return int64(v), nil
case float64:
return int64(v), nil
}
return 0, fmt.Errorf("failed to convert to an integer: %v", i)
}
func integerCompare(a interface{}, b interface{}) (int, error) {
n1, err := ToInt64(a)
if err != nil {
return 0, err
}
n2, err := ToInt64(b)
if err != nil {
return 0, err
}
if n1 == n2 {
return 0, nil
} else if n1 < n2 {
return -1, nil
} else {
return 1, nil
}
}
// ToFloat64 Convert all number like type to float64
func ToFloat64(f interface{}) (float64, error) {
switch v := f.(type) {
case json.Number:
if i, err := v.Int64(); err == nil {
return float64(i), nil
}
if f, err := v.Float64(); err == nil {
return f, nil
}
case string:
return strconv.ParseFloat(v, 64)
case int, uint, int32, uint32, int64, uint64:
i, err := ToInt64(f)
if err != nil {
return 0, err
}
return float64(i), nil
case float32:
return float64(v), nil
case float64:
return f.(float64), nil
}
return 0, fmt.Errorf("not a float: %v", f)
}
func floatCompare(a interface{}, b interface{}) (int, error) {
f1, err := ToFloat64(a)
if err != nil {
return 0, err
}
f2, err := ToFloat64(b)
if err != nil {
return 0, err
}
if f1 == f2 {
return 0, nil
} else if f1 < f2 {
return -1, nil
} else {
return 1, nil
}
}
// CrossTypeCompare compare 2 differents number types like Float64 vs Float32
func CrossTypeCompare(a interface{}, b interface{}) (int, error) {
switch a.(type) {
case float32, float64:
return floatCompare(a, b)
}
switch b.(type) {
case float32, float64:
return floatCompare(a, b)
}
switch a.(type) {
case int, uint, int32, uint32, int64, uint64:
return integerCompare(a, b)
default:
return 0, ErrCantCompareInterface
}
}
// CrossTypeEqual compare 2 differents number types
func CrossTypeEqual(a interface{}, b interface{}) bool {
result, err := CrossTypeCompare(a, b)
if err == ErrCantCompareInterface {
return a == b
} else if err != nil {
return false
}
return result == 0
}
// MinInt64 return the lowest value
func MinInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
// MaxInt64 return the biggest value
func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
// IPv6Supported return true if the platform support IPv6
func IPv6Supported() bool {
if _, err := os.Stat("/proc/net/if_inet6"); os.IsNotExist(err) {
return false
}
data, err := ioutil.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6")
if err != nil {
return false
}
if strings.TrimSpace(string(data)) == "1" {
return false
}
return true
}
// IPToString convert IPv4 or IPv6 to a string
func IPToString(ip net.IP) string {
if ip.To4() == nil {
return "[" + ip.String() + "]"
}
return ip.String()
}
// JSONDecode wrapper to UseNumber during JSON decoding
func JSONDecode(r io.Reader, i interface{}) error {
decoder := json.NewDecoder(r)
decoder.UseNumber()
return decoder.Decode(i)
}
// UnixMillis return the current time in miliseconds
func UnixMillis(t time.Time) int64 {
return t.UTC().UnixNano() / 1000000
}
// TimeSlice define a time boudary values
type TimeSlice struct {
Start int64 `json:"Start"`
Last int64 `json:"Last"`
}
// NewTimeSlice create a new TimeSlice based on Start and Last
func NewTimeSlice(s, l int64) *TimeSlice {
return &TimeSlice{Start: s, Last: l}
}
// Metric define accessors
type Metric interface {
GetFieldInt64(field string) (int64, error)
Add(m Metric) Metric
}
// TimedMetric define Metric during a time slice
type TimedMetric struct {
TimeSlice
Metric Metric
}
// GetFieldInt64 return the field value
func (tm *TimedMetric) GetFieldInt64(field string) (int64, error) {
return tm.Metric.GetFieldInt64(field)
}
// MarshalJSON serialized a TimedMetric in JSON
func (tm *TimedMetric) MarshalJSON() ([]byte, error) {
var s string
if tm.Metric != nil {
b, err := json.Marshal(tm.Metric)
if err != nil {
return nil, err
}
s = fmt.Sprintf(`{"Start":%d,"Last":%d,%s`, tm.Start, tm.Last, string(b[1:]))
} else {
s = "null"
}
return []byte(s), nil
}
// SetField set a value in a tree based on dot key ("a.b.c.d" = "ok")
func SetField(obj map[string]interface{}, k string, v interface{}) bool {
components := strings.Split(k, ".")
for n, component := range components {
if n == len(components)-1 {
obj[component] = v
} else {
m, ok := obj[component]
if !ok {
m := make(map[string]interface{})
obj[component] = m
obj = m
} else if obj, ok = m.(map[string]interface{}); !ok {
return false
}
}
}
return true
}
// GetField retrieve a value from a tree from the dot key like "a.b.c.d"
func GetField(obj map[string]interface{}, k string) (interface{}, error) {
components := strings.Split(k, ".")
for n, component := range components {
i, ok := obj[component]
if !ok {
return nil, ErrFieldNotFound
}
if n == len(components)-1 {
return i, nil
}
if list, ok := i.([]interface{}); ok {
var results []interface{}
for _, v := range list {
switch v := v.(type) {
case map[string]interface{}:
if obj, err := GetField(v, strings.Join(components[n+1:], ".")); err == nil {
results = append(results, obj)
}
}
}
return results, nil
}
if obj, ok = i.(map[string]interface{}); !ok {
return nil, fmt.Errorf("%s is not a map, but a %+v", component, reflect.TypeOf(obj))
}
}
return obj, nil
}