refactor: change ulong[] to List<ulong> for better ergonomics

This commit is contained in:
sam 2024-11-18 21:01:52 +01:00
parent e12bd6194b
commit 4eb5c16451
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
8 changed files with 37 additions and 46 deletions

View file

@ -112,11 +112,12 @@ public class DatabasePool
SqlMapper.RemoveTypeMap(typeof(ulong));
SqlMapper.AddTypeHandler(new UlongEncodeAsLongHandler());
SqlMapper.AddTypeHandler(new UlongArrayHandler());
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.ChannelConfig>());
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.MessageConfig>());
SqlMapper.AddTypeHandler(new UlongListHandler());
}
// Copied from PluralKit:
@ -132,34 +133,34 @@ public class DatabasePool
private class UlongEncodeAsLongHandler : SqlMapper.TypeHandler<ulong>
{
public override void SetValue(IDbDataParameter parameter, ulong value) =>
parameter.Value = (long)value;
public override ulong Parse(object value) =>
// Cast to long to unbox, then to ulong (???)
(ulong)(long)value;
public override void SetValue(IDbDataParameter parameter, ulong value) =>
parameter.Value = (long)value;
}
private class UlongArrayHandler : SqlMapper.TypeHandler<ulong[]>
private class UlongListHandler : SqlMapper.TypeHandler<List<ulong>>
{
public override void SetValue(IDbDataParameter parameter, ulong[]? value) =>
parameter.Value = value != null ? Array.ConvertAll(value, i => (long)i) : null;
public override void SetValue(IDbDataParameter parameter, List<ulong>? value) =>
parameter.Value = value?.Select(i => (long)i).ToArray();
public override ulong[] Parse(object value) =>
Array.ConvertAll((long[])value, i => (ulong)i);
public override List<ulong>? Parse(object value) =>
((long[])value).Select(i => (ulong)i).ToList();
}
private class JsonTypeHandler<T> : SqlMapper.TypeHandler<T>
{
public override void SetValue(IDbDataParameter parameter, T? value) =>
parameter.Value = JsonSerializer.Serialize(value);
public override T Parse(object value)
{
var json = (string)value;
return JsonSerializer.Deserialize<T>(json)
?? throw new CataloggerError("JsonTypeHandler<T> returned null");
}
public override void SetValue(IDbDataParameter parameter, T? value) =>
parameter.Value = JsonSerializer.Serialize(value);
}
}

View file

@ -6,7 +6,7 @@ public record ConfigExport(
ulong Id,
ChannelsBackup Channels,
string[] BannedSystems,
ulong[] KeyRoles,
List<ulong> KeyRoles,
IEnumerable<InviteExport> Invites,
IEnumerable<WatchlistExport> Watchlist
);

View file

@ -26,11 +26,11 @@ public class Guild
public ChannelConfig Channels { get; init; } = new();
public MessageConfig Messages { get; init; } = new();
public string[] BannedSystems { get; set; } = [];
public ulong[] KeyRoles { get; set; } = [];
public List<ulong> KeyRoles { get; set; } = [];
// These channels and roles are ignored for channel/role update/delete events.
public ulong[] IgnoredChannels { get; set; } = [];
public ulong[] IgnoredRoles { get; set; } = [];
public List<ulong> IgnoredChannels { get; set; } = [];
public List<ulong> IgnoredRoles { get; set; } = [];
public bool IsSystemBanned(PluralkitApiService.PkSystem system) =>
BannedSystems.Contains(system.Id) || BannedSystems.Contains(system.Uuid.ToString());

View file

@ -119,25 +119,14 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
private record BannedSystem(Guid SystemId, ulong UserId, ulong GuildId);
public async Task AddKeyRoleAsync(Snowflake guildId, Snowflake roleId) =>
await conn.ExecuteAsync(
"update guilds set key_roles = array_append(key_roles, @RoleId) where id = @GuildId",
new { GuildId = guildId.Value, RoleId = roleId.Value }
);
public async Task RemoveKeyRoleAsync(Snowflake guildId, Snowflake roleId) =>
await conn.ExecuteAsync(
"update guilds set key_roles = array_remove(key_roles, @RoleId) where id = @GuildId",
new { GuildId = guildId.Value, RoleId = roleId.Value }
);
public async Task UpdateConfigAsync(Snowflake id, Guild config) =>
await conn.ExecuteAsync(
"""
update guilds set channels = @Channels::jsonb,
messages = @Messages::jsonb,
ignored_channels = @IgnoredChannels,
ignored_roles = @IgnoredRoles
ignored_roles = @IgnoredRoles,
key_roles = @KeyRoles
where id = @Id
""",
new
@ -147,6 +136,7 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
config.Messages,
config.IgnoredChannels,
config.IgnoredRoles,
config.KeyRoles,
}
);
@ -155,7 +145,7 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
Guild.ChannelConfig channels,
Guild.MessageConfig messages,
string[] bannedSystems,
ulong[] keyRoles
List<ulong> keyRoles
) =>
await conn.ExecuteAsync(
"update guilds set channels = @channels::jsonb, messages = @messages::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",