diff --git a/src/main/java/in/dragonbra/javasteam/types/KeyValue.kt b/src/main/java/in/dragonbra/javasteam/types/KeyValue.kt index 0ca5f071..2a05e285 100644 --- a/src/main/java/in/dragonbra/javasteam/types/KeyValue.kt +++ b/src/main/java/in/dragonbra/javasteam/types/KeyValue.kt @@ -42,13 +42,25 @@ class KeyValue @JvmOverloads constructor( * @param key key * @return the child [KeyValue] */ - operator fun get(key: String): KeyValue = children.firstOrNull { - it.name?.equals(key, ignoreCase = true) == true - } ?: INVALID + operator fun get(key: String): KeyValue { + children.forEach { c -> + if (c.name?.equals(key, ignoreCase = true) == true) { + return c + } + } + + return INVALID + } + /** + * Sets the child [KeyValue] with the specified key. + * If no child with the given key exists, [KeyValue.INVALID] is returned. + * @param key key + * @param value the child [KeyValue] + */ operator fun set(key: String, value: KeyValue) { - // Remove existing key if it exists - children.removeAll { it.name?.equals(key, ignoreCase = true) == true } + // if the key already exists, remove the old one + children.removeIf { c -> c.name?.equals(key, ignoreCase = true) == true } // Ensure the given KV has the correct key assigned value.name = key diff --git a/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java b/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java index 3918cfed..90704cf4 100644 --- a/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java +++ b/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java @@ -81,6 +81,20 @@ public void keyValueIndexerUpdatesKey() { Assertions.assertEquals("subkey", kv.get("subkey").getName()); } + @Test + public void keyValueIndexerHandlesCaseInsensitiveDuplicates() { + var kv = new KeyValue(); + + kv.set("key", new KeyValue()); + Assertions.assertEquals(1, kv.getChildren().size()); + + kv.set("KEY", new KeyValue()); // Different case + Assertions.assertEquals(1, kv.getChildren().size()); // Should still be 1, not 2 + + kv.set("Key", new KeyValue()); // Another variation + Assertions.assertEquals(1, kv.getChildren().size()); // Should still be 1 + } + @Test public void keyValueLoadsFromString() { KeyValue kv = KeyValue.loadFromString("" +