Skip to content
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
22 changes: 17 additions & 5 deletions src/main/java/in/dragonbra/javasteam/types/KeyValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("" +
Expand Down