-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsdk_configs.go
87 lines (75 loc) · 1.53 KB
/
sdk_configs.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
package statsig
import (
"strconv"
"sync"
)
type SDKConfigs struct {
flags map[string]bool
configs map[string]interface{}
mu sync.RWMutex
}
func newSDKConfigs() *SDKConfigs {
return &SDKConfigs{
flags: make(map[string]bool),
configs: make(map[string]interface{}),
}
}
func (s *SDKConfigs) SetFlags(newFlags map[string]bool) {
s.mu.Lock()
s.flags = newFlags
s.mu.Unlock()
}
func (s *SDKConfigs) SetConfigs(newConfigs map[string]interface{}) {
s.mu.Lock()
s.configs = newConfigs
s.mu.Unlock()
}
func (s *SDKConfigs) On(key string) (bool, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
val, exists := s.flags[key]
return val, exists
}
func (s *SDKConfigs) GetConfigNumValue(config string) (float64, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
value, exists := s.configs[config]
if !exists {
return 0, false
}
return getNumericValue(value)
}
func (s *SDKConfigs) GetConfigIntValue(config string) (int, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
value, exists := s.configs[config]
if !exists {
return 0, false
}
switch v := value.(type) {
case int:
return v, true
case float64:
return int(v), true
default:
return 0, false
}
}
func (s *SDKConfigs) GetConfigStrValue(config string) (string, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
value, exists := s.configs[config]
if !exists {
return "", false
}
switch v := value.(type) {
case string:
return v, true
case int:
return strconv.Itoa(v), true
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), true
default:
return "", false
}
}