Migrate to type-safe model ID structs

This commit is contained in:
Ske
2020-06-14 21:37:04 +02:00
parent e5ac5edc35
commit b9cbd241de
21 changed files with 167 additions and 41 deletions

View File

@@ -0,0 +1,11 @@
using System;
namespace PluralKit.Core
{
public interface INumericId<T, out TInner>: IEquatable<T>, IComparable<T>
where T: INumericId<T, TInner>
where TInner: IEquatable<TInner>, IComparable<TInner>
{
public TInner Value { get; }
}
}

View File

@@ -3,7 +3,7 @@ namespace PluralKit.Core
{
public class MemberGuildSettings
{
public int Member { get; }
public MemberId Member { get; }
public ulong Guild { get; }
public string? DisplayName { get; }
public string? AvatarUrl { get; }

View File

@@ -0,0 +1,24 @@
namespace PluralKit.Core
{
public readonly struct MemberId: INumericId<MemberId, int>
{
public int Value { get; }
public MemberId(int value)
{
Value = value;
}
public bool Equals(MemberId other) => Value == other.Value;
public override bool Equals(object obj) => obj is MemberId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(MemberId left, MemberId right) => left.Equals(right);
public static bool operator !=(MemberId left, MemberId right) => !left.Equals(right);
public int CompareTo(MemberId other) => Value.CompareTo(other.Value);
}
}

View File

@@ -7,22 +7,22 @@ namespace PluralKit.Core
{
public static class ModelQueryExt
{
public static Task<PKSystem?> QuerySystem(this IPKConnection conn, int id) =>
public static Task<PKSystem?> QuerySystem(this IPKConnection conn, SystemId id) =>
conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new {id});
public static Task<PKMember?> QueryMember(this IPKConnection conn, int id) =>
public static Task<PKMember?> QueryMember(this IPKConnection conn, MemberId id) =>
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new {id});
public static Task<GuildConfig> QueryOrInsertGuildConfig(this IPKConnection conn, ulong guild) =>
conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild});
public static Task<SystemGuildSettings> QueryOrInsertSystemGuildConfig(this IPKConnection conn, ulong guild, int system) =>
public static Task<SystemGuildSettings> QueryOrInsertSystemGuildConfig(this IPKConnection conn, ulong guild, SystemId system) =>
conn.QueryFirstAsync<SystemGuildSettings>(
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
new {guild, system});
public static Task<MemberGuildSettings> QueryOrInsertMemberGuildConfig(
this IPKConnection conn, ulong guild, int member) =>
this IPKConnection conn, ulong guild, MemberId member) =>
conn.QueryFirstAsync<MemberGuildSettings>(
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
new {guild, member});

View File

@@ -8,9 +8,9 @@ using NodaTime.Text;
namespace PluralKit.Core {
public class PKMember
{
public int Id { get; }
public MemberId Id { get; }
public string Hid { get; set; }
public int System { get; set; }
public SystemId System { get; set; }
public string Color { get; set; }
public string AvatarUrl { get; set; }
public string Name { get; set; }

View File

@@ -3,8 +3,8 @@
namespace PluralKit.Core {
public class PKSwitch
{
public int Id { get; }
public int System { get; set; }
public SwitchId Id { get; }
public SystemId System { get; set; }
public Instant Timestamp { get; }
}
}

View File

@@ -8,7 +8,7 @@ namespace PluralKit.Core {
public class PKSystem
{
// Additions here should be mirrored in SystemStore::Save
[Key] public int Id { get; }
[Key] public SystemId Id { get; }
public string Hid { get; }
public string Name { get; set; }
public string Description { get; set; }

View File

@@ -0,0 +1,24 @@
namespace PluralKit.Core
{
public readonly struct SwitchId: INumericId<SwitchId, int>
{
public int Value { get; }
public SwitchId(int value)
{
Value = value;
}
public bool Equals(SwitchId other) => Value == other.Value;
public override bool Equals(object obj) => obj is SwitchId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(SwitchId left, SwitchId right) => left.Equals(right);
public static bool operator !=(SwitchId left, SwitchId right) => !left.Equals(right);
public int CompareTo(SwitchId other) => Value.CompareTo(other.Value);
}
}

View File

@@ -2,10 +2,10 @@
{
public class SystemGuildSettings
{
public ulong Guild { get; }
public SystemId Guild { get; }
public bool ProxyEnabled { get; } = true;
public AutoproxyMode AutoproxyMode { get; } = AutoproxyMode.Off;
public int? AutoproxyMember { get; }
public MemberId? AutoproxyMember { get; }
}
}

View File

@@ -0,0 +1,24 @@
namespace PluralKit.Core
{
public readonly struct SystemId: INumericId<SystemId, int>
{
public int Value { get; }
public SystemId(int value)
{
Value = value;
}
public bool Equals(SystemId other) => Value == other.Value;
public override bool Equals(object obj) => obj is SystemId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(SystemId left, SystemId right) => left.Equals(right);
public static bool operator !=(SystemId left, SystemId right) => !left.Equals(right);
public int CompareTo(SystemId other) => Value.CompareTo(other.Value);
}
}