Skip to content

Commit

Permalink
Allow adding multiple examples for config help
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Aug 18, 2021
1 parent 324535a commit 018c662
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
35 changes: 20 additions & 15 deletions java/src/org/openqa/selenium/grid/config/ConfigFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.PrintStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -109,6 +110,8 @@ public boolean dumpConfigHelp(Config config, Set<Role> currentRoles, PrintStream

StringBuilder demoToml = new StringBuilder();
demoToml.append("Configuration help for Toml config file").append("\n\n");
demoToml.append("In case of parsing errors, validate the config using https://www.toml-lint.com/").append("\n\n");
demoToml.append("Refer https://toml.io/en/ for TOML usage guidance").append("\n\n");
allOptions.forEach((section, options) -> {
demoToml.append("[").append(section).append("]\n");
options.stream().filter(option -> !option.hidden).forEach(option -> {
Expand All @@ -119,22 +122,24 @@ public boolean dumpConfigHelp(Config config, Set<Role> currentRoles, PrintStream
if (!option.defaultValue.isEmpty()) {
demoToml.append("# Default: ").append(option.defaultValue).append("\n");
}
demoToml.append("# Example: ").append("\n");
if (option.prefixed) {
demoToml.append("[[")
.append(section)
.append(".")
.append(option.optionName)
.append("]]")
.append(option.example(config))
.append("\n\n");
} else {
demoToml.append(option.optionName)
.append(" = ")
.append(option.example(config)).append("\n\n");
}
Arrays.stream(option.example()).forEach(example -> {
demoToml.append("# Example: ").append("\n");
if (option.prefixed) {
demoToml.append("[[")
.append(section)
.append(".")
.append(option.optionName)
.append("]]")
.append(option.example(config, example))
.append("\n\n");
} else {
demoToml.append(option.optionName)
.append(" = ")
.append(option.example(config, example)).append("\n\n");
}
});
demoToml.append("\n");
});
demoToml.append("\n");
});

dumpTo.print(demoToml);
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/grid/config/ConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@

boolean prefixed() default false;

String example();
String[] example();
}
11 changes: 6 additions & 5 deletions java/src/org/openqa/selenium/grid/config/DescribedOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand All @@ -50,7 +51,7 @@ public class DescribedOption implements Comparable<DescribedOption> {
public final String optionName;
public final String description;
public final String type;
public final String example;
public final String[] example;
public final String defaultValue;
public final boolean prefixed;
public final boolean repeats;
Expand Down Expand Up @@ -133,11 +134,11 @@ public boolean requiresTomlQuoting() {
return quotable;
}

public String example() {
public String[] example() {
return example;
}

public String example(Config config) {
public String example(Config config, String example) {
Optional<List<String>> allOptions = config.getAll(section, optionName);
if (allOptions.isPresent() && !allOptions.get().isEmpty()) {
if (repeats) {
Expand Down Expand Up @@ -179,7 +180,7 @@ public boolean equals(Object o) {
Objects.equals(description, that.description) &&
Objects.equals(defaultValue, that.defaultValue) &&
Objects.equals(type, that.type) &&
Objects.equals(example, that.example) &&
Arrays.equals(example, that.example) &&
Objects.equals(flags, that.flags);
}

Expand All @@ -189,7 +190,7 @@ public int hashCode() {
optionName,
description,
type,
example,
Arrays.hashCode(example),
repeats,
quotable,
flags,
Expand Down

0 comments on commit 018c662

Please sign in to comment.