-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
57 lines (49 loc) · 1.64 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
package eppoclient
import (
"errors"
"fmt"
"strconv"
"time"
)
type dictionary map[string]interface{}
type testData struct {
Experiment string `json:"experiment"`
ValueType string `json:"valueType"`
PercentExposure float32 `json:"percentExposure"`
Variations []testDataVariations `json:"variations"`
Subjects []string `json:"subjects"`
SubjectsWithAttributes []subjectWithAttributes `json:"subjectsWithAttributes"`
ExpectedAssignments []Value `json:"expectedAssignments"`
}
type subjectWithAttributes struct {
SubjectKey string `json:"subjectKey"`
SubjectAttributes dictionary `json:"subjectAttributes"`
}
type testDataVariations struct {
Name string `json:"name"`
ShardRange testDataShardRange `json:"shardRange"`
}
type testDataShardRange struct {
Start int `json:"start"`
End int `json:"end"`
}
// ToFloat64 attempts to convert an interface{} value to a float64.
// It supports inputs of type float64 or string (which can be parsed as float64).
// Returns a float64 and nil error on success, or 0 and an error on failure.
func ToFloat64(val interface{}) (float64, error) {
switch v := val.(type) {
case float64:
return v, nil
case string:
floatVal, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, fmt.Errorf("cannot convert string '%s' to float64: %w", v, err)
}
return floatVal, nil
default:
return 0, errors.New("value is neither a float64 nor a convertible string")
}
}
func TimeNow() string {
return time.Now().UTC().Format(time.RFC3339)
}