Skip to content

Commit

Permalink
chore: tease out yggdrasil adapters into their own static class
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Nov 28, 2024
1 parent 979229f commit d49d244
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 74 deletions.
83 changes: 9 additions & 74 deletions src/main/java/io/getunleash/DefaultUnleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
import io.getunleash.repository.JsonFeatureParser;
import io.getunleash.strategy.*;
import io.getunleash.util.UnleashConfig;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -100,10 +96,10 @@ public DefaultUnleash(
this.unleashEngine =
new UnleashEngine(
strategyMap.values().stream()
.map(this::asIStrategy)
.map(YggdrasilAdapters::adapt)
.collect(Collectors.toList()),
Optional.ofNullable(unleashConfig.getFallbackStrategy())
.map(this::asIStrategy)
.map(YggdrasilAdapters::adapt)
.orElse(null));
featureRepository.addConsumer(
featureCollection -> {
Expand Down Expand Up @@ -145,65 +141,6 @@ public DefaultUnleash(
});
}

@NotNull
private IStrategy asIStrategy(Strategy s) {
return new IStrategy() {
@Override
public String getName() {
return s.getName();
}

@Override
public boolean isEnabled(Map<String, String> map, Context context) {
return s.isEnabled(map, adapt(context));
}
};
}

private UnleashContext adapt(Context context) {
ZonedDateTime currentTime = ZonedDateTime.now();
if (context.getCurrentTime() != null) {
try {
currentTime = ZonedDateTime.parse(context.getCurrentTime());
} catch (DateTimeParseException e) {
LOGGER.warn(
"Unable to parse current time from context: {}, using current time instead",
context.getCurrentTime());
}
}

return new UnleashContext(
context.getAppName(),
context.getEnvironment(),
context.getUserId(),
context.getSessionId(),
context.getRemoteAddress(),
currentTime,
context.getProperties())
.applyStaticFields(config);
}

private Context adapt(UnleashContext context) {
Context mapped = new Context();
mapped.setAppName(context.getAppName().orElse(null));
mapped.setEnvironment(context.getEnvironment().orElse(null));
mapped.setUserId(context.getUserId().orElse(null));
mapped.setSessionId(context.getSessionId().orElse(null));
mapped.setRemoteAddress(context.getRemoteAddress().orElse(null));
mapped.setProperties(context.getProperties());
mapped.setCurrentTime(
DateTimeFormatter.ISO_DATE_TIME.format(
context.getCurrentTime().orElse(ZonedDateTime.now())));
return mapped;
}

private Variant adapt(VariantDef variant, Variant defaultValue) {
if (variant == null) {
return defaultValue;
}
return new Variant(variant.getName(), adapt(variant.getPayload()), variant.isEnabled());
}

@Override
public boolean isEnabled(final String toggleName, final boolean defaultSetting) {
return isEnabled(toggleName, contextProvider.getContext(), defaultSetting);
Expand All @@ -223,7 +160,9 @@ public boolean isEnabled(

UnleashContext enhancedContext = context.applyStaticFields(config);
try {
Boolean enabled = this.unleashEngine.isEnabled(toggleName, adapt(enhancedContext));
Boolean enabled =
this.unleashEngine.isEnabled(
toggleName, YggdrasilAdapters.adapt(enhancedContext));
if (enabled == null) {
enabled = fallbackAction.test(toggleName, enhancedContext);
}
Expand All @@ -243,12 +182,6 @@ public boolean isEnabled(
}
}

private @Nullable io.getunleash.variant.Payload adapt(@Nullable Payload payload) {
return Optional.ofNullable(payload)
.map(p -> new io.getunleash.variant.Payload(p.getType(), p.getValue()))
.orElse(new io.getunleash.variant.Payload("string", null));
}

@Override
public Variant getVariant(String toggleName, UnleashContext context) {
return getVariant(toggleName, context, DISABLED_VARIANT);
Expand All @@ -259,10 +192,12 @@ public Variant getVariant(String toggleName, UnleashContext context, Variant def
UnleashContext enhancedContext = context.applyStaticFields(config);

try {
Context adaptedContext = adapt(enhancedContext);
Context adaptedContext = YggdrasilAdapters.adapt(enhancedContext);

Variant variant =
adapt(this.unleashEngine.getVariant(toggleName, adaptedContext), defaultValue);
YggdrasilAdapters.adapt(
this.unleashEngine.getVariant(toggleName, adaptedContext),
defaultValue);

Boolean enabled = this.unleashEngine.isEnabled(toggleName, adaptedContext);

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/io/getunleash/YggdrasilAdapters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.getunleash;

import io.getunleash.engine.Context;
import io.getunleash.engine.IStrategy;
import io.getunleash.engine.Payload;
import io.getunleash.engine.VariantDef;
import io.getunleash.lang.Nullable;
import io.getunleash.strategy.Strategy;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;

final class YggdrasilAdapters {

@NotNull
public static IStrategy adapt(Strategy s) {
return new IStrategy() {
@Override
public String getName() {
return s.getName();
}

@Override
public boolean isEnabled(Map<String, String> map, Context context) {
return s.isEnabled(map, adapt(context));
}
};
}

public static UnleashContext adapt(Context context) {
ZonedDateTime currentTime = ZonedDateTime.now();
if (context.getCurrentTime() != null) {
try {
currentTime = ZonedDateTime.parse(context.getCurrentTime());
} catch (DateTimeParseException e) {
}
}

return new UnleashContext(
context.getAppName(),
context.getEnvironment(),
context.getUserId(),
context.getSessionId(),
context.getRemoteAddress(),
currentTime,
context.getProperties());
}

public static Context adapt(UnleashContext context) {
Context mapped = new Context();
mapped.setAppName(context.getAppName().orElse(null));
mapped.setEnvironment(context.getEnvironment().orElse(null));
mapped.setUserId(context.getUserId().orElse(null));
mapped.setSessionId(context.getSessionId().orElse(null));
mapped.setRemoteAddress(context.getRemoteAddress().orElse(null));
mapped.setProperties(context.getProperties());
mapped.setCurrentTime(
DateTimeFormatter.ISO_DATE_TIME.format(
context.getCurrentTime().orElse(ZonedDateTime.now())));
return mapped;
}

public static Variant adapt(VariantDef variant, Variant defaultValue) {
if (variant == null) {
return defaultValue;
}
return new Variant(variant.getName(), adapt(variant.getPayload()), variant.isEnabled());
}

public static @Nullable io.getunleash.variant.Payload adapt(@Nullable Payload payload) {
return Optional.ofNullable(payload)
.map(p -> new io.getunleash.variant.Payload(p.getType(), p.getValue()))
.orElse(new io.getunleash.variant.Payload("string", null));
}
}

0 comments on commit d49d244

Please sign in to comment.