Skip to content

Commit d5133eb

Browse files
authored
Merge pull request #462 from splitio/SDKS-7788
[SDKS-7788] Some flag set fixes
2 parents ef46ef3 + 918d664 commit d5133eb

File tree

7 files changed

+119
-11
lines changed

7 files changed

+119
-11
lines changed

client/src/main/java/io/split/client/SplitClient.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public interface SplitClient {
256256
Map<String, SplitResult> getTreatmentsWithConfig(String key, List<String> featureFlagNames, Map<String, Object> attributes);
257257

258258
/**
259-
* Same as {@link #getTreatments(Key, List<String>, Map)} but it returns for each feature flag the configuration associated to the
259+
* Same as {@link #getTreatments(Key, List, Map)} but it returns for each feature flag the configuration associated to the
260260
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
261261
*
262262
* @param key the matching and bucketing keys. MUST NOT be null.
@@ -268,6 +268,21 @@ public interface SplitClient {
268268
*/
269269
Map<String, SplitResult> getTreatmentsWithConfig(Key key, List<String> featureFlagNames, Map<String, Object> attributes);
270270

271+
/**
272+
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
273+
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
274+
* <p/>
275+
* <p/>
276+
* Examples include showing a different treatment to users on trial plan
277+
* vs. premium plan. Another example is to show a different treatment
278+
* to users created after a certain date.
279+
*
280+
* @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty.
281+
* @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty.
282+
* @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'.
283+
*/
284+
Map<String, String> getTreatmentsByFlagSet(String key, String flagSet);
285+
271286
/**
272287
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
273288
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
@@ -300,6 +315,21 @@ public interface SplitClient {
300315
*/
301316
Map<String, String> getTreatmentsByFlagSet(Key key, String flagSet, Map<String, Object> attributes);
302317

318+
/**
319+
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
320+
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
321+
* <p/>
322+
* <p/>
323+
* Examples include showing a different treatment to users on trial plan
324+
* vs. premium plan. Another example is to show a different treatment
325+
* to users created after a certain date.
326+
*
327+
* @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty.
328+
* @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty.
329+
* @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'.
330+
*/
331+
Map<String, String> getTreatmentsByFlagSets(String key, List<String> flagSets);
332+
303333
/**
304334
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
305335
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
@@ -332,6 +362,22 @@ public interface SplitClient {
332362
*/
333363
Map<String, String> getTreatmentsByFlagSets(Key key, List<String> flagSets, Map<String, Object> attributes);
334364

365+
/**
366+
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
367+
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
368+
* <p/>
369+
* <p/>
370+
* Examples include showing a different treatment to users on trial plan
371+
* vs. premium plan. Another example is to show a different treatment
372+
* to users created after a certain date.
373+
*
374+
* @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty.
375+
* @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty.
376+
* @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration
377+
* associated to this treatment if set.
378+
*/
379+
Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(String key, String flagSet);
380+
335381
/**
336382
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
337383
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
@@ -366,6 +412,22 @@ public interface SplitClient {
366412
*/
367413
Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map<String, Object> attributes);
368414

415+
/**
416+
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
417+
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
418+
* <p/>
419+
* <p/>
420+
* Examples include showing a different treatment to users on trial plan
421+
* vs. premium plan. Another example is to show a different treatment
422+
* to users created after a certain date.
423+
*
424+
* @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty.
425+
* @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty.
426+
* @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration
427+
* associated to this treatment if set.
428+
*/
429+
Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(String key, List<String> flagSets);
430+
369431
/**
370432
* Same as {@link #getTreatments(String, List<String>, Map)} but it returns for each feature flag the configuration associated to the
371433
* matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null.
@@ -444,7 +506,6 @@ public interface SplitClient {
444506
* @param key the identifier of the entity
445507
* @param trafficType the type of the event
446508
* @param eventType the type of the event
447-
* @param value the value of the event
448509
*
449510
* @return true if the track was successful, false otherwise
450511
*/

