Skip to content

Commit

Permalink
fix: naming strategy
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 226177a commit d26d737
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ public class QuarkusConstants {
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";

public static final String QUARKUS_DEPLOYMENT_BUILDSTEP_ANNOTATION = "io.quarkus.deployment.annotations.BuildStep";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.redhat.microprofile.psi.internal.quarkus.core.properties;

import io.quarkus.runtime.util.StringUtil;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigProperties {
String UNSET_PREFIX = "<< unset >>";

String prefix() default "<< unset >>";

NamingStrategy namingStrategy() default ConfigProperties.NamingStrategy.FROM_CONFIG;

public static enum NamingStrategy {
FROM_CONFIG {
public String getName(String name) {
throw new IllegalStateException("The naming strategy needs to substituted with the configured naming strategy");
}
},
VERBATIM {
public String getName(String name) {
return name;
}
},
KEBAB_CASE {
public String getName(String name) {
return StringUtil.hyphenate(name);
}
};

private NamingStrategy() {
}

public abstract String getName(String var1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.SearchContext;
import org.eclipse.lsp4mp.commons.metadata.ItemMetadata;
import io.quarkus.deployment.bean.JavaBeanUtil;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -100,10 +101,7 @@ public String getDefaultNamingStrategy() {
defaultNamingStrategy = PsiMicroProfileProjectManager.getInstance(javaProject.getProject())
.getMicroProfileProject(javaProject)
.getProperty(QuarkusConstants.QUARKUS_ARC_CONFIG_PROPERTIES_DEFAULT_NAMING_STRATEGY, null);
if (defaultNamingStrategy != null) {
defaultNamingStrategy = QuarkusConstants.NAMING_STRATEGY_PREFIX
+ defaultNamingStrategy.trim().toUpperCase();
} else {
if (defaultNamingStrategy == null) {
defaultNamingStrategy = "";
}
}
Expand Down Expand Up @@ -300,15 +298,14 @@ private static String convertName(String name, PsiMember member, PsiAnnotation c
}
// Quarkus >=1.2
// check if the ConfigProperties use the namingStrategy
String namingStrategy = getAnnotationMemberValue(configPropertiesAnnotation,
QuarkusConstants.CONFIG_PROPERTIES_ANNOTATION_NAMING_STRATEGY);
ConfigProperties.NamingStrategy namingStrategy = getNamingStrategy(configPropertiesAnnotation);
if (namingStrategy != null) {
switch (namingStrategy) {
case QuarkusConstants.CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_FROM_CONFIG:
case FROM_CONFIG:
return convertDefaultName(name, configPropertiesContext);
case QuarkusConstants.CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_VERBATIM:
case VERBATIM:
return name;
case QuarkusConstants.CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_KEBAB_CASE:
case KEBAB_CASE:
return hyphenate(name);
}
}
Expand All @@ -317,8 +314,8 @@ private static String convertName(String name, PsiMember member, PsiAnnotation c
}

private static String convertDefaultName(String name, ConfigPropertiesContext configPropertiesContext) {
if (QuarkusConstants.CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_VERBATIM
.equals(configPropertiesContext.getDefaultNamingStrategy())) {
ConfigProperties.NamingStrategy namingStrategy = getNamingStrategy(configPropertiesContext.getDefaultNamingStrategy());
if (namingStrategy != null && namingStrategy == ConfigProperties.NamingStrategy.VERBATIM) {
// the application.properties declares :
// quarkus.arc.config-properties-default-naming-strategy = verbatim
return name;
Expand Down Expand Up @@ -373,4 +370,30 @@ private static String getPrefixFromClassName(PsiClass className) {
return join("-", withoutSuffix(lowerCase(camelHumpsIterator(simpleName)), "config", "configuration",
"properties", "props"));
}

/**
* Returns the Quarkus @ConfigProperties(namingStrategy=...) value.
*
* @param configPropertiesAnnotation
* @return the Quarkus @ConfigProperties(namingStrategy=...) value.
*/
@Nullable
private static ConfigProperties.NamingStrategy getNamingStrategy(PsiAnnotation configPropertiesAnnotation) {
String namingStrategy = getAnnotationMemberValue(configPropertiesAnnotation,
QuarkusConstants.CONFIG_PROPERTIES_ANNOTATION_NAMING_STRATEGY);
return getNamingStrategy(namingStrategy);
}

@Nullable
private static ConfigProperties.@Nullable NamingStrategy getNamingStrategy(String namingStrategy) {
if (namingStrategy != null) {
try {
return ConfigProperties.NamingStrategy.valueOf(namingStrategy.toUpperCase());
}
catch(Exception e) {

}
}
return null;
}
}

0 comments on commit d26d737

Please sign in to comment.