Last active
March 25, 2022 01:16
-
-
Save youngkidwarrior/152cc59d4635c9d47be307ddddb15565 to your computer and use it in GitHub Desktop.
Wrapping abstract types into Variants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Discord_Snowflake | |
// Client | |
type clientT | |
type client = Client(clientT) | |
//Role | |
type roleT | |
type roleName = RoleName(string) | |
type reason = Reason(string) | |
type colorResolvable = String(string) | |
type roleData = {name: roleName, color: colorResolvable} | |
type role = {t: roleT, name: roleName} | |
//Guild | |
//RoleManager | |
type guildT | |
type roleManagerT | |
type guildName = GuildName(string) | |
type createRoleOptions = {data: roleData, reason: reason} | |
type rec guild = { | |
t: guildT, | |
id: snowflake, | |
name: guildName, | |
roles: roleManager, | |
} | |
and roleManager = { | |
t: roleManagerT, | |
cache: Belt.Map.t<snowflake, role, SnowflakeCompare.identity>, | |
guild: guild, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec wrapGuild = (guild): Types.guild => { | |
open Discord_Guild | |
let id = guild->getGuildId->Discord_Snowflake.Snowflake | |
let name = guild->getGuildName->Types.GuildName | |
let roles = guild->getGuildRoleManager->wrapRoleManager | |
{ | |
id: id, | |
name: name, | |
roles: roles, | |
} | |
} | |
and wrapRoleManager = (roleManager): Types.roleManager => { | |
open Discord_RoleManager | |
let keys = | |
roleManager | |
->getCache | |
->Discord_Collection.keyArray | |
->Belt.Array.map(key => Discord_Snowflake.Snowflake(key)) | |
let values = roleManager->getCache->Discord_Collection.array->Belt.Array.map(wrapRole) | |
let cache = | |
Belt.Array.zip(keys, values)->Belt.Map.fromArray(~id=module(Discord_Snowflake.SnowflakeCompare)) | |
let guild = roleManager->getGuild->wrapGuild | |
{t: roleManager, cache: cache, guild: guild} | |
} | |
and wrapRole = (role): Types.role => { | |
open Discord_Role | |
let name = role->getName | |
{t: role, name: Types.RoleName(name)} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment