Skip to content

Commit

Permalink
chore: use enum
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Oct 10, 2024
1 parent d26d737 commit ffd9814
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static String getAnnotationMemberValue(PsiAnnotation annotation, String m
if (member instanceof PsiEnumConstant) {
// ex : @ConfigRoot(phase = BUILD_AND_RUN_TIME_FIXED)
// returns BUILD_AND_RUN_TIME_FIXED
return member.getText();
return ((PsiEnumConstant) member).getName();
}
if (member instanceof PsiReference reference) {
// ex: @Path(MY_CONSTANTS) where MY_CONSTANTS is a Java field.
Expand All @@ -188,7 +188,7 @@ public static String getAnnotationMemberValue(PsiAnnotation annotation, String m
if (member instanceof PsiEnumConstant) {
// ex : @ConfigRoot(phase = io.quarkus.runtime.annotations.ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
// returns BUILD_AND_RUN_TIME_FIXED
return member.getText();
return ((PsiEnumConstant) member).getName();
}
if (member instanceof PsiField field) {
// ex: private static final String MY_CONSTANTS = "foo";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,6 @@ public class QuarkusConstants {

public static final String CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY = "namingStrategy";

// The method name is used as is to map the configuration property.
public static final String CONFIG_MAPPING_NAMING_STRATEGY_VERBATIM = "VERBATIM";

// The method name is derived by replacing case changes with a dash to map the
// configuration property.
public static final String CONFIG_MAPPING_NAMING_STRATEGY_KEBAB_CASE = "KEBAB_CASE";

// The method name is derived by replacing case changes with an underscore to
// map the configuration property.
public static final String CONFIG_MAPPING_NAMING_STRATEGY_SNAKE_CASE = "SNAKE_CASE";

public static final String WITH_NAME_ANNOTATION = "io.smallrye.config.WithName";

public static final String WITH_NAME_ANNOTATION_VALUE = "value";
Expand All @@ -107,21 +96,6 @@ public class QuarkusConstants {

public static final String CONFIG_PROPERTIES_ANNOTATION_PREFIX = "prefix";

public static final String CONFIG_PROPERTIES_ANNOTATION_NAMING_STRATEGY = "namingStrategy";

public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM = CONFIG_PROPERTIES_ANNOTATION
+ ".NamingStrategy";

public static final String NAMING_STRATEGY_PREFIX = "NamingStrategy.";

public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_FROM_CONFIG = NAMING_STRATEGY_PREFIX
+ "FROM_CONFIG";

public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_VERBATIM = NAMING_STRATEGY_PREFIX + "VERBATIM";

public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_KEBAB_CASE = NAMING_STRATEGY_PREFIX
+ "KEBAB_CASE";

public static final String QUARKUS_ARC_CONFIG_PROPERTIES_DEFAULT_NAMING_STRATEGY = "quarkus.arc.config-properties-default-naming-strategy";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.redhat.microprofile.psi.internal.quarkus.core.properties;

public enum NamingStrategy {

/**
* The method name is used as is to map the configuration property.
*/
VERBATIM,

/**
* The method name is derived by replacing case changes with a dash to map the configuration property.
*/
KEBAB_CASE,

/**
* The method name is derived by replacing case changes with an underscore to map the configuration property.
*/
SNAKE_CASE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiTreeUtil;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.AbstractAnnotationTypeReferencePropertiesProvider;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.IPropertiesCollector;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.SearchContext;
import com.redhat.devtools.intellij.quarkus.QuarkusConstants;
import com.redhat.microprofile.psi.quarkus.PsiQuarkusUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.lsp4mp.commons.metadata.ItemMetadata;
Expand Down Expand Up @@ -52,8 +54,6 @@
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_ANNOTATION;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_ANNOTATION_PREFIX;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_NAMING_STRATEGY_SNAKE_CASE;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_NAMING_STRATEGY_VERBATIM;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.WITH_DEFAULT_ANNOTATION;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.WITH_DEFAULT_ANNOTATION_VALUE;
import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.WITH_NAME_ANNOTATION;
Expand Down Expand Up @@ -273,18 +273,13 @@ private static String convertName(PsiMember member, PsiAnnotation configMappingA
// ConfigMapping.NamingStrategy.VERBATIM)
// public interface ServerVerbatimNamingStrategy
// --> See https://quarkus.io/guides/config-mappings#namingstrategy
String namingStrategy = getAnnotationMemberValue(configMappingAnnotation,
CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY);
NamingStrategy namingStrategy = getNamingStrategy(configMappingAnnotation);
if (namingStrategy != null) {
int index = namingStrategy.lastIndexOf('.');
if (index != -1) {
namingStrategy = namingStrategy.substring(index + 1);
}
switch (namingStrategy) {
case CONFIG_MAPPING_NAMING_STRATEGY_VERBATIM:
case VERBATIM:
// The method name is used as is to map the configuration property.
return name;
case CONFIG_MAPPING_NAMING_STRATEGY_SNAKE_CASE:
case SNAKE_CASE:
// The method name is derived by replacing case changes with an underscore to
// map the configuration property.
return snake(name);
Expand All @@ -300,6 +295,31 @@ private static String convertName(PsiMember member, PsiAnnotation configMappingA
return hyphenate(name);
}

/**
* Returns the Quarkus @ConfigRoot(phase=...) value.
*
* @param configMappingAnnotation
* @return the Quarkus @ConfigRoot(phase=...) value.
*/
private static NamingStrategy getNamingStrategy(PsiAnnotation configMappingAnnotation) {
// 2) Check if ConfigMapping.NamingStrategy is used
// @ConfigMapping(prefix = "server", namingStrategy =
// ConfigMapping.NamingStrategy.VERBATIM)
// public interface ServerVerbatimNamingStrategy
// --> See https://quarkus.io/guides/config-mappings#namingstrategy
String namingStrategy = getAnnotationMemberValue(configMappingAnnotation,
CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY);
if (namingStrategy != null) {
try {
return NamingStrategy.valueOf(namingStrategy.toUpperCase());
}
catch(Exception e) {

}
}
return null;
}

/**
* Returns the value of @WithDefault("a value") and null otherwise.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private static ConfigProperties.NamingStrategy getNamingStrategy(PsiAnnotation c

@Nullable
private static ConfigProperties.@Nullable NamingStrategy getNamingStrategy(String namingStrategy) {
if (namingStrategy != null) {
if (namingStrategy != null && !namingStrategy.isEmpty()) {
try {
return ConfigProperties.NamingStrategy.valueOf(namingStrategy.toUpperCase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils;
import org.eclipse.lsp4mp.commons.metadata.ItemMetadata;
import io.quarkus.runtime.annotations.ConfigItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jetbrains.annotations.NotNull;

import java.io.Reader;
import java.io.StringReader;
Expand Down Expand Up @@ -120,14 +119,15 @@ private void processConfigRoot(PsiModifierListOwner psiElement, PsiAnnotation co
* @param configRootAnnotation
* @return the Quarkus @ConfigRoot(phase=...) value.
*/
@NotNull
private static ConfigPhase getConfigPhase(PsiAnnotation configRootAnnotation) {
String value = AnnotationUtils.getAnnotationMemberValue(configRootAnnotation, QuarkusConstants.CONFIG_ROOT_ANNOTATION_PHASE);
if (value != null) {
if (value.endsWith(ConfigPhase.RUN_TIME.name())) {
return ConfigPhase.RUN_TIME;
String phase = AnnotationUtils.getAnnotationMemberValue(configRootAnnotation, QuarkusConstants.CONFIG_ROOT_ANNOTATION_PHASE);
if (phase != null) {
try {
return ConfigPhase.valueOf(phase.toUpperCase());
}
if (value.endsWith(ConfigPhase.BUILD_AND_RUN_TIME_FIXED.name())) {
return ConfigPhase.BUILD_AND_RUN_TIME_FIXED;
catch(Exception e) {

}
}
return ConfigPhase.BUILD_TIME;
Expand Down

0 comments on commit ffd9814

Please sign in to comment.