Skip to content

Commit

Permalink
improve options reporting configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
xvik committed Aug 21, 2016
1 parent 115f46f commit 8248c7b
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ru.vyarus.dropwizard.guice.module.context.debug.report.DiagnosticReporter;
import ru.vyarus.dropwizard.guice.module.context.debug.report.diagnostic.DiagnosticConfig;
import ru.vyarus.dropwizard.guice.module.context.debug.report.diagnostic.DiagnosticRenderer;
import ru.vyarus.dropwizard.guice.module.context.debug.report.option.OptionsConfig;
import ru.vyarus.dropwizard.guice.module.context.debug.report.option.OptionsRenderer;
import ru.vyarus.dropwizard.guice.module.context.debug.report.stat.StatsRenderer;
import ru.vyarus.dropwizard.guice.module.context.debug.report.tree.ContextTreeConfig;
Expand Down Expand Up @@ -51,7 +52,7 @@
public class DiagnosticBundle implements GuiceyBundle {

private final Boolean statsConfig;
private final Boolean optionsConfig;
private final OptionsConfig optionsConfig;
private final DiagnosticConfig config;
private final ContextTreeConfig treeConfig;

Expand All @@ -62,7 +63,10 @@ public class DiagnosticBundle implements GuiceyBundle {
public DiagnosticBundle() {
this(builder()
.printStartupStats(true)
.printOptions(true)

.printOptions(new OptionsConfig()
.showNotDefinedOptions()
.showNotUsedMarker())

.printConfiguration(new DiagnosticConfig()
.printDefaults())
Expand Down Expand Up @@ -111,7 +115,7 @@ public static Builder builder() {
public static class Builder {

private Boolean statsConfig;
private Boolean optionsConfig;
private OptionsConfig optionsConfig;
private DiagnosticConfig config;
private ContextTreeConfig treeConfig;

Expand All @@ -133,12 +137,11 @@ public Builder printStartupStats(final boolean hideSmallTimes) {
/**
* Enables options reporting. Some options could be read lazily and so marked as NOT_USED at reporting time.
*
* @param showNotUsedMarker true to show NOT_USED marker for user defined but never read options, false
* to avoid marker
* @param config options section configuration
* @return builder instance for chained calls
*/
public Builder printOptions(final boolean showNotUsedMarker) {
this.optionsConfig = showNotUsedMarker;
public Builder printOptions(final OptionsConfig config) {
this.optionsConfig = config;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ru.vyarus.dropwizard.guice.module.context.debug.DiagnosticBundle;
import ru.vyarus.dropwizard.guice.module.context.debug.report.diagnostic.DiagnosticConfig;
import ru.vyarus.dropwizard.guice.module.context.debug.report.diagnostic.DiagnosticRenderer;
import ru.vyarus.dropwizard.guice.module.context.debug.report.option.OptionsConfig;
import ru.vyarus.dropwizard.guice.module.context.debug.report.option.OptionsRenderer;
import ru.vyarus.dropwizard.guice.module.context.debug.report.stat.StatsRenderer;
import ru.vyarus.dropwizard.guice.module.context.debug.report.tree.ContextTreeConfig;
Expand Down Expand Up @@ -32,7 +33,7 @@ public final class DiagnosticReporter {
private ContextTreeRenderer contextTreeRenderer;

public void report(final Boolean statsConfig,
final Boolean optionsConfig,
final OptionsConfig optionsConfig,
final DiagnosticConfig config,
final ContextTreeConfig treeConfig) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ru.vyarus.dropwizard.guice.module.context.debug.report.option;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Options reporting configuration. By default, only user defined options are printed without not used options
* indication. Configuration could be performed in chained fashion:
* <pre><code>
* OptionsConfig config = new OptionsConfig()
* .showNotUsedMarker()
* .showNotDefinedOptions()
* </code></pre>
*
* @author Vyacheslav Rusakov
* @see OptionsRenderer for usage
* @since 22.08.2016
*/
public class OptionsConfig {

private final Set<Class<Enum>> hiddenGroups = new HashSet<>();
private final Set<Enum> hidden = new HashSet<>();
private boolean notUsed;
private boolean notDefined;

/**
* @return true to show not used options marker, false otherwise
*/
public boolean isShowNotUsedMarker() {
return notUsed;
}

/**
* CUSTOM marker is shown only when option enabled.
*
* @return true to show options with default values (not overridden by user), false otherwise
*/
public boolean isShowNotDefinedOptions() {
return notDefined;
}

/**
* @return set of option classed to hide or empty set
*/
public Set<Class<Enum>> getHiddenGroups() {
return hiddenGroups;
}

/**
* @return set of options to hide or empty set
*/
public Set<Enum> getHiddenOptions() {
return hidden;
}

/**
* Show NOT_USED marker for not (yet) used options.
*
* @return config instance for chained calls
*/
public OptionsConfig showNotUsedMarker() {
notUsed = true;
return this;
}

/**
* Show options not customized by user (in other words show used options defaults).
* When enabled, CUSTOM marker is shown for user defined options.
*
* @return config instance for chained calls
*/
public OptionsConfig showNotDefinedOptions() {
notDefined = true;
return this;
}

/**
* Hide option groups from reporting.
*
* @param groups option enum classes to hide
* @return config instance for chained calls
*/
@SafeVarargs
public final OptionsConfig hideGroups(final Class<Enum>... groups) {
hiddenGroups.addAll(Arrays.asList(groups));
return this;
}

/**
* Hide exact options from reporting.
*
* @param options options to hide
* @return config instance for chained calls
*/
public OptionsConfig hideOptions(final Enum... options) {
hidden.addAll(Arrays.asList(options));
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @since 13.08.2016
*/
@Singleton
public class OptionsRenderer implements ReportRenderer<Boolean> {
public class OptionsRenderer implements ReportRenderer<OptionsConfig> {

private static final String POSTFIX = "Options";

Expand All @@ -47,37 +47,38 @@ public OptionsRenderer(final GuiceyConfigurationInfo info) {
/**
* Renders options report.
*
* @param showNotUsedMarker true to show NOT_USED marker for set but never read options, false to avoid marker
* @param config rendering config
* @return rendered report
*/
@Override
public String renderReport(final Boolean showNotUsedMarker) {
public String renderReport(final OptionsConfig config) {
final StringBuilder res = new StringBuilder();
render(showNotUsedMarker, res);
render(config, res);
return res.toString();
}

private void render(final boolean showNotUsedMarker, final StringBuilder res) {
private void render(final OptionsConfig config, final StringBuilder res) {
final List<Class<Enum>> groups = info.getOptions().getOptionGroups();
groups.removeAll(config.getHiddenGroups());
for (Class<Enum> group : groups) {
res.append(NEWLINE).append(NEWLINE).append(TAB)
.append(String.format("%-25s (%s)", groupName(group), RenderUtils.renderClass(group)))
.append(NEWLINE);
renderOptions(group, showNotUsedMarker, res);
renderOptions(group, config, res);
}
}

@SuppressWarnings("unchecked")
private void renderOptions(final Class<Enum> group, final boolean showNotUsedMarker, final StringBuilder res) {
private void renderOptions(final Class<Enum> group, final OptionsConfig config, final StringBuilder res) {
final List<String> markers = Lists.newArrayList();
for (Enum option : group.getEnumConstants()) {
final OptionsInfo options = info.getOptions();
if (options.knowsOption(option)) {
if (!isHidden(option, config)) {
markers.clear();
if (options.isSet(option)) {
if (config.isShowNotDefinedOptions() && options.isSet(option)) {
markers.add("CUSTOM");
}
if (!options.isUsed(option) && showNotUsedMarker) {
if (config.isShowNotUsedMarker() && !options.isUsed(option)) {
markers.add("NOT_USED");
}
res.append(TAB).append(TAB)
Expand All @@ -95,6 +96,13 @@ private String groupName(final Class<Enum> group) {
return name;
}

@SuppressWarnings("PMD.UselessParentheses")
private boolean isHidden(final Enum option, final OptionsConfig config) {
return config.getHiddenOptions().contains(option)
|| !info.getOptions().knowsOption(option)
|| (!config.isShowNotDefinedOptions() && !info.getOptions().isSet(option));
}

private String valueToString(final Object value) {
final String res;
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ru.vyarus.dropwizard.guice.module.context.debug.report.tree;

import com.google.common.collect.Sets;
import ru.vyarus.dropwizard.guice.module.context.ConfigItem;
import ru.vyarus.dropwizard.guice.module.context.info.ItemInfo;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
Expand All @@ -24,8 +24,8 @@
*/
public final class ContextTreeConfig {

private final Set<ConfigItem> items = Sets.newHashSet();
private final Set<Class<?>> scopes = Sets.newHashSet();
private final Set<ConfigItem> items = new HashSet<>();
private final Set<Class<?>> scopes = new HashSet<>();
private boolean disables;
private boolean notUsedInstallers;
private boolean duplicateRegistrations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ru.vyarus.dropwizard.guice.diagnostic.support.features.FooModule
import ru.vyarus.dropwizard.guice.diagnostic.support.features.FooResource
import ru.vyarus.dropwizard.guice.module.context.debug.DiagnosticBundle
import ru.vyarus.dropwizard.guice.module.context.debug.report.diagnostic.DiagnosticConfig
import ru.vyarus.dropwizard.guice.module.context.debug.report.option.OptionsConfig
import ru.vyarus.dropwizard.guice.module.context.debug.report.tree.ContextTreeConfig
import ru.vyarus.dropwizard.guice.module.installer.feature.LifeCycleInstaller
import ru.vyarus.dropwizard.guice.test.spock.UseDropwizardApp
Expand Down Expand Up @@ -59,14 +60,14 @@ class DiagnosticBundleTest extends AbstractTest {
when: "all options configured"
bundle = DiagnosticBundle.builder()
.printStartupStats(false)
.printOptions(true)
.printOptions(new OptionsConfig().showNotUsedMarker().showNotDefinedOptions())
.printConfiguration(new DiagnosticConfig().printAll())
.printContextTree(new ContextTreeConfig())
.build()

then: "configured"
bundle.statsConfig == false
bundle.optionsConfig == true
bundle.optionsConfig.isShowNotUsedMarker()
bundle.config.isPrintBundles()
bundle.treeConfig.hiddenItems.empty

Expand All @@ -90,7 +91,7 @@ class DiagnosticBundleTest extends AbstractTest {
.searchCommands()
.bundles(new Foo2Bundle())
.modules(new FooModule())
// intentional duplicate to increment REG
// intentional duplicate to increment REG
.extensions(FooBundleResource, FooBundleResource)
.disableInstallers(LifeCycleInstaller)
.strictScopeControl()
Expand Down
Loading

0 comments on commit 8248c7b

Please sign in to comment.