Skip to content

Commit

Permalink
Apply the same NamingStrategy to all groups in a ConfigMapping root. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Apr 27, 2021
1 parent 7b766a0 commit 0e81400
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 58 deletions.
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 @@ -177,6 +185,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 @@ -395,7 +395,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 @@ -420,7 +420,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 @@ -565,14 +564,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 @@ -167,14 +167,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 @@ -530,7 +526,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

0 comments on commit 0e81400

Please sign in to comment.