Skip to content

Commit c512e3d

Browse files
committed
Provide a @ConfigDocEnum annotation to enforce hyphenation of enum
Also refactor things a bit to centralize the handling of the common annotations. Fixes #42514
1 parent f28e44e commit c512e3d

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/discovery/DiscoveryConfigProperty.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ public class DiscoveryConfigProperty {
1515
private final boolean unnamedMapKey;
1616
private final ResolvedType type;
1717
private final boolean converted;
18+
private final boolean enforceHyphenateEnumValue;
1819
private final boolean section;
1920
private final boolean sectionGenerated;
2021

2122
public DiscoveryConfigProperty(String path, String sourceClass, String sourceName, String defaultValue,
2223
String defaultValueForDoc, boolean deprecated, String mapKey, boolean unnamedMapKey,
23-
ResolvedType type, boolean converted,
24+
ResolvedType type, boolean converted, boolean enforceHyphenateEnumValue,
2425
boolean section, boolean sectionGenerated) {
2526
this.path = path;
2627
this.sourceClass = sourceClass;
@@ -32,6 +33,7 @@ public DiscoveryConfigProperty(String path, String sourceClass, String sourceNam
3233
this.unnamedMapKey = unnamedMapKey;
3334
this.type = type;
3435
this.converted = converted;
36+
this.enforceHyphenateEnumValue = enforceHyphenateEnumValue;
3537
this.section = section;
3638
this.sectionGenerated = sectionGenerated;
3739
}
@@ -76,6 +78,10 @@ public boolean isConverted() {
7678
return converted;
7779
}
7880

81+
public boolean isEnforceHyphenateEnumValue() {
82+
return enforceHyphenateEnumValue;
83+
}
84+
7985
public boolean isSection() {
8086
return section;
8187
}
@@ -132,6 +138,7 @@ public static class Builder {
132138
private String mapKey;
133139
private boolean unnamedMapKey = false;
134140
private boolean converted = false;
141+
private boolean enforceHyphenateEnumValue = false;
135142
private boolean section = false;
136143
private boolean sectionGenerated = false;
137144

@@ -176,6 +183,11 @@ public Builder converted() {
176183
return this;
177184
}
178185

186+
public Builder enforceHyphenateEnumValues() {
187+
this.enforceHyphenateEnumValue = true;
188+
return this;
189+
}
190+
179191
public Builder section(boolean generated) {
180192
this.section = true;
181193
this.sectionGenerated = generated;
@@ -191,7 +203,7 @@ public DiscoveryConfigProperty build() {
191203
}
192204

193205
return new DiscoveryConfigProperty(name, sourceClass, sourceName, defaultValue, defaultValueForDoc, deprecated,
194-
mapKey, unnamedMapKey, type, converted, section, sectionGenerated);
206+
mapKey, unnamedMapKey, type, converted, enforceHyphenateEnumValue, section, sectionGenerated);
195207
}
196208
}
197209
}

core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/resolver/ConfigResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ private void resolveProperty(ConfigRoot configRoot, Map<String, ConfigSection> e
153153
String typeSimplifiedName = discoveryConfigProperty.getType().simplifiedName();
154154

155155
// if the property has a converter, we don't hyphenate the values (per historical rules, not exactly sure of the reason)
156-
boolean hyphenateEnumValues = !discoveryConfigProperty.isConverted();
156+
boolean hyphenateEnumValues = discoveryConfigProperty.isEnforceHyphenateEnumValue() ||
157+
!discoveryConfigProperty.isConverted();
157158

158159
String defaultValue = getDefaultValue(discoveryConfigProperty.getDefaultValue(),
159160
discoveryConfigProperty.getDefaultValueForDoc(), discoveryConfigProperty.getType(), hyphenateEnumValues);

core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/scanner/AbstractConfigListener.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import javax.lang.model.element.TypeElement;
1111

1212
import io.quarkus.annotation.processor.documentation.config.discovery.DiscoveryConfigGroup;
13+
import io.quarkus.annotation.processor.documentation.config.discovery.DiscoveryConfigProperty;
1314
import io.quarkus.annotation.processor.documentation.config.discovery.EnumDefinition;
1415
import io.quarkus.annotation.processor.documentation.config.discovery.EnumDefinition.EnumConstant;
16+
import io.quarkus.annotation.processor.documentation.config.discovery.ResolvedType;
1517
import io.quarkus.annotation.processor.documentation.config.util.Types;
1618
import io.quarkus.annotation.processor.util.Config;
1719
import io.quarkus.annotation.processor.util.Utils;
@@ -63,4 +65,28 @@ public void onResolvedEnum(TypeElement enumTypeElement) {
6365
enumConstants);
6466
configCollector.addResolvedEnum(enumDefinition);
6567
}
68+
69+
protected void handleCommonPropertyAnnotations(DiscoveryConfigProperty.Builder builder,
70+
Map<String, AnnotationMirror> propertyAnnotations, ResolvedType resolvedType, String sourceName) {
71+
72+
AnnotationMirror configDocSectionAnnotation = propertyAnnotations.get(Types.ANNOTATION_CONFIG_DOC_SECTION);
73+
if (configDocSectionAnnotation != null) {
74+
Boolean sectionGenerated = (Boolean) utils.element().getAnnotationValues(configDocSectionAnnotation)
75+
.get("generated");
76+
if (sectionGenerated != null && sectionGenerated) {
77+
builder.section(true);
78+
} else {
79+
builder.section(false);
80+
}
81+
}
82+
83+
AnnotationMirror configDocEnum = propertyAnnotations.get(Types.ANNOTATION_CONFIG_DOC_ENUM);
84+
if (configDocEnum != null) {
85+
Boolean enforceHyphenateValues = (Boolean) utils.element().getAnnotationValues(configDocEnum)
86+
.get("enforceHyphenateValues");
87+
if (enforceHyphenateValues != null && enforceHyphenateValues) {
88+
builder.enforceHyphenateEnumValues();
89+
}
90+
}
91+
}
6692
}

