Skip to content

Commit 3458198

Browse files
authored
fix: change FeatureVariable type to string for forward compatibility (#370)
1 parent 683613e commit 3458198

File tree

11 files changed

+100
-120
lines changed

11 files changed

+100
-120
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public Boolean getFeatureVariableBoolean(@Nonnull String featureKey,
458458
variableKey,
459459
userId,
460460
attributes,
461-
FeatureVariable.VariableType.BOOLEAN
461+
FeatureVariable.BOOLEAN_TYPE
462462
);
463463
}
464464

@@ -501,7 +501,7 @@ public Double getFeatureVariableDouble(@Nonnull String featureKey,
501501
variableKey,
502502
userId,
503503
attributes,
504-
FeatureVariable.VariableType.DOUBLE
504+
FeatureVariable.DOUBLE_TYPE
505505
);
506506
} catch (Exception exception) {
507507
logger.error("NumberFormatException while trying to parse \"" + variableValue +
@@ -551,7 +551,7 @@ public Integer getFeatureVariableInteger(@Nonnull String featureKey,
551551
variableKey,
552552
userId,
553553
attributes,
554-
FeatureVariable.VariableType.INTEGER
554+
FeatureVariable.INTEGER_TYPE
555555
);
556556

557557
} catch (Exception exception) {
@@ -598,15 +598,15 @@ public String getFeatureVariableString(@Nonnull String featureKey,
598598
variableKey,
599599
userId,
600600
attributes,
601-
FeatureVariable.VariableType.STRING);
601+
FeatureVariable.STRING_TYPE);
602602
}
603603

