-
Notifications
You must be signed in to change notification settings - Fork 156
/
helpers.go
165 lines (140 loc) · 4.19 KB
/
helpers.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
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2017 by authors and contributors.
*/
package datadog
import (
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
)
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
// GetBool is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetBool(v *bool) (bool, bool) {
if v != nil {
return *v, true
}
return false, false
}
// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }
// Int64 is a helper routine that allocates a new int64 value to
// store v and return a pointer to it.
func Int64(v int64) *int64 { return &v }
// GetIntOk is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetIntOk(v *int) (int, bool) {
if v != nil {
return *v, true
}
return 0, false
}
// Float64 is a helper routine that allocates a new float64 value
// to store v and returns a pointer to it.
func Float64(v float64) *float64 { return &v }
// GetFloat64Ok is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetFloat64Ok(v *float64) (float64, bool) {
if v != nil {
return *v, true
}
return 0, false
}
// Float64AlmostEqual will return true if two floats are within a certain tolerance of each other
func Float64AlmostEqual(a, b, tolerance float64) bool {
return math.Abs(a-b) < tolerance
}
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }
// GetStringOk is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetStringOk(v *string) (string, bool) {
if v != nil {
return *v, true
}
return "", false
}
// JsonNumber is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func JsonNumber(v json.Number) *json.Number { return &v }
// GetJsonNumberOk is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetJsonNumberOk(v *json.Number) (json.Number, bool) {
if v != nil {
return *v, true
}
return "", false
}
// Precision is a helper routine that allocates a new precision value
// to store v and returns a pointer to it.
func Precision(v PrecisionT) *PrecisionT { return &v }
// GetPrecision is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetPrecision(v *PrecisionT) (PrecisionT, bool) {
if v != nil {
return *v, true
}
return PrecisionT(""), false
}
// GetStringId is a helper routine that allows screenboards and timeboards to be retrieved
// by either the legacy numerical format or the new string format.
// It returns the id as is if it is a string, converts it to a string if it is an integer.
// It return an error if the type is neither string or an integer
func GetStringId(id interface{}) (string, error) {
switch v := id.(type) {
case int:
return strconv.Itoa(v), nil
case string:
return v, nil
default:
return "", errors.New("unsupported id type")
}
}
func GetFloatFromInterface(intf *interface{}) (*float64, bool, error) {
var result *float64
var auto bool
if intf != nil {
val := *intf
switch tp := val.(type) {
case float32:
fv := float64(val.(float32))
result = &fv
case float64:
fv := val.(float64)
result = &fv
case int:
fv := float64(val.(int))
result = &fv
case int32:
fv := float64(val.(int32))
result = &fv
case int64:
fv := float64(val.(int64))
result = &fv
case string:
fv := val.(string)
if fv == "auto" {
auto = true
} else {
f, err := strconv.ParseFloat(fv, 64)
if err != nil {
return nil, false, err
}
result = &f
}
default:
return nil, false, fmt.Errorf(`bad type "%v" for Yaxis.min, expected "auto" or a number`, tp)
}
}
return result, auto, nil
}