core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/scanner/ConfigMappingListener.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,7 @@ public void onEnclosedMethod(DiscoveryRootElement discoveryRootElement, TypeElem
174174
builder.converted();
175175
}
176176

177-
AnnotationMirror configDocSectionAnnotation = methodAnnotations.get(Types.ANNOTATION_CONFIG_DOC_SECTION);
178-
if (configDocSectionAnnotation != null) {
179-
Boolean sectionGenerated = (Boolean) utils.element().getAnnotationValues(configDocSectionAnnotation)
180-
.get("generated");
181-
if (sectionGenerated != null && sectionGenerated) {
182-
builder.section(true);
183-
} else {
184-
builder.section(false);
185-
}
186-
}
177+
handleCommonPropertyAnnotations(builder, methodAnnotations, resolvedType, sourceName);
187178

188179
discoveryRootElement.addProperty(builder.build());
189180
}

core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/scanner/LegacyConfigRootListener.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,7 @@ public void onEnclosedField(DiscoveryRootElement discoveryRootElement, TypeEleme
176176
builder.converted();
177177
}
178178

179-
AnnotationMirror configDocSectionAnnotation = fieldAnnotations.get(Types.ANNOTATION_CONFIG_DOC_SECTION);
180-
if (configDocSectionAnnotation != null) {
181-
Boolean sectionGenerated = (Boolean) utils.element().getAnnotationValues(configDocSectionAnnotation)
182-
.get("generated");
183-
if (sectionGenerated != null && sectionGenerated) {
184-
builder.section(true);
185-
} else {
186-
builder.section(false);
187-
}
188-
}
179+
handleCommonPropertyAnnotations(builder, fieldAnnotations, resolvedType, sourceName);
189180

190181
discoveryRootElement.addProperty(builder.build());
191182
}

core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/util/Types.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ private Types() {
3131
public static final String ANNOTATION_CONFIG_DOC_DEFAULT = "io.quarkus.runtime.annotations.ConfigDocDefault";
3232
public static final String ANNOTATION_CONFIG_DOC_FILE_NAME = "io.quarkus.runtime.annotations.ConfigDocFilename";
3333
public static final String ANNOTATION_CONFIG_DOC_PREFIX = "io.quarkus.runtime.annotations.ConfigDocPrefix";
34+
public static final String ANNOTATION_CONFIG_DOC_ENUM = "io.quarkus.runtime.annotations.ConfigDocEnum";
3435

3536
public static final String ANNOTATION_CONFIG_WITH_CONVERTER = "io.smallrye.config.WithConverter";
3637
public static final String ANNOTATION_CONFIG_WITH_NAME = "io.smallrye.config.WithName";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.quarkus.runtime.annotations;
2+
3+
import static java.lang.annotation.ElementType.FIELD;
4+
import static java.lang.annotation.ElementType.METHOD;
5+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
6+
7+
import java.lang.annotation.Documented;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* Provides a way to configure how an enum is handled.
13+
*/
14+
@Retention(RUNTIME)
15+
@Target({ FIELD, METHOD })
16+
@Documented
17+
public @interface ConfigDocEnum {
18+
19+
/**
20+
* This can be used to enforce hyphenating the enum values even if a converter is present.
21+
*/
22+
boolean enforceHyphenateValues() default false;
23+
24+
}

0 commit comments

Comments
 (0)