Skip to content

Commit 5a34b81

Browse files
authored
feature/1.0.4: featbit java sdk v1.0.4 (#7)
1 parent 6cf4cf9 commit 5a34b81

20 files changed

+149
-156
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ install the sdk in using maven
4545
<dependency>
4646
<groupId>co.featbit</groupId>
4747
<artifactId>Featbit-Java-SDK</artifactId>
48-
<version>1.0.3</version>
48+
<version>1.0.4</version>
4949
</dependency>
5050
</dependencies>
5151
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>co.featbit</groupId>
88
<artifactId>featbit-java-sdk</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.0.4</version>
1010

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

src/main/java/co/featbit/commons/model/EvalDetail.java

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,19 @@
1515
* @param <T> - String/Boolean/Numeric Type
1616
*/
1717
public final class EvalDetail<T> implements Serializable {
18-
19-
private static final String NO_VARIATION = "NE";
20-
2118
private final T variation;
2219

23-
private final String id;
24-
2520
private final String reason;
2621

2722
private final String name;
2823

2924
private final String keyName;
3025

3126
private EvalDetail(T variation,
32-
String id,
3327
String reason,
3428
String keyName,
3529
String name) {
3630
this.variation = variation;
37-
this.id = id;
3831
this.reason = reason;
3932
this.keyName = keyName;
4033
this.name = name;
@@ -43,27 +36,25 @@ private EvalDetail(T variation,
4336
/**
4437
* build method, this method is only for internal use
4538
*
46-
* @param variation
47-
* @param id
48-
* @param reason
49-
* @param keyName
50-
* @param name
39+
* @param variation the result of flag value
40+
* @param reason main factor that influenced the flag evaluation value
41+
* @param keyName key name of the flag
42+
* @param name name of the flag
5143
* @param <T> String/Boolean/Numeric Type
5244
* @return an EvalDetail
5345
*/
5446
public static <T> EvalDetail<T> of(T variation,
55-
String id,
5647
String reason,
5748
String keyName,
5849
String name) {
59-
return new EvalDetail<>(variation, id, reason, keyName, name);
50+
return new EvalDetail<>(variation, reason, keyName, name);
6051
}
6152

6253
/**
6354
* build the method from a json string, this method is only for internal use
6455
*
65-
* @param json
66-
* @param cls
56+
* @param json json string of an EvalDetail
57+
* @param cls raw type of flag value
6758
* @param <T> String/Boolean/Numeric Type
6859
* @return an EvalDetail
6960
*/
@@ -82,16 +73,6 @@ public T getVariation() {
8273
return variation;
8374
}
8475

85-
/**
86-
* The id of the returned value within the flag's list of variations
87-
* In fact this value is an index, this value is only for internal use
88-
*
89-
* @return a integer value
90-
*/
91-
public String getId() {
92-
return id;
93-
}
94-
9576
/**
9677
* get the reason that evaluate the flag value.
9778
*
@@ -119,16 +100,6 @@ public String getKeyName() {
119100
return keyName;
120101
}
121102

122-
/**
123-
* Returns true if the flag evaluation returned a good value,
124-
* false if the default value returned
125-
*
126-
* @return Returns true if the flag evaluation returned a good value, false if the default value returned
127-
*/
128-
public boolean isSuccess() {
129-
return !id.equals(NO_VARIATION);
130-
}
131-
132103
/**
133104
* object converted to json string
134105
*
@@ -143,27 +114,21 @@ public boolean equals(Object o) {
143114
if (this == o) return true;
144115
if (o == null || getClass() != o.getClass()) return false;
145116
EvalDetail<?> that = (EvalDetail<?>) o;
146-
return id == that.id && Objects.equals(variation, that.variation) && Objects.equals(reason, that.reason) && Objects.equals(name, that.name) && Objects.equals(keyName, that.keyName);
117+
return Objects.equals(variation, that.variation) && Objects.equals(reason, that.reason) && Objects.equals(name, that.name) && Objects.equals(keyName, that.keyName);
147118
}
148119

149120
@Override
150121
public int hashCode() {
151-
return Objects.hash(variation, id, reason, name, keyName);
122+
return Objects.hash(variation, reason, name, keyName);
152123
}
153124

154125
@Override
155126
public String toString() {
156127
return MoreObjects.toStringHelper(this)
157128
.add("variation", variation)
158-
.add("id", id)
159129
.add("reason", reason)
160130
.add("name", name)
161131
.add("keyName", keyName)
162132
.toString();
163133
}
164-
165-
public FlagState<T> toFlagState() {
166-
return FlagState.of(this);
167-
}
168-
169134
}

src/main/java/co/featbit/commons/model/FBUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class FBUser implements Serializable {
2424

2525
private final static Function<FBUser, String> USERNAME = u -> u.userName;
2626
private final static Function<FBUser, String> KEY = u -> u.key;
27-
private final static Map<String, Function<FBUser, String>> BUILTINS = ImmutableMap.of("name", USERNAME, "keyid", KEY);
27+
private final static Map<String, Function<FBUser, String>> BUILTINS = ImmutableMap.of("name", USERNAME, "keyid", KEY, "key", KEY);
2828
private final String userName;
2929
private final String key;
3030
private final Map<String, String> custom;

src/main/java/co/featbit/commons/model/FlagState.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ private FlagState(boolean success, String message, EvalDetail<T> data) {
2727
* @param <T> String/Boolean/Numeric Type
2828
* @return a FlagState
2929
*/
30-
public static <T> FlagState<T> of(EvalDetail<T> data) {
31-
return new FlagState<>(data.isSuccess(),
32-
data.isSuccess() ? "OK" : data.getReason(),
33-
data);
30+
public static <T> FlagState<T> of(EvalDetail<T> data, boolean success) {
31+
return new FlagState<>(success, success ? "OK" : data.getReason(), data);
3432
}
3533

3634
/**

src/main/java/co/featbit/server/Evaluator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package co.featbit.server;
22

3+
import co.featbit.commons.model.EvalDetail;
34
import co.featbit.commons.model.FBUser;
5+
import co.featbit.commons.model.FlagState;
46
import co.featbit.server.exterior.DataStoreTypes;
57
import org.slf4j.Logger;
68

@@ -132,6 +134,19 @@ public String getKeyName() {
132134
public String getName() {
133135
return name;
134136
}
137+
138+
private boolean isDefaultValue() {
139+
return this.index.equals(NO_EVAL_RES);
140+
}
141+
142+
public <T> EvalDetail<T> toEvalDetail(T value) {
143+
return EvalDetail.of(value, this.reason, this.keyName, this.name);
144+
}
145+
146+
public <T> FlagState<T> toFlagState(T value) {
147+
return FlagState.of(EvalDetail.of(value, this.reason, this.keyName, this.name), !isDefaultValue());
148+
}
149+
135150
}
136151

137152
}

src/main/java/co/featbit/server/EvaluatorImp.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import co.featbit.commons.model.FBUser;
55
import com.google.gson.JsonParseException;
66
import com.google.gson.reflect.TypeToken;
7-
import org.apache.commons.lang3.BooleanUtils;
87
import org.apache.commons.lang3.StringUtils;
98

109
import java.math.BigDecimal;
@@ -51,7 +50,7 @@ private EvalResult matchUserVariation(DataModel.FeatureFlag flag, FBUser user, I
5150
return er;
5251
} finally {
5352
if (er != null) {
54-
logger.info("FFC JAVA SDK: User {}, Feature Flag {}, Flag Value {}", user.getKey(), flag.getKey(), er.getValue());
53+
logger.info("FB JAVA SDK: User {}, Feature Flag {}, Flag Value {}", user.getKey(), flag.getKey(), er.getValue());
5554
if (event != null) {
5655
event.add(InsightTypes.FlagEventVariation.of(flag.getKey(), er));
5756
}
@@ -124,7 +123,7 @@ private boolean ifUserMatchClause(FBUser user, DataModel.Condition condition) {
124123
} else if (op.equals(IS_TRUE_CLAUSE)) {
125124
return trueClause(user, condition);
126125
} else if (op.equals(IS_FALSE_CLAUSE)) {
127-
return !trueClause(user, condition);
126+
return falseClause(user, condition);
128127
} else if (op.equals(MATCH_REGEX_CLAUSE)) {
129128
return matchRegExClause(user, condition);
130129
} else if (op.equals(NOT_MATCH_REGEX_CLAUSE)) {
@@ -162,13 +161,18 @@ private boolean inSegmentClause(FBUser user, DataModel.Condition condition) {
162161

163162
private boolean trueClause(FBUser user, DataModel.Condition condition) {
164163
String pv = user.getProperty(condition.getProperty());
165-
return pv != null && BooleanUtils.toBoolean(pv);
164+
return pv != null && pv.toLowerCase().equals("true");
165+
}
166+
167+
private boolean falseClause(FBUser user, DataModel.Condition condition) {
168+
String pv = user.getProperty(condition.getProperty());
169+
return pv != null && pv.toLowerCase().equals("false");
166170
}
167171

168172
private boolean matchRegExClause(FBUser user, DataModel.Condition condition) {
169173
String pv = user.getProperty(condition.getProperty());
170174
String condValue = condition.getValue();
171-
return pv != null && Pattern.compile(condValue).matcher(pv).matches();
175+
return pv != null && condValue != null && Pattern.compile(condValue).matcher(pv).matches();
172176
}
173177

174178
private boolean endsWithClause(FBUser user, DataModel.Condition condition) {
@@ -208,7 +212,7 @@ private boolean thanClause(FBUser user, DataModel.Condition condition) {
208212
private boolean equalsClause(FBUser user, DataModel.Condition condition) {
209213
String pv = user.getProperty(condition.getProperty());
210214
String condValue = condition.getValue();
211-
return condValue.equals(pv);
215+
return condValue != null && condValue.equals(pv);
212216
}
213217

214218
private boolean containsClause(FBUser user, DataModel.Condition condition) {

0 commit comments

Comments
 (0)