Skip to content

Commit

Permalink
Merge pull request #11907 from knutwannheden/issue/10022
Browse files Browse the repository at this point in the history
Normalize duration default values rendered in documentation
  • Loading branch information
gastaldi committed Sep 7, 2020
2 parents 979b794 + 25fbd66 commit 65c5a32
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -286,6 +287,8 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
defaultValue = hyphenateEnumValue(defaultValue);
}
acceptedValues = extractEnumValues(declaredType, useHyphenateEnumValue);
} else if (isDurationType(declaredType) && !defaultValue.isEmpty()) {
defaultValue = DocGeneratorUtil.normalizeDurationValue(defaultValue);
}
}
}
Expand Down Expand Up @@ -360,6 +363,10 @@ private boolean isEnumType(TypeMirror realTypeMirror) {
&& ((DeclaredType) realTypeMirror).asElement().getKind() == ElementKind.ENUM;
}

private boolean isDurationType(TypeMirror realTypeMirror) {
return realTypeMirror.toString().equals(Duration.class.getName());
}

/**
* Scan or parse configuration items of a given configuration group.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ static String hyphenateEnumValue(String orig) {
return target.toString();
}

static String normalizeDurationValue(String value) {
if (!value.isEmpty() && Character.isDigit(value.charAt(value.length() - 1))) {
try {
value = Integer.parseInt(value) + "S";
} catch (NumberFormatException ignore) {
}
}
value = value.toUpperCase(Locale.ROOT);
return value;
}

static String joinAcceptedValues(List<String> acceptedValues) {
if (acceptedValues == null || acceptedValues.isEmpty()) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.quarkus.annotation.processor.generate_doc.DocGeneratorUtil.computeExtensionDocFileName;
import static io.quarkus.annotation.processor.generate_doc.DocGeneratorUtil.deriveConfigRootName;
import static io.quarkus.annotation.processor.generate_doc.DocGeneratorUtil.getJavaDocSiteLink;
import static io.quarkus.annotation.processor.generate_doc.DocGeneratorUtil.normalizeDurationValue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigInteger;
Expand Down Expand Up @@ -354,4 +355,16 @@ public void derivingConfigRootNameTestCase() {
actual = deriveConfigRootName(simpleClassName, ConfigPhase.BUILD_TIME);
assertEquals("quarkus.root-name", actual);
}

@Test
public void normalizeDurationValueTest() {
assertEquals("", normalizeDurationValue(""));
assertEquals("1S", normalizeDurationValue("1"));
assertEquals("1S", normalizeDurationValue("1S"));
assertEquals("1S", normalizeDurationValue("1s"));

// values are not validated here
assertEquals("1_000", normalizeDurationValue("1_000"));
assertEquals("FOO", normalizeDurationValue("foo"));
}
}

0 comments on commit 65c5a32

Please sign in to comment.