Skip to content

Commit

Permalink
Feature/logging-for-disabled-features (#1048)
Browse files Browse the repository at this point in the history
* Added Logging for disabled features
  • Loading branch information
Suleman70 authored Mar 16, 2024
1 parent 4ff5966 commit 6c3cc81
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.togetherjava.tjbot.config;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled.
Expand All @@ -13,6 +17,8 @@
public class FeatureBlacklist<T> {
private final Set<T> featureIdentifierBlacklist;

private static final Logger logger = LoggerFactory.getLogger(FeatureBlacklist.class);

/**
* Creates a feature blacklist
*
Expand All @@ -32,4 +38,25 @@ public FeatureBlacklist(Set<T> featureIdentifierBlacklist) {
public boolean isEnabled(T featureId) {
return !featureIdentifierBlacklist.contains(featureId);
}

/**
* Filters features stream to only having enabled features and logs any disabled features.
*
* @param features the feature stream to be filtered
* @param idExtractor function to get the class name of an object
* @return stream of features that are enabled
*/
public <F> Stream<F> filterStream(Stream<F> features,
Function<? super F, ? extends T> idExtractor) {
return features.filter(f -> {
T id = idExtractor.apply(f);
if (!this.isEnabled(id)) {
logger.info("Feature {} is disabled", id);
return false;
}
return true;
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new JShellCommand(jshellEval));

FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList();
return blacklist.filterStream(features.stream(), Object::getClass).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements
public CodeMessageHandler(FeatureBlacklist<String> blacklist, JShellEval jshellEval) {
componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());

List<CodeAction> codeActions =
Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval))
.filter(a -> blacklist.isEnabled(a.getClass().getSimpleName()))
.toList();
List<CodeAction> codeActions = blacklist
.filterStream(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)),
codeAction -> codeAction.getClass().getName())
.toList();

labelToCodeAction = codeActions.stream()
.collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,
Expand Down

0 comments on commit 6c3cc81

Please sign in to comment.