-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathclient.go
171 lines (141 loc) · 3.67 KB
/
client.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
package clienttest
import (
"sync"
sdk "github.com/configcat/go-sdk/v8"
)
// NewClient creates enough of the ConfigCat client to record flag interactions.
func NewClient() *Client {
return &Client{}
}
type Client struct {
mu sync.Mutex
requests []Request
boolEvaluation func(req Request) sdk.BoolEvaluationDetails
stringEvaluation func(req Request) sdk.StringEvaluationDetails
floatEvaluation func(req Request) sdk.FloatEvaluationDetails
intEvaluation func(req Request) sdk.IntEvaluationDetails
}
type Request struct {
Key string
DefaultValue interface{}
User sdk.User
}
func (r *Request) UserData() sdk.UserData {
userData, ok := r.User.(*sdk.UserData)
if !ok {
panic("user is not of type sdk.UserData")
}
return *userData
}
func (c *Client) Reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.requests = nil
c.boolEvaluation = nil
c.stringEvaluation = nil
c.floatEvaluation = nil
c.intEvaluation = nil
}
func (c *Client) GetRequests() []Request {
c.mu.Lock()
defer c.mu.Unlock()
requests := make([]Request, len(c.requests))
copy(requests, c.requests)
return requests
}
func (c *Client) WithBoolEvaluation(eval func(req Request) sdk.BoolEvaluationDetails) {
c.mu.Lock()
defer c.mu.Unlock()
c.boolEvaluation = eval
}
func (c *Client) WithStringEvaluation(eval func(req Request) sdk.StringEvaluationDetails) {
c.mu.Lock()
defer c.mu.Unlock()
c.stringEvaluation = eval
}
func (c *Client) WithFloatEvaluation(eval func(req Request) sdk.FloatEvaluationDetails) {
c.mu.Lock()
defer c.mu.Unlock()
c.floatEvaluation = eval
}
func (c *Client) WithIntEvaluation(eval func(req Request) sdk.IntEvaluationDetails) {
c.mu.Lock()
defer c.mu.Unlock()
c.intEvaluation = eval
}
func (c *Client) GetBoolValueDetails(key string, defaultValue bool, user sdk.User) sdk.BoolEvaluationDetails {
c.mu.Lock()
defer c.mu.Unlock()
req := Request{
Key: key,
DefaultValue: defaultValue,
User: user,
}
c.requests = append(c.requests, req)
if c.boolEvaluation == nil {
return sdk.BoolEvaluationDetails{
Data: evalNotFound(key, user),
Value: defaultValue,
}
}
return c.boolEvaluation(req)
}
func (c *Client) GetStringValueDetails(key string, defaultValue string, user sdk.User) sdk.StringEvaluationDetails {
c.mu.Lock()
defer c.mu.Unlock()
req := Request{
Key: key,
DefaultValue: defaultValue,
User: user,
}
c.requests = append(c.requests, req)
if c.stringEvaluation == nil {
return sdk.StringEvaluationDetails{
Data: evalNotFound(key, user),
Value: defaultValue,
}
}
return c.stringEvaluation(req)
}
func (c *Client) GetFloatValueDetails(key string, defaultValue float64, user sdk.User) sdk.FloatEvaluationDetails {
c.mu.Lock()
defer c.mu.Unlock()
req := Request{
Key: key,
DefaultValue: defaultValue,
User: user,
}
c.requests = append(c.requests, req)
if c.floatEvaluation == nil {
return sdk.FloatEvaluationDetails{
Data: evalNotFound(key, user),
Value: defaultValue,
}
}
return c.floatEvaluation(req)
}
func (c *Client) GetIntValueDetails(key string, defaultValue int, user sdk.User) sdk.IntEvaluationDetails {
c.mu.Lock()
defer c.mu.Unlock()
req := Request{
Key: key,
DefaultValue: defaultValue,
User: user,
}
c.requests = append(c.requests, req)
if c.intEvaluation == nil {
return sdk.IntEvaluationDetails{
Data: evalNotFound(key, user),
Value: defaultValue,
}
}
return c.intEvaluation(req)
}
func evalNotFound(key string, user sdk.User) sdk.EvaluationDetailsData {
return sdk.EvaluationDetailsData{
Key: key,
User: user,
IsDefaultValue: true,
Error: sdk.ErrKeyNotFound{Key: key},
}
}