-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Labels
2.19Issues planned at 2.19 or laterIssues planned at 2.19 or later
Description
NOTE: considered part of JSTEP-3
So I had this idea of how nice it would be to have a getter method that returns an optional JsonNode, like #path(String name)
does, but when returning an Optional, it would be simpler to only use a node when its actually present, or handle cases that its not present in.
Current Solution:
this.guild = data.has("guild_id") ? discord.getServer(data.get("guild_id").asLong()) : null;
With optional nodes:
this.guild = data.getOptional("guild_id").map(node -> discord.getServer(node.asLong())).orElse(null);
In this scenario, the difference is counter intuitive, because I can use a ternary-if in this case.
See this scenario:
Without the addition:
long afkChannelId = data.path("afk_channel_id").asLong(-1);
if (afkChannel == null || afkChannel.getId() != afkChannelId) {
afkChannel = afkChannelId == -1 ? null : discord.getChannelCache()
.getOrRequest(afkChannelId, afkChannelId);
traits.add(AFK_CHANNEL);
}
With the addition:
Optional<JsonNode> node = data.getOptional("afk_channel_id");
if (afkChannel == null || node.map(idN -> idN.asLong() == afkChannel.getId()).orElse(false)) {
afkChannel = node.map(json -> discord.getChannelCache().getOrRequest(json.asLong(), json.asLong())).orElse(null);
}
I think this scenario could show why the addition could be helpful.
If this idea gets approved and it is wished for; I could work on a PR myself.
Metadata
Metadata
Assignees
Labels
2.19Issues planned at 2.19 or laterIssues planned at 2.19 or later