604604
@VisibleForTesting
605605
<T> T getFeatureVariableValueForType(@Nonnull String featureKey,
606606
@Nonnull String variableKey,
607607
@Nonnull String userId,
608608
@Nonnull Map<String, ?> attributes,
609-
@Nonnull FeatureVariable.VariableType variableType) {
609+
@Nonnull String variableType) {
610610
if (featureKey == null) {
611611
logger.warn("The featureKey parameter must be nonnull.");
612612
return null;
@@ -691,29 +691,31 @@ <T> T getFeatureVariableValueForType(@Nonnull String featureKey,
691691

692692
// Helper method which takes type and variable value and convert it to object to use in Listener DecisionInfo object variable value
693693
@VisibleForTesting
694-
Object convertStringToType(String variableValue, FeatureVariable.VariableType type) {
694+
Object convertStringToType(String variableValue, String type) {
695695
if (variableValue != null) {
696696
switch (type) {
697-
case DOUBLE:
697+
case FeatureVariable.DOUBLE_TYPE:
698698
try {
699699
return Double.parseDouble(variableValue);
700700
} catch (NumberFormatException exception) {
701701
logger.error("NumberFormatException while trying to parse \"" + variableValue +
702702
"\" as Double. " + exception);
703703
}
704704
break;
705-
case STRING:
705+
case FeatureVariable.STRING_TYPE:
706706
return variableValue;
707-
case BOOLEAN:
707+
case FeatureVariable.BOOLEAN_TYPE:
708708
return Boolean.parseBoolean(variableValue);
709-
case INTEGER:
709+
case FeatureVariable.INTEGER_TYPE:
710710
try {
711711
return Integer.parseInt(variableValue);
712712
} catch (NumberFormatException exception) {
713713
logger.error("NumberFormatException while trying to parse \"" + variableValue +
714714
"\" as Integer. " + exception.toString());
715715
}
716716
break;
717+
default:
718+
return variableValue;
717719
}
718720
}
719721

core-api/src/main/java/com/optimizely/ab/config/FeatureVariable.java

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,15 @@ public static VariableStatus fromString(String variableStatusString) {
6161
}
6262
}
6363

64-
public enum VariableType {
65-
@SerializedName("boolean")
66-
BOOLEAN("boolean"),
67-
68-
@SerializedName("integer")
69-
INTEGER("integer"),
70-
71-
@SerializedName("string")
72-
STRING("string"),
73-
74-
@SerializedName("double")
75-
DOUBLE("double");
76-
77-
private final String variableType;
78-
79-
VariableType(String variableType) {
80-
this.variableType = variableType;
81-
}
82-
83-
@JsonValue
84-
public String getVariableType() {
85-
return variableType;
86-
}
87-
88-
public static VariableType fromString(String variableTypeString) {
89-
if (variableTypeString != null) {
90-
for (VariableType variableTypeEnum : VariableType.values()) {
91-
if (variableTypeString.equals(variableTypeEnum.getVariableType())) {
92-
return variableTypeEnum;
93-
}
94-
}
95-
}
96-
97-
return null;
98-
}
99-
100-
@Override
101-
public String toString() {
102-
return variableType;
103-
}
104-
}
64+
public static final String STRING_TYPE = "string";
65+
public static final String INTEGER_TYPE = "integer";
66+
public static final String DOUBLE_TYPE = "double";
67+
public static final String BOOLEAN_TYPE = "boolean";
10568

10669
private final String id;
10770
private final String key;
10871
private final String defaultValue;
109-
private final VariableType type;
72+
private final String type;
11073
@Nullable
11174
private final VariableStatus status;
11275

@@ -115,7 +78,7 @@ public FeatureVariable(@JsonProperty("id") String id,
11578
@JsonProperty("key") String key,
11679
@JsonProperty("defaultValue") String defaultValue,
11780
@JsonProperty("status") VariableStatus status,
118-
@JsonProperty("type") VariableType type) {
81+
@JsonProperty("type") String type) {
11982
this.id = id;
12083
this.key = key;
12184
this.defaultValue = defaultValue;
@@ -140,7 +103,7 @@ public String getDefaultValue() {
140103
return defaultValue;
141104
}
142105

143-
public VariableType getType() {
106+
public String getType() {
144107
return type;
145108
}
146109

@@ -165,7 +128,7 @@ public boolean equals(Object o) {
165128
if (!id.equals(variable.id)) return false;
166129
if (!key.equals(variable.key)) return false;
167130
if (!defaultValue.equals(variable.defaultValue)) return false;
168-
if (type != variable.type) return false;
131+
if (!type.equals(variable.type)) return false;
169132
return status == variable.status;
170133
}
171134

core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private List<FeatureVariable> parseFeatureVariables(JSONArray featureVariablesJs
344344
String id = FeatureVariableObject.getString("id");
345345
String key = FeatureVariableObject.getString("key");
346346
String defaultValue = FeatureVariableObject.getString("defaultValue");
347-
FeatureVariable.VariableType type = FeatureVariable.VariableType.fromString(FeatureVariableObject.getString("type"));
347+
String type = FeatureVariableObject.getString("type");
348348
FeatureVariable.VariableStatus status = null;
349349
if (FeatureVariableObject.has("status")) {
350350
status = FeatureVariable.VariableStatus.fromString(FeatureVariableObject.getString("status"));

core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.optimizely.ab.config.*;
2020
import com.optimizely.ab.config.Experiment.ExperimentStatus;
2121
import com.optimizely.ab.config.FeatureVariable.VariableStatus;
22-
import com.optimizely.ab.config.FeatureVariable.VariableType;
2322
import com.optimizely.ab.config.audience.Audience;
2423
import com.optimizely.ab.config.audience.AudienceIdCondition;
2524
import com.optimizely.ab.config.audience.Condition;
@@ -335,7 +334,7 @@ private List<FeatureVariable> parseFeatureVariables(JSONArray featureVariablesJs
335334
String id = (String) featureVariableObject.get("id");
336335
String key = (String) featureVariableObject.get("key");
337336
String defaultValue = (String) featureVariableObject.get("defaultValue");
338-
VariableType type = VariableType.fromString((String) featureVariableObject.get("type"));
337+
String type = (String) featureVariableObject.get("type");
339338
VariableStatus status = VariableStatus.fromString((String) featureVariableObject.get("status"));
340339

341340
featureVariables.add(new FeatureVariable(id, key, defaultValue, status, type));

core-api/src/main/java/com/optimizely/ab/notification/DecisionNotification.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public static class FeatureVariableDecisionNotificationBuilder {
244244
private Boolean featureEnabled;
245245
private FeatureDecision featureDecision;
246246
private String variableKey;
247-
private FeatureVariable.VariableType variableType;
247+
private String variableType;
248248
private Object variableValue;
249249
private String userId;
250250
private Map<String, ?> attributes;
@@ -283,7 +283,7 @@ public FeatureVariableDecisionNotificationBuilder withVariableKey(String variabl
283283
return this;
284284
}
285285

286-
public FeatureVariableDecisionNotificationBuilder withVariableType(FeatureVariable.VariableType variableType) {
286+
public FeatureVariableDecisionNotificationBuilder withVariableType(String variableType) {
287287
this.variableType = variableType;
288288
return this;
289289
}

core-api/src/main/java/com/optimizely/ab/optimizelyconfig/OptimizelyConfigService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Map<String, OptimizelyVariable> getMergedVariablesMap(Variation variation, Strin
130130
featureVariableKeyMap.put(featureVariable.getKey(), new OptimizelyVariable(
131131
featureVariable.getId(),
132132
featureVariable.getKey(),
133-
featureVariable.getType().getVariableType().toLowerCase(),
133+
featureVariable.getType(),
134134
variation.getFeatureEnabled() && tempVariableIdMap.get(featureVariable.getId()) != null
135135
? tempVariableIdMap.get(featureVariable.getId()).getValue()
136136
: featureVariable.getDefaultValue()
@@ -205,7 +205,7 @@ Map<String, OptimizelyVariable> getFeatureVariablesMap(List<FeatureVariable> fea
205205
featureVariableKeyMap.put(featureVariable.getKey(), new OptimizelyVariable(
206206
featureVariable.getId(),
207207
featureVariable.getKey(),
208-
featureVariable.getType().getVariableType().toLowerCase(),
208+
featureVariable.getType(),
209209
featureVariable.getDefaultValue()
210210
));
211211
}

0 commit comments

Comments
 (0)