@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"reflect"
24
24
"runtime/debug"
25
+ "strconv"
25
26
26
27
"github.com/optimizely/go-sdk/optimizely/event"
27
28
@@ -152,19 +153,58 @@ func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserConte
152
153
return nil
153
154
}
154
155
156
+ // GetFeatureVariableBoolean returns boolean feature variable value
157
+ func (o * OptimizelyClient ) GetFeatureVariableBoolean (featureKey string , variableKey string , userContext entities.UserContext ) (value bool , err error ) {
158
+ val , valueType , err := o .getFeatureVariable (featureKey , variableKey , userContext )
159
+ if err != nil {
160
+ return false , err
161
+ }
162
+ convertedValue , err := strconv .ParseBool (val )
163
+ if err != nil || valueType != entities .Boolean {
164
+ return false , fmt .Errorf ("Variable value for key %s is invalid or wrong type" , variableKey )
165
+ }
166
+ return convertedValue , err
167
+ }
168
+
169
+ // GetFeatureVariableDouble returns double feature variable value
170
+ func (o * OptimizelyClient ) GetFeatureVariableDouble (featureKey string , variableKey string , userContext entities.UserContext ) (value float64 , err error ) {
171
+ val , valueType , err := o .getFeatureVariable (featureKey , variableKey , userContext )
172
+ if err != nil {
173
+ return 0 , err
174
+ }
175
+ convertedValue , err := strconv .ParseFloat (val , 64 )
176
+ if err != nil || valueType != entities .Double {
177
+ return 0 , fmt .Errorf ("Variable value for key %s is invalid or wrong type" , variableKey )
178
+ }
179
+ return convertedValue , err
180
+ }
181
+
182
+ // GetFeatureVariableInteger returns integer feature variable value
183
+ func (o * OptimizelyClient ) GetFeatureVariableInteger (featureKey string , variableKey string , userContext entities.UserContext ) (value int , err error ) {
184
+ val , valueType , err := o .getFeatureVariable (featureKey , variableKey , userContext )
185
+ if err != nil {
186
+ return 0 , err
187
+ }
188
+ convertedValue , err := strconv .Atoi (val )
189
+ if err != nil || valueType != entities .Integer {
190
+ return 0 , fmt .Errorf ("Variable value for key %s is invalid or wrong type" , variableKey )
191
+ }
192
+ return convertedValue , err
193
+ }
194
+
155
195
// GetFeatureVariableString returns string feature variable value
156
196
func (o * OptimizelyClient ) GetFeatureVariableString (featureKey string , variableKey string , userContext entities.UserContext ) (value string , err error ) {
157
197
value , valueType , err := o .getFeatureVariable (featureKey , variableKey , userContext )
158
198
if err != nil {
159
199
return "" , err
160
200
}
161
- if valueType != "string" {
201
+ if valueType != entities . String {
162
202
return "" , fmt .Errorf ("Variable value for key %s is wrong type" , variableKey )
163
203
}
164
204
return value , err
165
205
}
166
206
167
- func (o * OptimizelyClient ) getFeatureVariable (featureKey string , variableKey string , userContext entities.UserContext ) (value string , valueType string , err error ) {
207
+ func (o * OptimizelyClient ) getFeatureVariable (featureKey string , variableKey string , userContext entities.UserContext ) (value string , valueType entities. VariableType , err error ) {
168
208
169
209
defer func () {
170
210
if r := recover (); r != nil {
0 commit comments