client/src/main/java/io/split/client/SplitClientImpl.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ public Map<String, SplitResult> getTreatmentsWithConfig(Key key, List<String> fe
150150
MethodEnum.TREATMENTS_WITH_CONFIG);
151151
}
152152

153+
@Override
154+
public Map<String, String> getTreatmentsByFlagSet(String key, String flagSet) {
155+
return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)),
156+
null, MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream()
157+
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment()));
158+
}
159+
153160
@Override
154161
public Map<String, String> getTreatmentsByFlagSet(String key, String flagSet, Map<String, Object> attributes) {
155162
return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)),
@@ -164,6 +171,13 @@ public Map<String, String> getTreatmentsByFlagSet(Key key, String flagSet, Map<S
164171
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment()));
165172
}
166173

174+
@Override
175+
public Map<String, String> getTreatmentsByFlagSets(String key, List<String> flagSets) {
176+
return getTreatmentsBySetsWithConfigInternal(key, null, flagSets,
177+
null, MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream()
178+
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment()));
179+
}
180+
167181
@Override
168182
public Map<String, String> getTreatmentsByFlagSets(String key, List<String> flagSets, Map<String, Object> attributes) {
169183
return getTreatmentsBySetsWithConfigInternal(key, null, flagSets,
@@ -178,6 +192,12 @@ public Map<String, String> getTreatmentsByFlagSets(Key key, List<String> flagSet
178192
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment()));
179193
}
180194

195+
@Override
196+
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(String key, String flagSet) {
197+
return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)),
198+
null, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET);
199+
}
200+
181201
@Override
182202
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map<String, Object> attributes) {
183203
return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)),
@@ -190,6 +210,12 @@ public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(Key key, String
190210
attributes, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET);
191211
}
192212

