From a66e87fcdd871ed4893dffece546b4d261c68181 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Tue, 3 Dec 2024 17:37:07 +0000 Subject: [PATCH] Improve reportUnknown allocations --- .../smallrye/config/ConfigMappingContext.java | 68 ++++++++----------- .../java/io/smallrye/config/PropertyName.java | 2 +- .../config/SmallRyeConfigBuilder.java | 4 +- 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java b/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java index 405fd3da4..79ec7cd9e 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java @@ -304,61 +304,51 @@ private static List indexOfDashes(final String mappedProperty, final St return dashesPosition; } - void reportUnknown(final List ignoredPaths) { - KeyMap ignoredProperties = new KeyMap<>(); + void reportUnknown(final Set ignoredPaths) { + Set ignoredNames = new HashSet<>(); + Set ignoredPrefixes = new HashSet<>(); for (String ignoredPath : ignoredPaths) { - KeyMap found; if (ignoredPath.endsWith(".**")) { - found = ignoredProperties.findOrAdd(ignoredPath.substring(0, ignoredPath.length() - 3)); - found.putRootValue(Boolean.TRUE); - ignoreRecursively(found); + ignoredPrefixes.add(ignoredPath.substring(0, ignoredPath.length() - 3)); } else { - if (!ignoredProperties.hasRootValue(ignoredPath)) { - found = ignoredProperties.findOrAdd(ignoredPath); - found.putRootValue(Boolean.TRUE); - } + ignoredNames.add(new PropertyName(ignoredPath)); } } - Set roots = new HashSet<>(); + Set prefixes = new HashSet<>(); for (Map value : this.roots.values()) { - roots.addAll(value.keySet()); + prefixes.addAll(value.keySet()); + } + if (prefixes.contains("")) { + prefixes.clear(); } - for (String name : filterPropertiesInRoots(config.getPropertyNames(), roots)) { - if (usedProperties.contains(name)) { + propertyNames: for (String propertyName : config.getPropertyNames()) { + if (usedProperties.contains(propertyName)) { continue; } - if (!ignoredProperties.hasRootValue(name)) { - ConfigValue configValue = config.getConfigValue(name); - // TODO - https://github.com/quarkusio/quarkus/issues/38479 - if (configValue.getSourceName() != null && configValue.getSourceName().startsWith(EnvConfigSource.NAME)) { - continue; - } - problems.add(new Problem( - ConfigMessages.msg.propertyDoesNotMapToAnyRoot(name, configValue.getLocation()))); + if (ignoredNames.contains(new PropertyName(propertyName))) { + continue; } - } - } - - private static void ignoreRecursively(KeyMap root) { - if (root.getRootValue() == null) { - root.putRootValue(Boolean.TRUE); - } - if (root.getAny() == null) { - //noinspection CollectionAddedToSelf - root.putAny(root); - } else { - var any = root.getAny(); - if (root != any) { - ignoreRecursively(any); + for (String ignoredPrefix : ignoredPrefixes) { + if (propertyName.startsWith(ignoredPrefix)) { + continue propertyNames; + } } - } - for (var value : root.values()) { - ignoreRecursively(value); + for (String prefix : prefixes) { + if (isPropertyInRoot(propertyName, prefix)) { + ConfigValue configValue = config.getConfigValue(propertyName); + // TODO - https://github.com/quarkusio/quarkus/issues/38479 + if (configValue.getSourceName() != null && configValue.getSourceName().startsWith(EnvConfigSource.NAME)) { + continue; + } + problems.add(new Problem( + ConfigMessages.msg.propertyDoesNotMapToAnyRoot(propertyName, configValue.getLocation()))); + } + } } } diff --git a/implementation/src/main/java/io/smallrye/config/PropertyName.java b/implementation/src/main/java/io/smallrye/config/PropertyName.java index f51a3d5df..a7aafee2a 100644 --- a/implementation/src/main/java/io/smallrye/config/PropertyName.java +++ b/implementation/src/main/java/io/smallrye/config/PropertyName.java @@ -32,7 +32,7 @@ static boolean equals(final String name, final String other) { return true; } - if (name.equals("*") && (other.equals("") || other.equals("\"\""))) { + if (name.equals("*") && (other.isEmpty() || other.equals("\"\""))) { return false; } diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java index c8f2a3c9d..403fbeb02 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java @@ -769,7 +769,7 @@ public SmallRyeConfig build() { public final class MappingBuilder { private final Map, Set> mappings = new HashMap<>(); - private final List ignoredPaths = new ArrayList<>(); + private final Set ignoredPaths = new HashSet<>(); private final StringBuilder sb = new StringBuilder(); @@ -818,7 +818,7 @@ public Map, Set> getMappings() { return mappings; } - public List getIgnoredPaths() { + public Set getIgnoredPaths() { return ignoredPaths; }