-
Notifications
You must be signed in to change notification settings - Fork 10
/
user_persistent_storage_utils.go
90 lines (70 loc) · 1.87 KB
/
user_persistent_storage_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
package statsig
import (
"fmt"
)
type userPersistentStorageUtils struct {
storage IUserPersistentStorage
}
func newUserPersistentStorageUtils(options *Options) *userPersistentStorageUtils {
return &userPersistentStorageUtils{
storage: options.UserPersistentStorage,
}
}
func (p *userPersistentStorageUtils) load(user User, idType string) UserPersistedValues {
if p.storage == nil {
return nil
}
key := getStorageKey(user, idType)
logError := func(err error) {
Logger().LogError(fmt.Sprintf("Failed to load key (%s) from UserPersistentStorage (%s)\n", key, err.Error()))
}
defer func() {
if err := recover(); err != nil {
logError(toError(err))
}
}()
storedValues, exists := p.storage.Load(key)
if !exists {
return nil
}
return storedValues
}
func (p *userPersistentStorageUtils) save(user User, idType string, configName string, evaluation *evalResult) {
if p.storage == nil {
return
}
key := getStorageKey(user, idType)
logError := func(err error) {
Logger().LogError(fmt.Sprintf("Failed to save key (%s) to UserPersistentStorage (%s)\n", key, err.Error()))
}
defer func() {
if err := recover(); err != nil {
logError(toError(err))
}
}()
p.storage.Save(key, configName, evaluation.toStickyValues())
}
func (p *userPersistentStorageUtils) delete(user User, idType string, configName string) {
if p.storage == nil {
return
}
key := getStorageKey(user, idType)
logError := func(err error) {
Logger().LogError(fmt.Sprintf("Failed to save key (%s) to UserPersistentStorage (%s)\n", key, err.Error()))
}
defer func() {
if err := recover(); err != nil {
logError(toError(err))
}
}()
p.storage.Delete(key, configName)
}
func getStorageKey(user User, idType string) string {
var unitID string
if idType == "userID" {
unitID = user.UserID
} else {
unitID = user.CustomIDs[idType]
}
return fmt.Sprintf("%s:%s", unitID, idType)
}