refactor: move modeltypes to own folder, move IDs to file of respective type
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public readonly struct GroupId: INumericId<GroupId, int>
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
public GroupId(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public bool Equals(GroupId other) => Value == other.Value;
|
||||
|
||||
public override bool Equals(object obj) => obj is GroupId other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Value;
|
||||
|
||||
public static bool operator ==(GroupId left, GroupId right) => left.Equals(right);
|
||||
|
||||
public static bool operator !=(GroupId left, GroupId right) => !left.Equals(right);
|
||||
|
||||
public int CompareTo(GroupId other) => Value.CompareTo(other.Value);
|
||||
|
||||
public override string ToString() => $"Group #{Value}";
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
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);
|
||||
|
||||
public override string ToString() => $"Member #{Value}";
|
||||
}
|
||||
}
|
89
PluralKit.Core/Models/ModelTypes/Partial.cs
Normal file
89
PluralKit.Core/Models/ModelTypes/Partial.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
[JsonConverter(typeof(PartialConverter))]
|
||||
public struct Partial<T>: IEnumerable<T>, IPartial
|
||||
{
|
||||
public bool IsPresent { get; }
|
||||
public T Value { get; }
|
||||
public object? RawValue => Value;
|
||||
|
||||
private Partial(bool isPresent, T value)
|
||||
{
|
||||
IsPresent = isPresent;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public static Partial<T> Null() => new Partial<T>(true, default!);
|
||||
public static Partial<T> Present(T obj) => new Partial<T>(true, obj);
|
||||
public static Partial<T> Absent = new Partial<T>(false, default!);
|
||||
|
||||
public IEnumerable<T> ToArray() => IsPresent ? new[] {Value} : new T[0];
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => ToArray().GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => ToArray().GetEnumerator();
|
||||
|
||||
public static implicit operator Partial<T>(T val) => Present(val);
|
||||
}
|
||||
|
||||
public interface IPartial
|
||||
{
|
||||
public bool IsPresent { get; }
|
||||
public object? RawValue { get; }
|
||||
}
|
||||
|
||||
public class PartialConverter: JsonConverter
|
||||
{
|
||||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue,
|
||||
JsonSerializer serializer)
|
||||
{
|
||||
var innerType = objectType.GenericTypeArguments[0];
|
||||
var innerValue = serializer.Deserialize(reader, innerType);
|
||||
|
||||
return typeof(Partial<>)
|
||||
.MakeGenericType(innerType)
|
||||
.GetMethod(nameof(Partial<object>.Present))!
|
||||
.Invoke(null, new[] {innerValue});
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public override bool CanConvert(Type objectType) => true;
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanWrite => false;
|
||||
}
|
||||
|
||||
public static class PartialExt
|
||||
{
|
||||
public static bool TryGet<T>(this Partial<T> pt, out T value)
|
||||
{
|
||||
value = pt.IsPresent ? pt.Value : default!;
|
||||
return pt.IsPresent;
|
||||
}
|
||||
|
||||
public static T Or<T>(this Partial<T> pt, T fallback) => pt.IsPresent ? pt.Value : fallback;
|
||||
public static T Or<T>(this Partial<T> pt, Func<T> fallback) => pt.IsPresent ? pt.Value : fallback.Invoke();
|
||||
|
||||
public static Partial<TOut> Map<TIn, TOut>(this Partial<TIn> pt, Func<TIn, TOut> fn) =>
|
||||
pt.IsPresent ? Partial<TOut>.Present(fn.Invoke(pt.Value)) : Partial<TOut>.Absent;
|
||||
|
||||
public static void Apply<T>(this Partial<T> pt, DynamicParameters bag, QueryBuilder qb, string fieldName)
|
||||
{
|
||||
if (!pt.IsPresent) return;
|
||||
|
||||
bag.Add(fieldName, pt.Value);
|
||||
qb.Variable(fieldName, $"@{fieldName}");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +1,34 @@
|
||||
using NodaTime;
|
||||
|
||||
#nullable enable
|
||||
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public readonly struct GroupId: INumericId<GroupId, int>
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
public GroupId(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public bool Equals(GroupId other) => Value == other.Value;
|
||||
|
||||
public override bool Equals(object obj) => obj is GroupId other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Value;
|
||||
|
||||
public static bool operator ==(GroupId left, GroupId right) => left.Equals(right);
|
||||
|
||||
public static bool operator !=(GroupId left, GroupId right) => !left.Equals(right);
|
||||
|
||||
public int CompareTo(GroupId other) => Value.CompareTo(other.Value);
|
||||
|
||||
public override string ToString() => $"Group #{Value}";
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
public class PKGroup
|
||||
{
|
||||
public GroupId Id { get; private set; }
|
||||
|
@@ -5,7 +5,33 @@ using Newtonsoft.Json;
|
||||
using NodaTime;
|
||||
using NodaTime.Text;
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
public override string ToString() => $"Member #{Value}";
|
||||
}
|
||||
|
||||
public class PKMember
|
||||
{
|
||||
// Dapper *can* figure out mapping to getter-only properties, but this doesn't work
|
||||
|
@@ -1,6 +1,33 @@
|
||||
using NodaTime;
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
public override string ToString() => $"Switch #{Value}";
|
||||
}
|
||||
|
||||
public class PKSwitch
|
||||
{
|
||||
public SwitchId Id { get; }
|
||||
|
@@ -4,7 +4,34 @@ using Newtonsoft.Json;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
public override string ToString() => $"System #{Value}";
|
||||
}
|
||||
|
||||
public class PKSystem
|
||||
{
|
||||
// Additions here should be mirrored in SystemStore::Save
|
||||
|
@@ -1,26 +0,0 @@
|
||||
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);
|
||||
|
||||
public override string ToString() => $"Switch #{Value}";
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
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);
|
||||
|
||||
public override string ToString() => $"System #{Value}";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user