Updated common abstractions
This commit is contained in:
parent
4fe4c4eff2
commit
b045015efb
23 changed files with 47 additions and 51 deletions
|
@ -1,4 +1,4 @@
|
||||||
using OneOf;
|
using OneOf;
|
||||||
using OneOf.Types;
|
using OneOf.Types;
|
||||||
|
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
@ -13,7 +13,7 @@ public static class BotCacheExtensions
|
||||||
|
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TypedKey<byte[]> GetImgKey(Uri uri)
|
private static TypedKey<byte[]> GetImgKey(Uri uri)
|
||||||
=> new($"image:{uri}");
|
=> new($"image:{uri}");
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ public interface IBotCache
|
||||||
/// <typeparam name="T">Type of the value</typeparam>
|
/// <typeparam name="T">Type of the value</typeparam>
|
||||||
/// <returns>Returns whether add was sucessful. Always true unless ovewrite = false</returns>
|
/// <returns>Returns whether add was sucessful. Always true unless ovewrite = false</returns>
|
||||||
ValueTask<bool> AddAsync<T>(TypedKey<T> key, T value, TimeSpan? expiry = null, bool overwrite = true);
|
ValueTask<bool> AddAsync<T>(TypedKey<T> key, T value, TimeSpan? expiry = null, bool overwrite = true);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get an element from the cache
|
/// Get an element from the cache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -23,7 +23,7 @@ public interface IBotCache
|
||||||
/// <typeparam name="T">Type of the value</typeparam>
|
/// <typeparam name="T">Type of the value</typeparam>
|
||||||
/// <returns>Either a value or <see cref="None"/></returns>
|
/// <returns>Either a value or <see cref="None"/></returns>
|
||||||
ValueTask<OneOf<T, None>> GetAsync<T>(TypedKey<T> key);
|
ValueTask<OneOf<T, None>> GetAsync<T>(TypedKey<T> key);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Remove a key from the cache
|
/// Remove a key from the cache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -26,12 +26,12 @@ public sealed class MemoryBotCache : IBotCache
|
||||||
item.AbsoluteExpirationRelativeToNow = expiry;
|
item.AbsoluteExpirationRelativeToNow = expiry;
|
||||||
return new(true);
|
return new(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
lock (_cacheLock)
|
lock (_cacheLock)
|
||||||
{
|
{
|
||||||
if (_cache.TryGetValue(key.Key, out var old) && old is not null)
|
if (_cache.TryGetValue(key.Key, out var old) && old is not null)
|
||||||
return new(false);
|
return new(false);
|
||||||
|
|
||||||
using var item = _cache.CreateEntry(key.Key);
|
using var item = _cache.CreateEntry(key.Key);
|
||||||
item.Value = value;
|
item.Value = value;
|
||||||
item.AbsoluteExpirationRelativeToNow = expiry;
|
item.AbsoluteExpirationRelativeToNow = expiry;
|
||||||
|
@ -63,7 +63,7 @@ public sealed class MemoryBotCache : IBotCache
|
||||||
{
|
{
|
||||||
lock (_cacheLock)
|
lock (_cacheLock)
|
||||||
{
|
{
|
||||||
var toReturn = _cache.TryGetValue(key.Key, out var old) && old is not null;
|
var toReturn = _cache.TryGetValue(key.Key, out var old ) && old is not null;
|
||||||
_cache.Remove(key.Key);
|
_cache.Remove(key.Key);
|
||||||
return new(toReturn);
|
return new(toReturn);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace System.Collections.Generic;
|
namespace System.Collections.Generic;
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ namespace System.Collections.Generic;
|
||||||
public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T> where T : notnull
|
public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T> where T : notnull
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<T, bool> _backingStore;
|
private readonly ConcurrentDictionary<T, bool> _backingStore;
|
||||||
|
|
||||||
public ConcurrentHashSet()
|
public ConcurrentHashSet()
|
||||||
=> _backingStore = new();
|
=> _backingStore = new();
|
||||||
|
|
||||||
|
@ -46,12 +46,8 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
||||||
public void CopyTo(T[] array, int arrayIndex)
|
public void CopyTo(T[] array, int arrayIndex)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(array);
|
ArgumentNullException.ThrowIfNull(array);
|
||||||
|
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
|
||||||
if (arrayIndex < 0)
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length);
|
||||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
|
||||||
|
|
||||||
if (arrayIndex >= array.Length)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
|
||||||
|
|
||||||
CopyToInternal(array, arrayIndex);
|
CopyToInternal(array, arrayIndex);
|
||||||
}
|
}
|
||||||
|
@ -63,7 +59,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
||||||
{
|
{
|
||||||
if (arrayIndex >= len)
|
if (arrayIndex >= len)
|
||||||
throw new IndexOutOfRangeException(nameof(arrayIndex));
|
throw new IndexOutOfRangeException(nameof(arrayIndex));
|
||||||
|
|
||||||
array[arrayIndex++] = k;
|
array[arrayIndex++] = k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
|
@ -16,7 +16,7 @@ public sealed class EllieRandom : Random
|
||||||
_rng.GetBytes(bytes);
|
_rng.GetBytes(bytes);
|
||||||
return Math.Abs(BitConverter.ToInt32(bytes, 0));
|
return Math.Abs(BitConverter.ToInt32(bytes, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates a random integer between 0 (inclusive) and
|
/// Generates a random integer between 0 (inclusive) and
|
||||||
/// a specified exclusive upper bound using a cryptographically strong random number generator.
|
/// a specified exclusive upper bound using a cryptographically strong random number generator.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public static class ArrayExtensions
|
||||||
public static TOut[] Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
|
public static TOut[] Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
|
||||||
{
|
{
|
||||||
var toReturn = new TOut[col.Count];
|
var toReturn = new TOut[col.Count];
|
||||||
|
|
||||||
var i = 0;
|
var i = 0;
|
||||||
foreach (var item in col)
|
foreach (var item in col)
|
||||||
toReturn[i++] = f(item);
|
toReturn[i++] = f(item);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ public static class EnumerableExtensions
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string>? func = null)
|
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string>? func = null)
|
||||||
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Randomize element order by performing the Fisher-Yates shuffle
|
/// Randomize element order by performing the Fisher-Yates shuffle
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
public static class Extensions
|
public static class Extensions
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,16 +7,16 @@ public static class PipeExtensions
|
||||||
{
|
{
|
||||||
public static TOut Pipe<TIn, TOut>(this TIn a, Func<TIn, TOut> fn)
|
public static TOut Pipe<TIn, TOut>(this TIn a, Func<TIn, TOut> fn)
|
||||||
=> fn(a);
|
=> fn(a);
|
||||||
|
|
||||||
public static TOut Pipe<TIn, TOut>(this TIn a, PipeFunc<TIn, TOut> fn)
|
public static TOut Pipe<TIn, TOut>(this TIn a, PipeFunc<TIn, TOut> fn)
|
||||||
=> fn(a);
|
=> fn(a);
|
||||||
|
|
||||||
public static TOut Pipe<TIn1, TIn2, TOut>(this (TIn1, TIn2) a, PipeFunc<TIn1, TIn2, TOut> fn)
|
public static TOut Pipe<TIn1, TIn2, TOut>(this (TIn1, TIn2) a, PipeFunc<TIn1, TIn2, TOut> fn)
|
||||||
=> fn(a.Item1, a.Item2);
|
=> fn(a.Item1, a.Item2);
|
||||||
|
|
||||||
public static (TIn, TExtra) With<TIn, TExtra>(this TIn a, TExtra b)
|
public static (TIn, TExtra) With<TIn, TExtra>(this TIn a, TExtra b)
|
||||||
=> (a, b);
|
=> (a, b);
|
||||||
|
|
||||||
public static async Task<TOut> Pipe<TIn, TOut>(this Task<TIn> a, Func<TIn, TOut> fn)
|
public static async Task<TOut> Pipe<TIn, TOut>(this Task<TIn> a, Func<TIn, TOut> fn)
|
||||||
=> fn(await a);
|
=> fn(await a);
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
using EllieBot.Common.Yml;
|
using EllieBot.Common.Yml;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
@ -82,14 +82,14 @@ public static class StringExtensions
|
||||||
// Step 3
|
// Step 3
|
||||||
for (var i = 1; i <= n; i++)
|
for (var i = 1; i <= n; i++)
|
||||||
//Step 4
|
//Step 4
|
||||||
for (var j = 1; j <= m; j++)
|
for (var j = 1; j <= m; j++)
|
||||||
{
|
{
|
||||||
// Step 5
|
// Step 5
|
||||||
var cost = t[j - 1] == s[i - 1] ? 0 : 1;
|
var cost = t[j - 1] == s[i - 1] ? 0 : 1;
|
||||||
|
|
||||||
// Step 6
|
// Step 6
|
||||||
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
|
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 7
|
// Step 7
|
||||||
return d[n, m];
|
return d[n, m];
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
public static class StandardConversions
|
public static class StandardConversions
|
||||||
{
|
{
|
||||||
|
|
|
@ -50,9 +50,9 @@ public class EventPubSub : IPubSub
|
||||||
{
|
{
|
||||||
// get subscriptions for this action
|
// get subscriptions for this action
|
||||||
if (_actions.TryGetValue(key.Key, out var actions))
|
if (_actions.TryGetValue(key.Key, out var actions))
|
||||||
// get subscriptions which have the same action hash code
|
// get subscriptions which have the same action hash code
|
||||||
// note: having this as a list allows for multiple subscriptions of
|
// note: having this as a list allows for multiple subscriptions of
|
||||||
// the same insance's/static method
|
// the same insance's/static method
|
||||||
{
|
{
|
||||||
if (actions.TryGetValue(action, out var sameActions))
|
if (actions.TryGetValue(action, out var sameActions))
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
public interface IPubSub
|
public interface IPubSub
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
public interface ISeria
|
public interface ISeria
|
||||||
{
|
{
|
||||||
|
|
|
@ -37,7 +37,7 @@ public sealed class QueueRunner
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var func = await _channel.Reader.ReadAsync(cancel);
|
var func = await _channel.Reader.ReadAsync(cancel);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await func();
|
await func();
|
||||||
|
@ -55,7 +55,7 @@ public sealed class QueueRunner
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValueTask EnqueueAsync(Func<Task> action)
|
public ValueTask EnqueueAsync(Func<Task> action)
|
||||||
=> _channel.Writer.WriteAsync(action);
|
=> _channel.Writer.WriteAsync(action);
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Ellie.Common;
|
namespace Ellie.Common;
|
||||||
|
|
||||||
public readonly struct TypedKey<TData>
|
public readonly struct TypedKey<TData>
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#nullable disable
|
#nullable disable
|
||||||
namespace EllieBot;
|
namespace EllieBot;
|
||||||
|
|
||||||
public interface IBotCredentials
|
public interface IBotCredentials
|
||||||
{
|
{
|
||||||
string Token { get; }
|
string Token { get; }
|
||||||
string GoogleApiKey { get; }
|
string EllieAiToken { get; }
|
||||||
ICollection<ulong> OwnerIds { get; set; }
|
ICollection<ulong> OwnerIds { get; set; }
|
||||||
|
string GoogleApiKey { get; }
|
||||||
bool UsePrivilegedIntents { get; }
|
bool UsePrivilegedIntents { get; }
|
||||||
string RapidApiKey { get; }
|
string RapidApiKey { get; }
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ public sealed record class CommandStrings
|
||||||
|
|
||||||
[YamlMember(Alias = "ex")]
|
[YamlMember(Alias = "ex")]
|
||||||
public string[] Examples { get; set; }
|
public string[] Examples { get; set; }
|
||||||
|
|
||||||
[YamlMember(Alias = "params")]
|
[YamlMember(Alias = "params")]
|
||||||
public Dictionary<string, CommandStringParam>[] Params { get; set; }
|
public Dictionary<string, CommandStringParam>[] Params { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ public sealed record class CommandStringParam
|
||||||
{
|
{
|
||||||
// [YamlMember(Alias = "type", ScalarStyle = ScalarStyle.DoubleQuoted)]
|
// [YamlMember(Alias = "type", ScalarStyle = ScalarStyle.DoubleQuoted)]
|
||||||
// public string Type { get; set; }
|
// public string Type { get; set; }
|
||||||
|
|
||||||
[YamlMember(Alias = "desc", ScalarStyle = ScalarStyle.DoubleQuoted)]
|
[YamlMember(Alias = "desc", ScalarStyle = ScalarStyle.DoubleQuoted)]
|
||||||
public string Desc { get; set; }
|
public string Desc { get; set; }
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
#nullable disable
|
#nullable disable
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
|
||||||
namespace EllieBot.Common;
|
namespace EllieBot.Common;
|
||||||
|
|
|
@ -8,7 +8,7 @@ public static class BotStringsExtensions
|
||||||
// this one is for pipe fun, see PipeExtensions.cs
|
// this one is for pipe fun, see PipeExtensions.cs
|
||||||
public static string GetText(this IBotStrings strings, in LocStr str, in ulong guildId)
|
public static string GetText(this IBotStrings strings, in LocStr str, in ulong guildId)
|
||||||
=> strings.GetText(str.Key, guildId, str.Params);
|
=> strings.GetText(str.Key, guildId, str.Params);
|
||||||
|
|
||||||
public static string GetText(this IBotStrings strings, in LocStr str, ulong? guildId = null)
|
public static string GetText(this IBotStrings strings, in LocStr str, ulong? guildId = null)
|
||||||
=> strings.GetText(str.Key, guildId, str.Params);
|
=> strings.GetText(str.Key, guildId, str.Params);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#nullable disable
|
#nullable disable
|
||||||
namespace EllieBot.Services;
|
namespace EllieBot.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
|
||||||
namespace EllieBot.Services;
|
namespace EllieBot.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in a new issue