Skip to content

Commit

Permalink
fix(core): MapNode#getString should check if the node is StringRepres…
Browse files Browse the repository at this point in the history
…entable
  • Loading branch information
Siroshun09 committed Mar 23, 2024
1 parent e397b88 commit 3aa77f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public sealed interface MapNode extends CommentableNode<Map<Object, Node<?>>> pe
* @return the {@link String} to which the specified key is mapped, or the specified value if the key is not mapped to {@link StringValue}
*/
default @NotNull String getString(@NotNull Object key, @NotNull String def) {
return this.raw(key) instanceof StringValue value ? value.asString() : def;
return this.raw(key) instanceof StringRepresentable value ? value.asString() : def;
}

/**
Expand All @@ -304,7 +304,7 @@ public sealed interface MapNode extends CommentableNode<Map<Object, Node<?>>> pe
* @return the {@link String} to which the specified key is mapped, or {@code null} if the key is not mapped to {@link StringValue}
*/
default @Nullable String getStringOrNull(@NotNull Object key) {
return this.raw(key) instanceof StringValue value ? value.asString() : null;
return this.raw(key) instanceof StringRepresentable value ? value.asString() : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ protected Stream<NodeTestCase<MapNode>> nodeTestCases() {
assertSame(COMMENT, initial.getComment());
assertSame(COMMENT, view.getComment());
}
),
nodeTest(
"MapNode#getString(String)",
MapNode.create(Map.of("a", "b", 1, 2)),
node -> {
assertEquals("b", node.getString("a"));
assertEquals("2", node.getString(1));
assertEquals("", node.getString("unknown"));
assertEquals("default", node.getString("unknown", "default"));
}
)
);
}
Expand Down

0 comments on commit 3aa77f4

Please sign in to comment.