-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclient_option.go
222 lines (189 loc) · 6.23 KB
/
client_option.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package growthbook
import (
"log/slog"
"maps"
"net/http"
"net/url"
"github.com/growthbook/growthbook-golang/internal/condition"
"github.com/growthbook/growthbook-golang/internal/value"
)
type ClientOption func(*Client) error
// WithEnabled sets enabled switch to globally disable all experiments. Default true.
func WithEnabled(enabled bool) ClientOption {
return func(c *Client) error {
c.enabled = enabled
return nil
}
}
// WithApiHost sets the GrowthBook API Host.
func WithApiHost(apiHost string) ClientOption {
return func(c *Client) error {
c.data.apiHost = apiHost
return nil
}
}
// WithClientKey sets client key used to fetch features from the GrowthBook API.
func WithClientKey(clientKey string) ClientOption {
return func(c *Client) error {
c.data.clientKey = clientKey
return nil
}
}
// WithDecryptionKey sets key used to decrypt encrypted features from the API.
func WithDecryptionKey(decryptionKey string) ClientOption {
return func(c *Client) error {
c.data.decryptionKey = decryptionKey
return nil
}
}
// WithAttributes sets attributes that used to assign variations.
func WithAttributes(attributes Attributes) ClientOption {
return func(c *Client) error {
c.attributes = value.Obj(attributes)
return nil
}
}
// WithSavedGroups sets saved groups used to target the same group of users across multiple features and experiments.
func WithSavedGroups(savedGroups condition.SavedGroups) ClientOption {
return func(c *Client) error {
c.data.savedGroups = savedGroups
return nil
}
}
// WithUrl sets url of the current page.
func WithUrl(rawUrl string) ClientOption {
return func(c *Client) error {
url, err := url.Parse(rawUrl)
if err != nil {
return err
}
c.url = url
return nil
}
}
// WithFeatures sets features definitions (usually pulled from an API or cache).
func WithFeatures(features FeatureMap) ClientOption {
return func(c *Client) error {
return c.SetFeatures(features)
}
}
// WithJsonFeatures sets features definitions from JSON string.
func WithJsonFeatures(featuresJson string) ClientOption {
return func(c *Client) error {
return c.SetJSONFeatures(featuresJson)
}
}
// WithEncryptedJsonFeatures sets features definitions from encrypted JSON string.
func WithEncryptedJsonFeatures(featuresJson string) ClientOption {
return func(c *Client) error {
return c.SetEncryptedJSONFeatures(featuresJson)
}
}
// WithForcedVariations force specific experiments to always assign a specific variation (used for QA)
func WithForcedVariations(forcedVariations ForcedVariationsMap) ClientOption {
return func(c *Client) error {
c.forcedVariations = forcedVariations
return nil
}
}
// WithQaMode if true, random assignment is disabled and only explicitly forced variations are used.
func WithQaMode(qaMode bool) ClientOption {
return func(c *Client) error {
c.qaMode = qaMode
return nil
}
}
// WithHttpClient sets http client for GrowthBook API calls.
func WithHttpClient(httpClient *http.Client) ClientOption {
return func(c *Client) error {
c.data.httpClient = httpClient
return nil
}
}
// WithLogger sets logger for GrowthBook client.
func WithLogger(logger *slog.Logger) ClientOption {
return func(c *Client) error {
c.logger = logger
return nil
}
}
// WithExtraData sets extra data that will be to callback calls.
func WithExtraData(extraData any) ClientOption {
return func(c *Client) error {
c.extraData = extraData
return nil
}
}
// WithExperiementCallbaback sets experiment callback function.
func WithExperimentCallback(cb ExperimentCallback) ClientOption {
return func(c *Client) error {
c.experimentCallback = cb
return nil
}
}
// WithFeatureUsageCallback sets feature usage callback function.
func WithFeatureUsageCallback(cb FeatureUsageCallback) ClientOption {
return func(c *Client) error {
c.featureUsageCallback = cb
return nil
}
}
// Child client instance options
// WithEnabled creates child client instance with updated enabled switch.
func (c *Client) WithEnabled(enabled bool) (*Client, error) {
return c.cloneWith(WithEnabled(enabled))
}
// WithQaMode creates child client instance with updated qaMode switch.
func (c *Client) WithQaMode(qaMode bool) (*Client, error) {
return c.cloneWith(WithQaMode(qaMode))
}
// WithLogger creates child client instance that uses provided logger.
func (c *Client) WithLogger(logger *slog.Logger) (*Client, error) {
return c.cloneWith(WithLogger(logger))
}
// WithAttributes creates child client instance that uses provided attributes for evaluation.
func (c *Client) WithAttributes(attributes Attributes) (*Client, error) {
return c.cloneWith(WithAttributes(attributes))
}
// WithAttributeOverrides creates child client instance with updated top-level attributes.
func (c *Client) WithAttributeOverrides(attributes Attributes) (*Client, error) {
newAttrs := maps.Clone(c.attributes)
maps.Copy(newAttrs, value.Obj(attributes))
return c.cloneWith(withValueAttributes(newAttrs))
}
// WithUrl creates child client with updated current page URL.
func (c *Client) WithUrl(rawUrl string) (*Client, error) {
return c.cloneWith(WithUrl(rawUrl))
}
// WithForcedVariations creates child client with updated forced variations.
func (c *Client) WithForcedVariations(forcedVariations ForcedVariationsMap) (*Client, error) {
return c.cloneWith(WithForcedVariations(forcedVariations))
}
// WithExtraData creates child client with extra data that will be sent to a callback.
func (c *Client) WithExtraData(extraData any) (*Client, error) {
return c.cloneWith(WithExtraData(extraData))
}
// WithExperimentCallback creates child client with updated experiment callback function.
func (c *Client) WithExperimentCallback(cb ExperimentCallback) (*Client, error) {
return c.cloneWith(WithExperimentCallback(cb))
}
// WithFeatureUsageCallback creates child client with udpated feature usage callback function.
func (c *Client) WithFeatureUsageCallback(cb FeatureUsageCallback) (*Client, error) {
return c.cloneWith(WithFeatureUsageCallback(cb))
}
func withValueAttributes(value value.ObjValue) ClientOption {
return func(c *Client) error {
c.attributes = value
return nil
}
}
func (c *Client) cloneWith(opts ...ClientOption) (*Client, error) {
clone := c.clone()
for _, opt := range opts {
err := opt(clone)
if err != nil {
return nil, err
}
}
return clone, nil
}