Skip to content

Commit 1f0e341

Browse files
authored
feature variable accessor apis (#133)
* link feature variable accessors to feature variable by value and type * add unit tests for getFeatureVariable calling through to internals * add unit tests to catch parsing error
1 parent e41b2a0 commit 1f0e341

File tree

2 files changed

+500
-0
lines changed

2 files changed

+500
-0
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ public void track(@Nonnull String eventName,
383383
@Nonnull String variableKey,
384384
@Nonnull String userId,
385385
@Nonnull Map<String, String> attributes) {
386+
String variableValue = getFeatureVariableValueForType(
387+
featureKey,
388+
variableKey,
389+
userId,
390+
attributes,
391+
LiveVariable.VariableType.BOOLEAN
392+
);
393+
if (variableValue != null) {
394+
return Boolean.parseBoolean(variableValue);
395+
}
386396
return null;
387397
}
388398

@@ -413,6 +423,22 @@ public void track(@Nonnull String eventName,
413423
@Nonnull String variableKey,
414424
@Nonnull String userId,
415425
@Nonnull Map<String, String> attributes) {
426+
String variableValue = getFeatureVariableValueForType(
427+
featureKey,
428+
variableKey,
429+
userId,
430+
attributes,
431+
LiveVariable.VariableType.DOUBLE
432+
);
433+
if (variableValue != null) {
434+
try {
435+
return Double.parseDouble(variableValue);
436+
}
437+
catch (NumberFormatException exception) {
438+
logger.error("NumberFormatException while trying to parse \"" + variableValue +
439+
"\" as Double. " + exception);
440+
}
441+
}
416442
return null;
417443
}
418444

@@ -443,6 +469,22 @@ public void track(@Nonnull String eventName,
443469
@Nonnull String variableKey,
444470
@Nonnull String userId,
445471
@Nonnull Map<String, String> attributes) {
472+
String variableValue = getFeatureVariableValueForType(
473+
featureKey,
474+
variableKey,
475+
userId,
476+
attributes,
477+
LiveVariable.VariableType.INTEGER
478+
);
479+
if (variableValue != null) {
480+
try {
481+
return Integer.parseInt(variableValue);
482+
}
483+
catch (NumberFormatException exception) {
484+
logger.error("NumberFormatException while trying to parse \"" + variableValue +
485+
"\" as Integer. " + exception.toString());
486+
}
487+
}
446488
return null;
447489
}
448490

0 commit comments

Comments
 (0)