generated from kasthack-labs/dotnet-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve solution structure, add an option to allow empty collections,…
… bump version to 1.0.6
- Loading branch information
Showing
8 changed files
with
166 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace kasthack.NotEmpty.Core | ||
{ | ||
public class AssertOptions | ||
{ | ||
internal static AssertOptions Default { get; } = new(); | ||
|
||
/* | ||
///// <summary> | ||
///// Maximum assert depth. Useful for preventing stack overflows for objects with generated properties / complex graphs. | ||
///// </summary> | ||
//public int? MaxDepth { get; set; } = 100; | ||
///// <summary> | ||
///// Allow zeros in number arrays. Useful when you have binary data as a byte array. | ||
///// </summary> | ||
//public bool AllowZerosInNumberArrays { get; set; } = false; | ||
*/ | ||
|
||
/// <summary> | ||
/// Allows empty strings but not nulls. | ||
/// </summary> | ||
public bool AllowEmptyStrings { get; set; } = false; | ||
|
||
/// <summary> | ||
/// Allows empty strings but not nulls. | ||
/// </summary> | ||
public bool AllowEmptyCollections { get; set; } = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace kasthack.NotEmpty.Core | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
// Creates NotEmptyInternal<T> wrapper: | ||
// (object value, AssertOptions options, string path) => this.NotEmptyInternal<ACTUAL_TYPE_OF_VALUE>((ACTUAL_TYPE_OF_VALUE)value, options, path) | ||
internal static class CachedEmptyDelegate | ||
{ | ||
private static readonly MethodInfo NotEmptyMethod = typeof(NotEmptyExtensionsBase) | ||
.GetMethod(nameof(NotEmptyExtensionsBase.NotEmptyInternal), BindingFlags.NonPublic | BindingFlags.Instance)! | ||
.GetGenericMethodDefinition(); | ||
|
||
private static readonly Dictionary<Type, Action<NotEmptyExtensionsBase, object?, AssertOptions, string?>> Delegates = new(); | ||
|
||
public static Action<NotEmptyExtensionsBase, object?, AssertOptions, string?> GetDelegate(Type type) | ||
{ | ||
if (!Delegates.TryGetValue(type, out var result)) | ||
{ | ||
lock (Delegates) | ||
{ | ||
if (!Delegates.TryGetValue(type, out result)) | ||
{ | ||
var thisParam = Expression.Parameter(typeof(NotEmptyExtensionsBase)); | ||
var valueParam = Expression.Parameter(typeof(object)); | ||
var optionsParam = Expression.Parameter(typeof(AssertOptions)); | ||
var pathParam = Expression.Parameter(typeof(string)); | ||
var parameters = new[] | ||
{ | ||
thisParam, | ||
valueParam, | ||
optionsParam, | ||
pathParam, | ||
}; | ||
result = (Action<NotEmptyExtensionsBase, object?, AssertOptions, string?>)Expression | ||
.Lambda( | ||
Expression.Call( | ||
thisParam, | ||
NotEmptyMethod.MakeGenericMethod(type), | ||
arguments: new Expression[] | ||
{ | ||
Expression.Convert( | ||
valueParam, | ||
type), | ||
optionsParam, | ||
pathParam, | ||
}), | ||
parameters) | ||
.Compile(); | ||
Delegates[type] = result; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace kasthack.NotEmpty.Core | ||
{ | ||
using System; | ||
using System.Reflection; | ||
|
||
// Returns all properties as an array of KV pairs | ||
internal static class CachedPropertyExtractor<T> | ||
{ | ||
private static readonly PropertyInfo[] Properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty); | ||
|
||
public static PathValue[] GetProperties(T? value) | ||
{ | ||
if (Properties.Length == 0) | ||
{ | ||
return Array.Empty<PathValue>(); | ||
} | ||
|
||
var props = new PathValue[Properties.Length]; | ||
for (int i = 0; i < props.Length; i++) | ||
{ | ||
props[i] = new PathValue(Properties[i].Name, Properties[i].GetValue(value)); | ||
} | ||
|
||
return props; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace kasthack.NotEmpty.Core | ||
{ | ||
internal readonly struct PathValue | ||
{ | ||
public PathValue(string path, object? value) | ||
{ | ||
this.Path = path; | ||
this.Value = value; | ||
} | ||
|
||
public readonly string Path { get; } | ||
|
||
public readonly object? Value { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters