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

Apply the same NamingStrategy to all groups in a ConfigMapping root. #545

Merged
merged 1 commit into from
Apr 9, 2021
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 @@ -26,6 +26,7 @@
import org.eclipse.microprofile.config.spi.Converter;

import io.smallrye.config.ConfigMappingInterface.CollectionProperty;
import io.smallrye.config.ConfigMappingInterface.NamingStrategy;

/**
* A mapping context. This is used by generated classes during configuration mapping, and is released once the configuration
Expand All @@ -42,6 +43,8 @@ public final class ConfigMappingContext {
private final StringBuilder stringBuilder = new StringBuilder();
private final ArrayList<Problem> problems = new ArrayList<>();

private NamingStrategy namingStrategy = null;

ConfigMappingContext(final SmallRyeConfig config) {
this.config = config;
}
Expand All @@ -68,6 +71,11 @@ public void registerEnclosedField(Class<?> enclosingType, String key, Object enc
.put(enclosingObject, value);
}

public <T> T constructRoot(Class<T> interfaceType) {
this.namingStrategy = ConfigMappingInterface.getConfigurationInterface(interfaceType).getNamingStrategy();
return constructGroup(interfaceType);
}

public <T> T constructGroup(Class<T> interfaceType) {
final T mappingObject = ConfigMappingLoader.configMappingObject(interfaceType, this);
allInstances.add((ConfigMappingObject) mappingObject);
Expand Down Expand Up @@ -179,6 +187,10 @@ public <T> Converter<T> getConverterInstance(Class<? extends Converter<? extends
});
}

public String applyNamingStrategy(final String name) {
return namingStrategy.apply(name);
}

public static IntFunction<Collection<?>> createCollectionFactory(final Class<?> type) {
if (type == List.class) {
return ArrayList::new;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ private static void addProperties(
// register the group
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);
ctor.visitLdcInsn(Type.getType(mapping.getInterfaceType()));
ctor.visitLdcInsn(property.getPropertyName(mapping.getNamingStrategy()));
ctor.visitLdcInsn(property.getPropertyName());
ctor.visitVarInsn(Opcodes.ALOAD, V_THIS);
ctor.visitVarInsn(Opcodes.ALOAD, V_THIS);
ctor.visitFieldInsn(Opcodes.GETFIELD, className, memberName, fieldDesc);
Expand All @@ -637,7 +637,6 @@ private static void addProperties(
// stack: this config key
// get the converter to use
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);
// public <T> Converter<T> getValueConverter(Class<?> enclosingType, String field) {
ctor.visitLdcInsn(getType(mapping.getInterfaceType()));
ctor.visitLdcInsn(memberName);
ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_MAPPING_CONTEXT, "getValueConverter",
Expand Down Expand Up @@ -782,14 +781,13 @@ private static boolean appendPropertyName(final MethodVisitor ctor, final Config

ctor.visitVarInsn(Opcodes.ALOAD, V_STRING_BUILDER);

// stack: sb
// TODO - NammingStrategy
// The NamingStrategy comes from the current mapping interface. We don't support setting a NamingStrategy in
// the top of the config root for all the configs in that root to inherit the same NamingStrategy. This needs
// to be handled per instance (since groups may belong to different roots), so the NamingStrategy should be
// retrieved from the Context. This is just a first implementation that could move into that direction, which
// needs more work.
ctor.visitLdcInsn(property.getPropertyName(mapping.getNamingStrategy()));
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);

ctor.visitLdcInsn(property.getPropertyName());

ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_MAPPING_CONTEXT,
"applyNamingStrategy", "(L" + I_STRING + ";)L" + I_STRING + ";", false);

// stack: sb name
ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_STRING + ";)L" + I_STRING_BUILDER + ';', false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,10 @@ public Method getMethod() {
}

public String getPropertyName() {
return getPropertyName(KEBAB_CASE_NAMING_STRATEGY);
}

public String getPropertyName(final NamingStrategy namingStrategy) {
if (isParentPropertyName()) {
return propertyName;
}
return hasPropertyName() && !propertyName.isEmpty() ? propertyName : namingStrategy.apply(method.getName());
return hasPropertyName() && !propertyName.isEmpty() ? propertyName : method.getName();
}

public boolean hasPropertyName() {
Expand Down Expand Up @@ -529,7 +525,6 @@ public static final class CollectionProperty extends MayBeOptionalProperty {
private final Property element;

CollectionProperty(final Class<?> collectionType, final Property element) {
// TODO - Naming Strategy
super(element.getMethod(), element.getPropertyName());
this.collectionRawType = collectionType;
this.element = element;
Expand Down
Loading