mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-21 07:56:09 +03:00
Add NamespacedId and converters
This commit is contained in:
parent
2e5670a048
commit
40a44145fe
25
JetHerald/DapperMappers.cs
Normal file
25
JetHerald/DapperMappers.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System.Data;
|
||||||
|
|
||||||
|
using Dapper;
|
||||||
|
|
||||||
|
namespace JetHerald
|
||||||
|
{
|
||||||
|
public static class DapperConverters
|
||||||
|
{
|
||||||
|
static bool registered = false;
|
||||||
|
public static void Register()
|
||||||
|
{
|
||||||
|
if (registered)
|
||||||
|
return;
|
||||||
|
registered = true;
|
||||||
|
|
||||||
|
SqlMapper.AddTypeHandler(new NamespacedIdHandler());
|
||||||
|
}
|
||||||
|
|
||||||
|
class NamespacedIdHandler : SqlMapper.TypeHandler<NamespacedId>
|
||||||
|
{
|
||||||
|
public override void SetValue(IDbDataParameter parameter, NamespacedId value) => parameter.Value = value.ToString();
|
||||||
|
public override NamespacedId Parse(object value) => new NamespacedId((string)value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
47
JetHerald/NamespacedId.cs
Normal file
47
JetHerald/NamespacedId.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace JetHerald
|
||||||
|
{
|
||||||
|
public struct NamespacedId
|
||||||
|
{
|
||||||
|
public string Namespace { get; init; }
|
||||||
|
public string Id { get; init; }
|
||||||
|
|
||||||
|
public NamespacedId(string str)
|
||||||
|
{
|
||||||
|
var ind = str.IndexOf("://");
|
||||||
|
if (ind < 0) throw new ArgumentException("Could not parse namespaced id");
|
||||||
|
Namespace = str[..ind].ToLowerInvariant();
|
||||||
|
Id = str[(ind + 3)..];
|
||||||
|
}
|
||||||
|
|
||||||
|
public NamespacedId(string ns, string id)
|
||||||
|
{
|
||||||
|
Namespace = ns;
|
||||||
|
Id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NamespacedId Telegram(long id)
|
||||||
|
=> new NamespacedId("telegram", $"{id}");
|
||||||
|
|
||||||
|
public static NamespacedId Discord(ulong id)
|
||||||
|
=> new NamespacedId("discord", $"{id}");
|
||||||
|
|
||||||
|
public override string ToString() => $"{Namespace}://{Id}";
|
||||||
|
|
||||||
|
public override int GetHashCode() => HashCode.Combine(Namespace, Id);
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
=> obj is NamespacedId nsid && this == nsid;
|
||||||
|
|
||||||
|
public static bool operator == (NamespacedId a, NamespacedId b)
|
||||||
|
=> a.Namespace == b.Namespace && a.Id == b.Id;
|
||||||
|
|
||||||
|
public static bool operator !=(NamespacedId a, NamespacedId b)
|
||||||
|
=> !(a == b);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -15,6 +15,8 @@ namespace JetHerald
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
DapperConverters.Register();
|
||||||
|
|
||||||
var logger = NLog.Web.NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
|
var logger = NLog.Web.NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user