Skip to content

Commit

Permalink
Safe settings node move
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenicos committed Jan 24, 2024
1 parent 324901b commit 7405387
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/saicone/settings/SettingsNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ default SettingsNode delete(boolean deep) {
default SettingsNode move(@NotNull String... path) {
final MapNode parent = getParent();
if (parent != null) {
parent.remove(getKey());
if (getKey() != null) {
parent.remove(getKey(), true);
}
parent.set(this, path);
} else {
return setKey(path[path.length - 1]);
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/saicone/settings/node/NodeValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@ public void testMapDeepMerge() {

assertEquals(expected, actual);
}

@Test
public void testMapMove() {
MapNode expected = new MapNode();
expected.child("test", "asd");
expected.child("number", 1234);
expected.child("sub", ImmutableMap.of("number", 55, "key", true));

MapNode actual = new MapNode();
actual.child("key1", "asd");
actual.child("key2", ImmutableMap.of("sub1", true));
actual.child("key3", 1234);
actual.child("key4", 55);
actual.child("sub", ImmutableMap.of("number", 55));

actual.get("key1").move("test");
actual.get("key2", "sub1").move("sub", "key");
actual.get("key3").setKey("number");
actual.get("key4").delete();

assertEquals(expected, actual);
}
}

0 comments on commit 7405387

Please sign in to comment.