Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ install the sdk in using maven
<dependency>
<groupId>co.featbit</groupId>
<artifactId>Featbit-Java-SDK</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
</dependencies>
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>co.featbit</groupId>
<artifactId>featbit-java-sdk</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>

<name>featbit/featbit-java-sdk</name>

Expand Down
53 changes: 9 additions & 44 deletions src/main/java/co/featbit/commons/model/EvalDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,19 @@
* @param <T> - String/Boolean/Numeric Type
*/
public final class EvalDetail<T> implements Serializable {

private static final String NO_VARIATION = "NE";

private final T variation;

private final String id;

private final String reason;

private final String name;

private final String keyName;

private EvalDetail(T variation,
String id,
String reason,
String keyName,
String name) {
this.variation = variation;
this.id = id;
this.reason = reason;
this.keyName = keyName;
this.name = name;
Expand All @@ -43,27 +36,25 @@ private EvalDetail(T variation,
/**
* build method, this method is only for internal use
*
* @param variation
* @param id
* @param reason
* @param keyName
* @param name
* @param variation the result of flag value
* @param reason main factor that influenced the flag evaluation value
* @param keyName key name of the flag
* @param name name of the flag
* @param <T> String/Boolean/Numeric Type
* @return an EvalDetail
*/
public static <T> EvalDetail<T> of(T variation,
String id,
String reason,
String keyName,
String name) {
return new EvalDetail<>(variation, id, reason, keyName, name);
return new EvalDetail<>(variation, reason, keyName, name);
}

/**
* build the method from a json string, this method is only for internal use
*
* @param json
* @param cls
* @param json json string of an EvalDetail
* @param cls raw type of flag value
* @param <T> String/Boolean/Numeric Type
* @return an EvalDetail
*/
Expand All @@ -82,16 +73,6 @@ public T getVariation() {
return variation;
}

/**
* The id of the returned value within the flag's list of variations
* In fact this value is an index, this value is only for internal use
*
* @return a integer value
*/
public String getId() {
return id;
}

/**
* get the reason that evaluate the flag value.
*
Expand Down Expand Up @@ -119,16 +100,6 @@ public String getKeyName() {
return keyName;
}

/**
* Returns true if the flag evaluation returned a good value,
* false if the default value returned
*
* @return Returns true if the flag evaluation returned a good value, false if the default value returned
*/
public boolean isSuccess() {
return !id.equals(NO_VARIATION);
}

/**
* object converted to json string
*
Expand All @@ -143,27 +114,21 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EvalDetail<?> that = (EvalDetail<?>) o;
return id == that.id && Objects.equals(variation, that.variation) && Objects.equals(reason, that.reason) && Objects.equals(name, that.name) && Objects.equals(keyName, that.keyName);
return Objects.equals(variation, that.variation) && Objects.equals(reason, that.reason) && Objects.equals(name, that.name) && Objects.equals(keyName, that.keyName);
}

@Override
public int hashCode() {
return Objects.hash(variation, id, reason, name, keyName);
return Objects.hash(variation, reason, name, keyName);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("variation", variation)
.add("id", id)
.add("reason", reason)
.add("name", name)
.add("keyName", keyName)
.toString();
}

public FlagState<T> toFlagState() {
return FlagState.of(this);
}

}
2 changes: 1 addition & 1 deletion src/main/java/co/featbit/commons/model/FBUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class FBUser implements Serializable {

private final static Function<FBUser, String> USERNAME = u -> u.userName;
private final static Function<FBUser, String> KEY = u -> u.key;
private final static Map<String, Function<FBUser, String>> BUILTINS = ImmutableMap.of("name", USERNAME, "keyid", KEY);
private final static Map<String, Function<FBUser, String>> BUILTINS = ImmutableMap.of("name", USERNAME, "keyid", KEY, "key", KEY);
private final String userName;
private final String key;
private final Map<String, String> custom;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/co/featbit/commons/model/FlagState.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ private FlagState(boolean success, String message, EvalDetail<T> data) {
* @param <T> String/Boolean/Numeric Type
* @return a FlagState
*/
public static <T> FlagState<T> of(EvalDetail<T> data) {
return new FlagState<>(data.isSuccess(),
data.isSuccess() ? "OK" : data.getReason(),
data);
public static <T> FlagState<T> of(EvalDetail<T> data, boolean success) {
return new FlagState<>(success, success ? "OK" : data.getReason(), data);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/co/featbit/server/Evaluator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package co.featbit.server;

import co.featbit.commons.model.EvalDetail;
import co.featbit.commons.model.FBUser;
import co.featbit.commons.model.FlagState;
import co.featbit.server.exterior.DataStoreTypes;
import org.slf4j.Logger;

Expand Down Expand Up @@ -132,6 +134,19 @@ public String getKeyName() {
public String getName() {
return name;
}

private boolean isDefaultValue() {
return this.index.equals(NO_EVAL_RES);
}

public <T> EvalDetail<T> toEvalDetail(T value) {
return EvalDetail.of(value, this.reason, this.keyName, this.name);
}

public <T> FlagState<T> toFlagState(T value) {
return FlagState.of(EvalDetail.of(value, this.reason, this.keyName, this.name), !isDefaultValue());
}

}

}
16 changes: 10 additions & 6 deletions src/main/java/co/featbit/server/EvaluatorImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import co.featbit.commons.model.FBUser;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
Expand Down Expand Up @@ -51,7 +50,7 @@ private EvalResult matchUserVariation(DataModel.FeatureFlag flag, FBUser user, I
return er;
} finally {
if (er != null) {
logger.info("FFC JAVA SDK: User {}, Feature Flag {}, Flag Value {}", user.getKey(), flag.getKey(), er.getValue());
logger.info("FB JAVA SDK: User {}, Feature Flag {}, Flag Value {}", user.getKey(), flag.getKey(), er.getValue());
if (event != null) {
event.add(InsightTypes.FlagEventVariation.of(flag.getKey(), er));
}
Expand Down Expand Up @@ -124,7 +123,7 @@ private boolean ifUserMatchClause(FBUser user, DataModel.Condition condition) {
} else if (op.equals(IS_TRUE_CLAUSE)) {
return trueClause(user, condition);
} else if (op.equals(IS_FALSE_CLAUSE)) {
return !trueClause(user, condition);
return falseClause(user, condition);
} else if (op.equals(MATCH_REGEX_CLAUSE)) {
return matchRegExClause(user, condition);
} else if (op.equals(NOT_MATCH_REGEX_CLAUSE)) {
Expand Down Expand Up @@ -162,13 +161,18 @@ private boolean inSegmentClause(FBUser user, DataModel.Condition condition) {

private boolean trueClause(FBUser user, DataModel.Condition condition) {
String pv = user.getProperty(condition.getProperty());
return pv != null && BooleanUtils.toBoolean(pv);
return pv != null && pv.toLowerCase().equals("true");
}

private boolean falseClause(FBUser user, DataModel.Condition condition) {
String pv = user.getProperty(condition.getProperty());
return pv != null && pv.toLowerCase().equals("false");
}

private boolean matchRegExClause(FBUser user, DataModel.Condition condition) {
String pv = user.getProperty(condition.getProperty());
String condValue = condition.getValue();
return pv != null && Pattern.compile(condValue).matcher(pv).matches();
return pv != null && condValue != null && Pattern.compile(condValue).matcher(pv).matches();
}

private boolean endsWithClause(FBUser user, DataModel.Condition condition) {
Expand Down Expand Up @@ -208,7 +212,7 @@ private boolean thanClause(FBUser user, DataModel.Condition condition) {
private boolean equalsClause(FBUser user, DataModel.Condition condition) {
String pv = user.getProperty(condition.getProperty());
String condValue = condition.getValue();
return condValue.equals(pv);
return condValue != null && condValue.equals(pv);
}

private boolean containsClause(FBUser user, DataModel.Condition condition) {
Expand Down
Loading