From 560604b290a1db3bde59e81f8b90103c672e6d9a Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Tue, 19 Sep 2023 15:10:04 +0100 Subject: [PATCH] feat(application): implemented auto model decoding for ApplicationRoleConnectionMetadata.java --- .../model/application/Application.java | 3 +- .../ApplicationRoleConnectionMetadata.java | 120 ++++++++++-------- 2 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/seailz/discordjar/model/application/Application.java b/src/main/java/com/seailz/discordjar/model/application/Application.java index b76469c4..faafab8d 100644 --- a/src/main/java/com/seailz/discordjar/model/application/Application.java +++ b/src/main/java/com/seailz/discordjar/model/application/Application.java @@ -12,6 +12,7 @@ import com.seailz.discordjar.utils.model.DiscordJarProp; import com.seailz.discordjar.utils.model.JSONProp; import com.seailz.discordjar.utils.model.Model; +import com.seailz.discordjar.utils.model.ModelDecoder; import com.seailz.discordjar.utils.rest.DiscordRequest; import com.seailz.discordjar.utils.rest.DiscordResponse; import com.seailz.discordjar.utils.flag.BitwiseUtil; @@ -223,7 +224,7 @@ public List getRoleConnections() { if (response.code() == 200) { return new JSONArray(response.body()).toList().stream() - .map(o -> ApplicationRoleConnectionMetadata.decompile((JSONObject) o)) + .map(o -> (ApplicationRoleConnectionMetadata) ModelDecoder.decodeObject((JSONObject) o, ApplicationRoleConnectionMetadata.class, discordJar)) .toList(); } else { return null; diff --git a/src/main/java/com/seailz/discordjar/model/application/ApplicationRoleConnectionMetadata.java b/src/main/java/com/seailz/discordjar/model/application/ApplicationRoleConnectionMetadata.java index c62cf0bd..17ab2634 100644 --- a/src/main/java/com/seailz/discordjar/model/application/ApplicationRoleConnectionMetadata.java +++ b/src/main/java/com/seailz/discordjar/model/application/ApplicationRoleConnectionMetadata.java @@ -1,11 +1,14 @@ package com.seailz.discordjar.model.application; import com.seailz.discordjar.core.Compilerable; +import com.seailz.discordjar.utils.model.JSONProp; +import com.seailz.discordjar.utils.model.Model; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; import java.util.HashMap; +import java.util.function.Function; /** * A representation of role connection metadata for an application.

@@ -16,7 +19,7 @@ * its metadata will appear in the role verification configuration when the * application has been added as a verification method to the role.

* - * When a user connects their account using the bot's role_connections_verification_url, + * When a user connects their account using the bots role_connections_verification_url, * the bot will update a user's role connection with metadata * using the OAuth2 {@code role_connections.write} scope. * @@ -24,65 +27,72 @@ * @since 1.0 * @see Application */ -public record ApplicationRoleConnectionMetadata( - Type type, - String key, - String name, - HashMap nameLocalizations, - String description, - HashMap descriptionLocalizations -) implements Compilerable { + +public class ApplicationRoleConnectionMetadata implements Model { + @JSONProp("type") + private Type type; + @JSONProp("key") + private String key; + @JSONProp("name") + private String name; + @JSONProp("name_localizations") + private HashMap nameLocalizations; + @JSONProp("description") + private String description; + @JSONProp("description_localizations") + private HashMap descriptionLocalizations; + @Override - public JSONObject compile() { - JSONObject json = new JSONObject(); - json.put("type", type.getCode()); - json.put("key", key); - json.put("name", name); - if (!nameLocalizations.isEmpty()) json.put("name_localizations", new JSONObject(nameLocalizations)); - json.put("description", description); - if (!descriptionLocalizations.isEmpty()) json.put("description_localizations", new JSONObject(descriptionLocalizations)); - return json; + public HashMap> customDecoders() { + return new HashMap<>(){{ + put("name_localizations", (json) -> { + if (json.has("name_localizations")) { + JSONObject nameLocalizationsJson = json.getJSONObject("name_localizations"); + HashMap nameLocalizations = new HashMap<>(); + for (String key1 : nameLocalizationsJson.keySet()) { + nameLocalizations.put(key1, nameLocalizationsJson.getString(key1)); + } + return nameLocalizations; + } + return null; + }); + + put("description_localizations", (json) -> { + if (json.has("description_localizations")) { + JSONObject descriptionLocalizationsJson = json.getJSONObject("description_localizations"); + HashMap descriptionLocalizations = new HashMap<>(); + for (String key1 : descriptionLocalizationsJson.keySet()) { + descriptionLocalizations.put(key1, descriptionLocalizationsJson.getString(key1)); + } + return descriptionLocalizations; + } + return null; + }); + }}; } - @NotNull - @Contract("_ -> new") - public static ApplicationRoleConnectionMetadata decompile(@NotNull JSONObject json) { - Type type = json.has("type") ? Type.fromCode(json.getInt("type")) : null; - String key = json.has("key") ? json.getString("key") : null; - String name = json.has("name") ? json.getString("name") : null; - HashMap nameLocalizations; - - if (json.has("name_localizations")) { - JSONObject nameLocalizationsJson = json.getJSONObject("name_localizations"); - nameLocalizations = new HashMap<>(); - for (String key1 : nameLocalizationsJson.keySet()) { - nameLocalizations.put(key1, nameLocalizationsJson.getString(key1)); - } - } else { - nameLocalizations = null; - } + public Type type() { + return type; + } + + public String key() { + return key; + } - String description = json.has("description") ? json.getString("description") : null; - HashMap descriptionLocalizations; + public String name() { + return name; + } - if (json.has("description_localizations")) { - JSONObject descriptionLocalizationsJson = json.getJSONObject("description_localizations"); - descriptionLocalizations = new HashMap<>(); - for (String key1 : descriptionLocalizationsJson.keySet()) { - descriptionLocalizations.put(key1, descriptionLocalizationsJson.getString(key1)); - } - } else { - descriptionLocalizations = null; - } + public HashMap nameLocalizations() { + return nameLocalizations; + } + + public String description() { + return description; + } - return new ApplicationRoleConnectionMetadata( - type == null ? Type.UNKNOWN : type, - key, - name, - nameLocalizations, - description, - descriptionLocalizations - ); + public HashMap descriptionLocalizations() { + return descriptionLocalizations; } public ApplicationRoleConnectionMetadata(Type type, String key, String name, HashMap nameLocalizations, String description, HashMap descriptionLocalizations) { @@ -98,6 +108,8 @@ public ApplicationRoleConnectionMetadata(Type type, String key, String name, Str this(type, key, name, new HashMap<>(), description, new HashMap<>()); } + private ApplicationRoleConnectionMetadata() {} + public ApplicationRoleConnectionMetadata addNameLocalization(String language, String name) { this.nameLocalizations.put(language, name); return this;