Skip to content

Commit

Permalink
v9.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Abraham committed Dec 15, 2024
1 parent 59a313e commit 1d6c1db
Show file tree
Hide file tree
Showing 38 changed files with 1,262 additions and 636 deletions.
4 changes: 2 additions & 2 deletions src/TypeCache.GraphQL/Extensions/SchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static FieldType[] AddDatabaseSchemaQueries(this ISchema @this, IDataSour
var table = dataSource.GetDatabaseSchema(SchemaCollection.MetaDataCollections);
var rows = table.Rows.OfType<DataRow>();

return rows.Select(row => @this.AddDatabaseSchemaQuery(dataSource, row[SchemaColumn.collectionName].ToString().ToEnum<SchemaCollection>()!.Value)).ToArray();
return rows.Select(row => @this.AddDatabaseSchemaQuery(dataSource, row[SchemaColumn.collectionName].ToString().Enum<SchemaCollection>()!.Value)).ToArray();
}

/// <summary>
Expand Down Expand Up @@ -125,7 +125,7 @@ public static FieldType AddDatabaseSchemaQuery(this ISchema @this, IDataSource d
Resolver = new DatabaseSchemaFieldResolver()
});
fieldType.Metadata.Add(nameof(IDataSource), dataSource);
fieldType.Metadata.Add(nameof(SchemaCollection), table.TableName.ToEnum<SchemaCollection>());
fieldType.Metadata.Add(nameof(SchemaCollection), table.TableName.Enum<SchemaCollection>());

return fieldType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TypeCache.GraphQL/Resolvers/PropertyFieldResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public sealed class PropertyFieldResolver<T>(PropertyInfo propertyInfo) : FieldR
var pattern = context.GetArgument<string>("match");
if (pattern.IsNotBlank())
{
var match = text.ToRegex(RegexOptions.Compiled | RegexOptions.Singleline).Match(text);
var match = text.Regex(RegexOptions.Compiled | RegexOptions.Singleline).Match(text);
if (match.Success)
text = match.Value;
else
Expand Down
2 changes: 1 addition & 1 deletion src/TypeCache.GraphQL/TypeCache.GraphQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<RootNamespace>TypeCache.GraphQL</RootNamespace>
<PackageId>TypeCache.GraphQL</PackageId>
<Version>9.0.1</Version>
<Version>9.0.2</Version>
<Authors>Samuel Abraham &lt;sam987883@gmail.com&gt;</Authors>
<Company>Samuel Abraham &lt;sam987883@gmail.com&gt;</Company>
<Title>TypeCache GraphQL</Title>
Expand Down
1 change: 0 additions & 1 deletion src/TypeCache/Converters/BigIntegerJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2021 Samuel Abraham

using System.Buffers;
using System.Globalization;
using System.Numerics;
using System.Text.Json;
Expand Down
2 changes: 1 addition & 1 deletion src/TypeCache/Data/DataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ _ when factoryName.ContainsIgnoreCase("SqlClient") => SqlServer,
var metadata = connection.GetSchema(SchemaCollection.MetaDataCollections.Name());
this.SupportedMetadataCollections = metadata.Rows
.Cast<DataRow>()
.Select(row => row[SchemaColumn.collectionName]!.ToString()!.ToEnum<SchemaCollection>()!.Value)
.Select(row => row[SchemaColumn.collectionName]!.ToString()!.Enum<SchemaCollection>()!.Value)
.WhereNotNull()
.ToFrozenSet();

Expand Down
10 changes: 10 additions & 0 deletions src/TypeCache/Extensions/ActionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ public static async Task Retry(this Action @this, IEnumerable<TimeSpan> retryDel
await Task.FromException(lastError);
}
}

/// <summary>
/// Runs the action and returns how long it took to run.
/// </summary>
public static TimeSpan Timed(this Action @this)
{
var ticks = Stopwatch.GetTimestamp();
@this();
return Stopwatch.GetElapsedTime(ticks);
}
}
25 changes: 25 additions & 0 deletions src/TypeCache/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2021 Samuel Abraham

using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
using TypeCache.Utilities;

namespace TypeCache.Extensions;
Expand Down Expand Up @@ -270,6 +272,29 @@ public static ArraySegment<T> Segment<T>(this T[] @this)
public static ArraySegment<T> Segment<T>(this T[] @this, int offset, int count)
=> new(@this, offset, count);

/// <param name="message">Pass in a custom error message or omit to use a default message.</param>
/// <param name="logger">Pass a logger to log exception if thrown.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfEmpty<T>([NotNull] this T[] @this, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument = null)
where T : notnull
{
if (@this.Length is 0)
{
var exception = new ArgumentOutOfRangeException(
paramName: argument,
actualValue: @this,
message: message ?? Invariant($"{caller}: {nameof(ThrowIfEmpty)}"));

logger?.LogError(exception, exception.Message);

throw exception;
}
}

/// <inheritdoc cref="Convert.ToBase64String(ReadOnlySpan{byte}, Base64FormattingOptions)"/>
/// <remarks>
/// <c>=&gt; <see cref="Convert"/>.ToBase64String(@<paramref name="this"/>, <paramref name="options"/>);</c>
Expand Down
85 changes: 85 additions & 0 deletions src/TypeCache/Extensions/BooleanExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2021 Samuel Abraham

using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;

namespace TypeCache.Extensions;

Expand All @@ -23,4 +24,88 @@ public static bool Then(this bool @this, Action doIfTrue)

return @this;
}

/// <param name="message">Pass in a custom message or omitt to use a default message.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfFalse(this bool @this, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument = null)
{
if (!@this)
{
var exception = new ArgumentOutOfRangeException(
paramName: argument,
actualValue: @this.ToString(),
message: message ?? Invariant($"{caller}: {nameof(ThrowIfFalse)}"));

logger?.LogError(exception, exception.Message);

throw exception;
}
}

/// <param name="message">Pass in a custom message or omitt to use a default message.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfNotTrue(this bool? @this, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument = null)
{
if (@this is not true)
{
var exception = new ArgumentOutOfRangeException(
paramName: argument,
actualValue: @this.ToString(),
message: message ?? Invariant($"{caller}: {nameof(ThrowIfFalse)}"));

logger?.LogError(exception, exception.Message);

throw exception;
}
}

/// <param name="message">Pass in a custom message or omitt to use a default message.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfTrue(this bool @this, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument = null)
{
if (@this)
{
var exception = new ArgumentOutOfRangeException(
paramName: argument,
actualValue: @this.ToString(),
message: message ?? Invariant($"{caller}: {nameof(ThrowIfFalse)}"));

logger?.LogError(exception, exception.Message);

throw exception;
}
}

/// <param name="message">Pass in a custom message or omitt to use a default message.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfTrue(this bool? @this, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument = null)
{
if (@this is true)
{
var exception = new ArgumentOutOfRangeException(
paramName: argument,
actualValue: @this.ToString(),
message: message ?? Invariant($"{caller}: {nameof(ThrowIfFalse)}"));

logger?.LogError(exception, exception.Message);

throw exception;
}
}
}
83 changes: 62 additions & 21 deletions src/TypeCache/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Samuel Abraham

using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using TypeCache.Extensions;
using TypeCache.Utilities;
using static System.Reflection.BindingFlags;
Expand All @@ -15,6 +17,14 @@ public static Attribute[] Attributes<T>(this T @this)
? typeof(T).GetField(@this.Name(), Public | Static)!.GetCustomAttributes(false).Cast<Attribute>().ToArray()
: Array<Attribute>.Empty;

/// <inheritdoc cref="StringComparer.FromComparison(StringComparison)"/>
/// <remarks>
/// <c>=&gt; <see cref="StringComparer"/>.FromComparison(@<paramref name="this"/>);</c>
/// </remarks>
[MethodImpl(AggressiveInlining), DebuggerHidden]
public static StringComparer Comparer(this StringComparison @this)
=> StringComparer.FromComparison(@this);

[DebuggerHidden]
public static bool HasAnyFlag<T>(this T @this, T[] flags)
where T : struct, Enum
Expand Down Expand Up @@ -73,13 +83,50 @@ public static string Number<T>(this T @this)
where T : struct, Enum
=> @this.ToString("D");

/// <inheritdoc cref="StringComparer.FromComparison(StringComparison)"/>
/// <remarks>
/// <c>=&gt; <see cref="StringComparer"/>.FromComparison(@<paramref name="this"/>);</c>
/// </remarks>
[MethodImpl(AggressiveInlining), DebuggerHidden]
public static StringComparer ToStringComparer(this StringComparison @this)
=> StringComparer.FromComparison(@this);
/// <param name="message">Pass in a custom error message or omit to use a default message.</param>
/// <param name="logger">Pass a logger to log exception if thrown.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument1">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument2">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfEqual<T>(this T @this, T value, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument1 = null,
[CallerArgumentExpression("value")] string? argument2 = null)
where T : struct, Enum
{
if (Enum<T>.Equals(@this, value))
Throw(caller!, (argument1!, argument2!), (@this, value), message, logger);
}

/// <param name="message">Pass in a custom error message or omit to use a default message.</param>
/// <param name="logger">Pass a logger to log exception if thrown.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument1">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument2">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfNotEqual<T>(this T @this, T value, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument1 = null,
[CallerArgumentExpression("value")] string? argument2 = null)
where T : struct, Enum
{
if (!Enum<T>.Equals(@this, value))
Throw(caller!, (argument1!, argument2!), (@this, value), message, logger);
}

