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

check duplicate keys #1270

Merged
merged 3 commits into from
Dec 20, 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 @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.invoke.MethodHandles;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
Expand All @@ -14,7 +15,9 @@
import java.util.Set;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.logging.Logger;

import io.smallrye.config._private.ConfigLogging;
import io.smallrye.config._private.ConfigMessages;

/**
Expand Down Expand Up @@ -340,6 +343,9 @@ final class ConfigValueProperties extends HashMap<String, ConfigValue> {
private final String configSourceName;
private final int configSourceOrdinal;

private static ConfigLogging log = Logger.getMessageLogger(MethodHandles.lookup(), ConfigLogging.class,
"io.smallrye.config");

public ConfigValueProperties(final String configSourceName, final int configSourceOrdinal) {
this.configSourceName = configSourceName;
this.configSourceOrdinal = configSourceOrdinal;
Expand Down Expand Up @@ -401,14 +407,18 @@ private void load0(LineReader lr) throws IOException {
}
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, ConfigValue.builder()
ConfigValue oldConfigValue = put(key, ConfigValue.builder()
.withName(key)
.withValue(value)
.withRawValue(value)
.withConfigSourceName(configSourceName)
.withConfigSourceOrdinal(configSourceOrdinal)
.withLineNumber(lr.lineNumber)
.build());
if (oldConfigValue != null) {
ConfigLogging.log.duplicateValue(oldConfigValue.getName(), oldConfigValue.getConfigSourceName(),
oldConfigValue.getValue());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public interface ConfigLogging extends BasicLogger {
@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 1006, value = "Loaded ConfigSource %s with ordinal %d")
void loadedConfigSource(String name, int ordinal);

@LogMessage(level = Logger.Level.WARN)
@Message(id = 1007, value = "Duplicate value found for name : %s, config source name : %s, (old value : %s)")
void duplicateValue(String key, String sourceName, String oldValue);

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ void wrapValue() throws Exception {
assertEquals(2, map.get("key2").getLineNumber());
assertEquals(6, map.get("key3").getLineNumber());
}

@Test
void logDuplicateValue() throws Exception {
ConfigValueProperties map = new ConfigValueProperties("config", 1);
String config = "key=value\n" +
"key2=value\n" +
"key2=value2\n";
map.load(new StringReader(config));

// property expected with the last value found
assertEquals("value2", map.get("key2").getValue());
}

}
Loading