213+
@Override
214+
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(String key, List<String> flagSets) {
215+
return getTreatmentsBySetsWithConfigInternal(key, null, flagSets,
216+
null, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS);
217+
}
218+
193219
@Override
194220
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(String key, List<String> flagSets, Map<String, Object> attributes) {
195221
return getTreatmentsBySetsWithConfigInternal(key, null, flagSets,
@@ -381,7 +407,8 @@ private Map<String, SplitResult> getTreatmentsBySetsWithConfigInternal(String ma
381407
return new HashMap<>();
382408
}
383409
Set cleanFlagSets = cleanup(sets);
384-
if (filterSetsAreInConfig(cleanFlagSets, methodEnum).isEmpty()) {
410+
cleanFlagSets = filterSetsAreInConfig(cleanFlagSets, methodEnum);
411+
if (cleanFlagSets.isEmpty()) {
385412
return new HashMap<>();
386413
}
387414
List<String> featureFlagNames = new ArrayList<>();
@@ -447,8 +474,8 @@ private Map<String, SplitResult> validateBeforeEvaluate(List<String> featureFlag
447474
}
448475
return null;
449476
}
450-
private List<String> filterSetsAreInConfig(Set<String> sets, MethodEnum methodEnum) {
451-
List<String> setsToReturn = new ArrayList<>();
477+
private Set<String> filterSetsAreInConfig(Set<String> sets, MethodEnum methodEnum) {
478+
Set<String> setsToReturn = new HashSet<>();
452479
for (String set : sets) {
453480
if (!_flagSetsFilter.intersect(set)) {
454481
_log.warn(String.format("%s: you passed %s which is not part of the configured FlagSetsFilter, " +

client/src/main/java/io/split/inputValidation/FlagSetsValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static Set<String> cleanup(List<String> flagSets) {
3333
}
3434
if (!Pattern.matches(FLAG_SET_REGEX, flagSet)) {
3535
_log.warn(String.format("you passed %s, Flag Set must adhere to the regular expressions %s. This means a Flag Set must be " +
36-
"start with a letter, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.",
36+
"start with a letter or number, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.",
3737
flagSet, FLAG_SET_REGEX, flagSet));
3838
continue;
3939
}

client/src/main/java/io/split/storages/memory/InMemoryCacheImp.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ public Map<String, HashSet<String>> getNamesByFlagSets(List<String> flagSets) {
110110
Map<String, HashSet<String>> toReturn = new HashMap<>();
111111
for (String set: flagSets) {
112112
HashSet<String> keys = _flagSets.get(set);
113-
if(keys != null){
114-
toReturn.put(set, keys);
115-
}
113+
toReturn.put(set, keys);
116114
}
117115
return toReturn;
118116
}

client/src/test/java/io/split/client/SplitClientImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,7 @@ public void testTreatmentsByFlagSets() {
19551955
fetchManyResult.put(test2, parsedSplit2);
19561956
when(splitCacheConsumer.fetchMany(new ArrayList<>(Arrays.asList(test2, test)))).thenReturn(fetchManyResult);
19571957

1958-
List<String> sets = new ArrayList<>(Arrays.asList("set1", "set3"));
1958+
List<String> sets = new ArrayList<>(Arrays.asList("set3", "set1"));
19591959
Map<String, HashSet<String>> flagsBySets = new HashMap<>();
19601960
flagsBySets.put("set1", new HashSet<>(Arrays.asList(test)));
19611961
flagsBySets.put("set3", new HashSet<>(Arrays.asList(test2)));

client/src/test/java/io/split/storages/memory/InMemoryCacheTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.stream.Stream;
2525

2626
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertNull;
2728
import static org.junit.Assert.assertTrue;
2829

2930
public class InMemoryCacheTest {
@@ -229,7 +230,8 @@ public void testGetNamesByFlagSets() {
229230
assertTrue(namesByFlagSets.get("set1").contains("splitName_2"));
230231
assertFalse(namesByFlagSets.get("set1").contains("splitName_3"));
231232
assertFalse(namesByFlagSets.get("set1").contains("splitName_4"));
232-
assertFalse(namesByFlagSets.keySet().contains("set3"));
233+
assertTrue(namesByFlagSets.keySet().contains("set3"));
234+
assertNull(namesByFlagSets.get("set3"));
233235

234236
_cache.remove("splitName_2");
235237
namesByFlagSets = _cache.getNamesByFlagSets(new ArrayList<>(Arrays.asList("set1", "set2", "set3")));

testing/src/main/java/io/split/client/testing/SplitClientForTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public Map<String, SplitResult> getTreatmentsWithConfig(Key key, List<String> fe
132132
return treatments;
133133
}
134134

135+
@Override
136+
public Map<String, String> getTreatmentsByFlagSet(String key, String flagSet) {
137+
return null;
138+
}
139+
135140
@Override
136141
public Map<String, String> getTreatmentsByFlagSet(String key, String flagSet, Map<String, Object> attributes) {
137142
return new HashMap<>();
@@ -142,6 +147,11 @@ public Map<String, String> getTreatmentsByFlagSet(Key key, String flagSet, Map<S
142147
return new HashMap<>();
143148
}
144149

150+
@Override
151+
public Map<String, String> getTreatmentsByFlagSets(String key, List<String> flagSets) {
152+
return null;
153+
}
154+
145155
@Override
146156
public Map<String, String> getTreatmentsByFlagSets(String key, List<String> flagSets, Map<String, Object> attributes) {
147157
return new HashMap<>();
@@ -152,6 +162,11 @@ public Map<String, String> getTreatmentsByFlagSets(Key key, List<String> flagSet
152162
return new HashMap<>();
153163
}
154164

165+
@Override
166+
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(String key, String flagSet) {
167+
return null;
168+
}
169+
155170
@Override
156171
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map<String, Object> attributes) {
157172
return new HashMap<>();
@@ -162,6 +177,11 @@ public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(Key key, String
162177
return new HashMap<>();
163178
}
164179

180+
@Override
181+
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(String key, List<String> flagSets) {
182+
return null;
183+
}
184+
165185
@Override
166186
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(String key, List<String> flagSets, Map<String, Object> attributes) {
167187
return new HashMap<>();

0 commit comments

Comments
 (0)