Various small fixes, setting guild config now works

This commit is contained in:
Ske
2020-06-13 19:15:50 +02:00
parent 2038f023a0
commit c875c8af9f
9 changed files with 56 additions and 70 deletions

View File

@@ -20,31 +20,4 @@ namespace PluralKit.Core
Interlocked.Decrement(ref _connectionCount);
}
}
public class PassthroughTypeHandler<T>: SqlMapper.TypeHandler<T>
{
public override void SetValue(IDbDataParameter parameter, T value)
{
parameter.Value = value;
}
public override T Parse(object value)
{
return (T) value;
}
}
public class UlongEncodeAsLongHandler: SqlMapper.TypeHandler<ulong>
{
public override ulong Parse(object value)
{
// Cast to long to unbox, then to ulong (???)
return (ulong) (long) value;
}
public override void SetValue(IDbDataParameter parameter, ulong value)
{
parameter.Value = (long) value;
}
}
}

View File

@@ -1,7 +1,5 @@
using System.IO;
using Dapper;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
@@ -9,8 +7,6 @@ using Newtonsoft.Json;
using NodaTime;
using NodaTime.Serialization.JsonNet;
using Npgsql;
namespace PluralKit.Core {
public static class InitUtils
{
@@ -19,32 +15,6 @@ namespace PluralKit.Core {
.AddJsonFile("pluralkit.conf", true)
.AddEnvironmentVariables()
.AddCommandLine(args);
public static void Init()
{
InitDatabase();
}
private static void InitDatabase()
{
// Dapper by default tries to pass ulongs to Npgsql, which rejects them since PostgreSQL technically
// doesn't support unsigned types on its own.
// Instead we add a custom mapper to encode them as signed integers instead, converting them back and forth.
SqlMapper.RemoveTypeMap(typeof(ulong));
SqlMapper.AddTypeHandler<ulong>(new UlongEncodeAsLongHandler());
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
// Also, use NodaTime. it's good.
NpgsqlConnection.GlobalTypeMapper.UseNodaTime();
// With the thing we add above, Npgsql already handles NodaTime integration
// This makes Dapper confused since it thinks it has to convert it anyway and doesn't understand the types
// So we add a custom type handler that literally just passes the type through to Npgsql
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<LocalDate>());
// Add global type mapper for ProxyTag compound type in Postgres
NpgsqlConnection.GlobalTypeMapper.MapComposite<ProxyTag>("proxy_tag");
}
public static JsonSerializerSettings BuildSerializerSettings() => new JsonSerializerSettings().BuildSerializerSettings();