Skip to content

Commit 55690db

Browse files
yasirfolio3Michael Ng
authored andcommitted
feat(getFeatureVariable): Add getFeatureVariableBoolean, getFeatureVariableDouble and getFeatureVariableInteger API (#78)
1 parent 984923b commit 55690db

File tree

4 files changed

+904
-24
lines changed

4 files changed

+904
-24
lines changed

optimizely/client/client.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"reflect"
2424
"runtime/debug"
25+
"strconv"
2526

2627
"github.com/optimizely/go-sdk/optimizely/event"
2728

@@ -152,19 +153,58 @@ func (o *OptimizelyClient) Track(eventKey string, userContext entities.UserConte
152153
return nil
153154
}
154155

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+
155195
// GetFeatureVariableString returns string feature variable value
156196
func (o *OptimizelyClient) GetFeatureVariableString(featureKey string, variableKey string, userContext entities.UserContext) (value string, err error) {
157197
value, valueType, err := o.getFeatureVariable(featureKey, variableKey, userContext)
158198
if err != nil {
159199
return "", err
160200
}
161-
if valueType != "string" {
201+
if valueType != entities.String {
162202
return "", fmt.Errorf("Variable value for key %s is wrong type", variableKey)
163203
}
164204
return value, err
165205
}
166206

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) {
168208

169209
defer func() {
170210
if r := recover(); r != nil {

0 commit comments

Comments
 (0)