diff --git a/src/main/java/com/devcycle/sdk/server/cloud/api/DVCCloudClient.java b/src/main/java/com/devcycle/sdk/server/cloud/api/DVCCloudClient.java index b23e8f1b..f14e66d6 100755 --- a/src/main/java/com/devcycle/sdk/server/cloud/api/DVCCloudClient.java +++ b/src/main/java/com/devcycle/sdk/server/cloud/api/DVCCloudClient.java @@ -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; @@ -64,18 +65,21 @@ public Variable variable(User user, String key, T defaultValue) { throw new IllegalArgumentException("defaultValue cannot be null"); } + TypeEnum variableType = TypeEnum.fromClass(defaultValue.getClass()); Variable variable; try { Call response = api.getVariableByKey(user, key, dvcOptions.getEnableEdgeDB()); variable = getResponse(response); + variable.setIsDefaulted(false); } catch (Exception exception) { variable = (Variable) 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; } diff --git a/src/main/java/com/devcycle/sdk/server/common/model/Variable.java b/src/main/java/com/devcycle/sdk/server/common/model/Variable.java index 30397783..a6ab3f5f 100755 --- a/src/main/java/com/devcycle/sdk/server/common/model/Variable.java +++ b/src/main/java/com/devcycle/sdk/server/common/model/Variable.java @@ -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; @@ -29,7 +30,8 @@ @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public class Variable { - @Schema(required = true, description = "unique database id") + @Schema(required = false, description = "unique database id") + @JsonIgnore @JsonProperty("_id") private String id; @@ -42,13 +44,12 @@ public class Variable { @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"), @@ -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; } } } diff --git a/src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java b/src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java index b97dbb05..2a49c5f3 100755 --- a/src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java +++ b/src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java @@ -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; @@ -89,17 +90,24 @@ public Variable 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 defaultVariable = (Variable) 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 variable = bucketedUserConfig.variables.get(key); + variable.setDefaultValue(defaultValue); variable.setIsDefaulted(false); eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableEvaluated").target(key).build(), bucketedUserConfig); return variable;