Skip to content

Commit

Permalink
Variable model always returns type and defaultValue fields. remove un…
Browse files Browse the repository at this point in the history
…used reasonUsingDefault. update type enum from value to use fromClass
  • Loading branch information
kaushalkapasi committed Feb 9, 2023
1 parent 85401bd commit 5dc2cc2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.devcycle.sdk.server.common.api.IDVCApi;
import com.devcycle.sdk.server.common.exception.DVCException;
import com.devcycle.sdk.server.common.model.*;
import com.devcycle.sdk.server.common.model.Variable.TypeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -64,18 +65,21 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
throw new IllegalArgumentException("defaultValue cannot be null");
}

TypeEnum variableType = TypeEnum.fromClass(defaultValue.getClass());
Variable<T> variable;

try {
Call<Variable> response = api.getVariableByKey(user, key, dvcOptions.getEnableEdgeDB());
variable = getResponse(response);
variable.setIsDefaulted(false);
} catch (Exception exception) {
variable = (Variable<T>) Variable.builder()
.key(key)
.value(defaultValue)
.isDefaulted(true)
.reasonUsingDefaultValue(exception.getMessage())
.build();
.key(key)
.type(variableType)
.value(defaultValue)
.defaultValue(defaultValue)
.isDefaulted(true)
.build();
}
return variable;
}
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/devcycle/sdk/server/common/model/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.devcycle.sdk.server.common.model;

import java.util.LinkedHashMap;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -29,7 +30,8 @@
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Variable<T> {
@Schema(required = true, description = "unique database id")
@Schema(required = false, description = "unique database id")
@JsonIgnore
@JsonProperty("_id")
private String id;

Expand All @@ -42,13 +44,12 @@ public class Variable<T> {
@Schema(required = true, description = "Variable type")
private TypeEnum type;

@JsonIgnore
@Schema(required = true, description = "Variable default value")
private T defaultValue;

@Builder.Default
private Boolean isDefaulted = false;

@JsonIgnore
private String reasonUsingDefaultValue;

public enum TypeEnum {
STRING("String"),
BOOLEAN("Boolean"),
Expand All @@ -71,13 +72,18 @@ public String toString() {
return String.valueOf(value);
}

public static TypeEnum fromValue(String text) {
for (TypeEnum b : TypeEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
public static TypeEnum fromClass(Class<?> clazz) {
if (clazz == LinkedHashMap.class) {
return JSON;
} else if (clazz == Boolean.class) {
return BOOLEAN;
} else if (clazz == Integer.class || clazz == Double.class || clazz == Float.class) {
return NUMBER;
} else if (clazz == String.class) {
return STRING;
} else {
return null;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import com.devcycle.sdk.server.common.model.*;
import com.devcycle.sdk.server.common.model.Variable.TypeEnum;
import com.devcycle.sdk.server.local.bucketing.LocalBucketing;
import com.devcycle.sdk.server.local.managers.EnvironmentConfigManager;
import com.devcycle.sdk.server.local.managers.EventQueueManager;
Expand Down Expand Up @@ -89,17 +90,24 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
System.out.println("Variable called before DVCClient has initialized, returning default value");
}

TypeEnum variableType = TypeEnum.fromClass(defaultValue.getClass());
Variable<T> defaultVariable = (Variable<T>) Variable.builder()
.key(key)
.type(variableType)
.value(defaultValue)
.defaultValue(defaultValue)
.isDefaulted(true)
.reasonUsingDefaultValue("Variable not found")
.build();

if (!isInitialized) {
return defaultVariable;
}

try {
BucketedUserConfig bucketedUserConfig = localBucketing.generateBucketedConfig(serverKey, user);
if (bucketedUserConfig.variables.containsKey(key)) {
Variable<T> variable = bucketedUserConfig.variables.get(key);
variable.setDefaultValue(defaultValue);
variable.setIsDefaulted(false);
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableEvaluated").target(key).build(), bucketedUserConfig);
return variable;
Expand Down

0 comments on commit 5dc2cc2

Please sign in to comment.