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
86 changes: 49 additions & 37 deletions client/src/main/java/io/split/client/LocalhostSplitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import io.split.client.api.Key;
import io.split.client.api.SplitResult;
import io.split.grammar.Treatments;
import io.split.inputValidation.KeyValidator;
import io.split.inputValidation.SplitNameValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeoutException;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -60,43 +63,6 @@ public SplitResult getTreatmentWithConfig(Key key, String split, Map<String, Obj
return getTreatmentAndConfigInternal(key.matchingKey(), split, attributes);
}

private SplitResult getTreatmentAndConfigInternal(String key, String split) {
return getTreatmentAndConfigInternal(key, split, null);
}

private SplitResult getTreatmentAndConfigInternal(String key, String split, Map<String, Object> attributes) {
if (key == null || split == null) {
return SPLIT_RESULT_CONTROL;
}

SplitAndKey override = SplitAndKey.of(split, key);
if (_map.containsKey(override)) {
return toSplitResult(_map.get(override));
}

SplitAndKey splitDefaultTreatment = SplitAndKey.of(split);

LocalhostSplit localhostSplit = _map.get(splitDefaultTreatment);

if (localhostSplit == null) {
return SPLIT_RESULT_CONTROL;
}

return toSplitResult(localhostSplit);
}

private SplitResult toSplitResult(LocalhostSplit localhostSplit) {
return new SplitResult(localhostSplit.treatment,localhostSplit.config);
}

public void updateFeatureToTreatmentMap(Map<SplitAndKey, LocalhostSplit> map) {
if (map == null) {
_log.warn("A null map was passed as an update. Ignoring this update.");
return;
}
_map = map;
}

@Override
public void destroy() {
_map.clear();
Expand Down Expand Up @@ -127,4 +93,50 @@ public void blockUntilReady() throws TimeoutException, InterruptedException {
// LocalhostSplitClient is always ready
}

public void updateFeatureToTreatmentMap(Map<SplitAndKey, LocalhostSplit> map) {
if (map == null) {
_log.warn("A null map was passed as an update. Ignoring this update.");
return;
}
_map = map;
}

private SplitResult getTreatmentAndConfigInternal(String key, String split, Map<String, Object> attributes) {
boolean keyIsValid = KeyValidator.isValid(key, "matchingKey", "getTreatment");

if (!keyIsValid) {
return SPLIT_RESULT_CONTROL;
}

Optional<String> splitName = SplitNameValidator.isValid(split, "getTreatment");

if (!splitName.isPresent()) {
return SPLIT_RESULT_CONTROL;
}

split = splitName.get();

SplitAndKey override = SplitAndKey.of(split, key);
if (_map.containsKey(override)) {
return toSplitResult(_map.get(override));
}

SplitAndKey splitDefaultTreatment = SplitAndKey.of(split);

LocalhostSplit localhostSplit = _map.get(splitDefaultTreatment);

if (localhostSplit == null) {
return SPLIT_RESULT_CONTROL;
}

return toSplitResult(localhostSplit);
}

private SplitResult toSplitResult(LocalhostSplit localhostSplit) {
return new SplitResult(localhostSplit.treatment,localhostSplit.config);
}

private SplitResult getTreatmentAndConfigInternal(String key, String split) {
return getTreatmentAndConfigInternal(key, split, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ public final class LocalhostSplitClientAndFactory implements SplitClient {
private LocalhostSplitClient _splitClient;

public LocalhostSplitClientAndFactory(LocalhostSplitFactory container, LocalhostSplitClient client) {
_factory = container;
_splitClient = client;

checkNotNull(_factory);
checkNotNull(_splitClient);
_factory = checkNotNull(container);
_splitClient = checkNotNull(client);
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion client/src/main/java/io/split/inputValidation/KeyValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class KeyValidator {
private static final Logger _log = LoggerFactory.getLogger(KeyValidator.class);

public static boolean isValid(String key, String propertyName, int maxStringLength, String method) {
public static boolean isValid(String key, String propertyName, String method) {
if (key == null) {
_log.error(String.format("%s: you passed a null %s, %s must be a non-empty string", method, propertyName, propertyName));
return false;
Expand All @@ -17,6 +17,14 @@ public static boolean isValid(String key, String propertyName, int maxStringLeng
return false;
}

return true;
}

public static boolean isValid(String key, String propertyName, int maxStringLength, String method) {
if (!isValid(key, propertyName, method)) {
return false;
}

if (key.length() > maxStringLength) {
_log.error(String.format("%s: %s too long - must be %s characters or less", method, propertyName, maxStringLength));
return false;
Expand Down