Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize duration default values rendered in documentation #11907

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"));
}
}