cloud.prefab.client.internal.AbstractFeatureFlagResolverImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prefab-cloud-java Show documentation
Show all versions of prefab-cloud-java Show documentation
API Client for https://prefab.cloud: rate limits, feature flags and semaphores as a service
The newest version!
package cloud.prefab.client.internal;
import cloud.prefab.client.FeatureFlagClient;
import cloud.prefab.client.config.ConfigLoader;
import cloud.prefab.client.config.ConfigResolver;
import cloud.prefab.domain.Prefab;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public abstract class AbstractFeatureFlagResolverImpl implements FeatureFlagClient {
@Override
public boolean featureIsOn(String feature) {
return featureIsOnFor(feature, Optional.empty(), ImmutableMap.of());
}
@Override
public boolean featureIsOnFor(String feature, String lookupKey) {
return isOn(get(feature, Optional.of(lookupKey), ImmutableMap.of()));
}
/**
* Simplified method for boolean flags. Returns false if flag is not defined.
*
* @param feature
* @param lookupKey
* @param attributes
* @return
*/
@Override
public boolean featureIsOnFor(
String feature,
String lookupKey,
Map attributes
) {
return featureIsOnFor(feature, Optional.of(lookupKey), attributes);
}
@Override
public boolean featureIsOnFor(
String feature,
Optional lookupKey,
Map attributes
) {
return isOn(get(feature, lookupKey, attributes));
}
/**
* Fetch a feature flag and evaluate
*
* @param feature
* @param lookupKey
* @param properties
* @return
*/
@Override
public Optional get(
String feature,
Optional lookupKey,
Map properties
) {
return getFrom(
feature,
lookupKey,
properties
.entrySet()
.stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
e -> ConfigLoader.configValueFromObj(feature, e.getValue())
)
)
);
}
@Override
public Optional getFrom(
String feature,
Optional lookupKey,
Map attributes
) {
if (lookupKey.isPresent()) {
attributes.put(
ConfigResolver.LOOKUP_KEY,
Prefab.ConfigValue.newBuilder().setString(lookupKey.get()).build()
);
}
return getConfigValue(feature, attributes);
}
protected abstract Optional getConfigValue(
String feature,
Map attributes
);
private boolean isOn(Optional featureFlagVariant) {
if (featureFlagVariant.isPresent()) {
if (featureFlagVariant.get().hasBool()) {
return featureFlagVariant.get().getBool();
} else {
// TODO log
return false;
}
} else {
return false;
}
}
}