private static void Throw(string method, (string, string) arguments, (object?, object?) items, string? message, ILogger? logger,
[CallerMemberName] string? caller = null)
{
var exception = new ArgumentOutOfRangeException(
paramName: arguments.ToString(),
actualValue: items,
message: message ?? Invariant($"{method}: {caller}"));

logger?.LogError(exception, exception.Message);

throw exception;
}

public static bool IsConcurrent(this CollectionType @this) => @this switch
{
Expand Down Expand Up @@ -270,9 +317,8 @@ or CollectionType.KeyedCollection or CollectionType.ListDictionary or Collection
public static bool IsEnumUnderlyingType(this ScalarType @this)
=> @this switch
{
ScalarType.SByte or ScalarType.Byte
or ScalarType.Int16 or ScalarType.Int32 or ScalarType.Int64
or ScalarType.UInt16 or ScalarType.UInt32 or ScalarType.UInt64 => true,
ScalarType.SByte or ScalarType.Int16 or ScalarType.Int32 or ScalarType.Int64
or ScalarType.Byte or ScalarType.UInt16 or ScalarType.UInt32 or ScalarType.UInt64 => true,
_ => false
};

Expand All @@ -296,23 +342,17 @@ or CollectionType.ImmutableQueue
};

/// <summary>
/// Returns true for the current .Net primitives.<br/>
/// In addition, returns true for the following types:
/// <list type="bullet">
/// <item><c><see cref="ScalarType.Decimal"/></c></item>
/// <item><c><see cref="ScalarType.Int128"/></c></item>
/// <item><c><see cref="ScalarType.UInt128"/></c></item>
/// </list>
/// Returns true for the current .Net primitives.
/// </summary>
public static bool IsPrimitive(this ScalarType @this)
=> @this switch
{
ScalarType.Boolean
or ScalarType.SByte or ScalarType.Byte
or ScalarType.Int16 or ScalarType.Int32 or ScalarType.Int64 or ScalarType.Int128
or ScalarType.Int16 or ScalarType.Int32 or ScalarType.Int64
or ScalarType.IntPtr or ScalarType.UIntPtr
or ScalarType.UInt16 or ScalarType.UInt32 or ScalarType.UInt64 or ScalarType.UInt128
or ScalarType.Single or ScalarType.Double or ScalarType.Decimal
or ScalarType.UInt16 or ScalarType.UInt32 or ScalarType.UInt64
or ScalarType.Half or ScalarType.Single or ScalarType.Double
or ScalarType.Char => true,
_ => false
};
Expand All @@ -327,7 +367,8 @@ public static bool IsQueue(this CollectionType @this)
public static bool IsReadOnly(this CollectionType @this)
=> @this switch
{
CollectionType.ReadOnlyCollection or CollectionType.ReadOnlyDictionary or CollectionType.ReadOnlyObservableCollection => true,
CollectionType.ReadOnlyCollection or CollectionType.ReadOnlyDictionary or CollectionType.ReadOnlyObservableCollection
or CollectionType.ReadOnlyList or CollectionType.ReadOnlyObservableCollection or CollectionType.ReadOnlySet => true,
_ => false
};

Expand Down
25 changes: 25 additions & 0 deletions src/TypeCache/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2021 Samuel Abraham

using System.Collections;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using TypeCache.Utilities;

namespace TypeCache.Extensions;
Expand Down Expand Up @@ -115,6 +117,29 @@ public static void Deconstruct<T>(this IEnumerable<T> @this, out T? first, out T
public static T? SingleOrDefault<T>(this IEnumerable @this)
=> @this.OfType<T>().SingleOrDefault();

/// <param name="message">Pass in a custom error message or omit to use a default message.</param>
/// <param name="logger">Pass a logger to log exception if thrown.</param>
/// <param name="caller">Do not pass any value to this parameter as it will be injected automatically</param>
/// <param name="argument">Do not pass any value to this parameter as it will be injected automatically</param>
/// <exception cref="ArgumentOutOfRangeException"/>
public static void ThrowIfEmpty<T>([NotNull] this IEnumerable<T> @this, string? message = null, ILogger? logger = null,
[CallerMemberName] string? caller = null,
[CallerArgumentExpression("this")] string? argument = null)
where T : notnull
{
if (!@this.Any())
{
var exception = new ArgumentOutOfRangeException(
paramName: argument,
actualValue: @this,
message: message ?? Invariant($"{caller}: {nameof(ThrowIfEmpty)}"));

logger?.LogError(exception, exception.Message);

throw exception;
}
}

/// <inheritdoc cref="Dictionary{TKey, TValue}.Dictionary(IEnumerable{KeyValuePair{TKey, TValue}}, IEqualityComparer{TKey}?)"/>
/// <remarks>
/// <c>=&gt; <see langword="new"/> <see cref="Dictionary{TKey, TValue}"/>(@<paramref name="this"/>, <paramref name="comparer"/>);</c>
Expand Down
Loading

0 comments on commit 1d6c1db

Please sign in to comment.