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

fix: naming strategy #1403

Merged
merged 1 commit into from
Oct 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading