-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into unique-attribute
- Loading branch information
Showing
16 changed files
with
254 additions
and
23 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
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,35 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using Qowaiv.Diagnostics.Contracts; | ||
using Qowaiv.Globalization; | ||
using Qowaiv.Validation.DataAnnotations; | ||
|
||
namespace Benchmarks; | ||
|
||
[Inheritable] | ||
public class AllowedValues | ||
{ | ||
private readonly AllowedValuesAttribute NonGeneric = new("NL", "BE", "LU", "DE", "FR"); | ||
private readonly AllowedAttribute<Country> WithGenerics = new("NL", "BE", "LU", "DE", "FR"); | ||
|
||
[Benchmark(Baseline = true)] | ||
public bool non_generic() | ||
{ | ||
var result = false; | ||
foreach(var country in Country.All) | ||
{ | ||
result |= NonGeneric.IsValid(country); | ||
} | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool generic() | ||
{ | ||
var result = false; | ||
foreach (var country in Country.All) | ||
{ | ||
result |= WithGenerics.IsValid(country); | ||
} | ||
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Qowaiv.Validation.DataAnnotations\Qowaiv.Validation.DataAnnotations.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="nuget.config"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
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,11 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace Benchmarks; | ||
|
||
internal static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BenchmarkRunner.Run<AllowedValues>(); | ||
} | ||
} |
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,8 @@ | ||
# Qowaiv validation benchmarks | ||
|
||
## Allowed Values attribute | ||
|
||
| Method | Mean | Ratio | | ||
|----------------- |-----------:|------:| | ||
| AllowedValues | 321.043 us | 1.000 | | ||
| Allowed<T> | 2.775 us | 0.009 | |
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,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" /> | ||
</packageSources> | ||
</configuration> |
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
19 changes: 19 additions & 0 deletions
19
src/Qowaiv.Validation.DataAnnotations/Attributes/AllowedAttribute.cs
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,19 @@ | ||
namespace Qowaiv.Validation.DataAnnotations; | ||
|
||
/// <summary>Validates if the decorated item has a value that is specified in the allowed values.</summary> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] | ||
[CLSCompliant(false)] | ||
public sealed class AllowedAttribute<TValue> : SetOfAttribute<TValue> | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="AllowedAttribute{TValue}"/> class.</summary> | ||
/// <param name="values"> | ||
/// String representations of the allowed values. | ||
/// </param> | ||
public AllowedAttribute(params object[] values) | ||
: base(values) => Do.Nothing(); | ||
|
||
/// <summary>Return true the value of <see cref="SetOfAttribute{TValue}.IsValid(object)"/> | ||
/// equals one of the values of the <see cref="SetOfAttribute{TValue}" />. | ||
/// </summary> | ||
protected override bool OnEqual => true; | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/Qowaiv.Validation.DataAnnotations/Attributes/ForbiddenAttribute.cs
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,19 @@ | ||
namespace Qowaiv.Validation.DataAnnotations; | ||
|
||
/// <summary>Validates if the decorated item has a value that is specified in the forbidden values.</summary> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] | ||
[CLSCompliant(false)] | ||
public sealed class ForbiddenAttribute<TValue> : SetOfAttribute<TValue> | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="ForbiddenAttribute{TValue}"/> class.</summary> | ||
/// <param name="values"> | ||
/// String representations of the forbidden values. | ||
/// </param> | ||
public ForbiddenAttribute(params object[] values) | ||
: base(values) => Do.Nothing(); | ||
|
||
/// <summary>Return false if the value of <see cref="SetOfValuesAttribute.IsValid(object)"/> | ||
/// equals one of the values of the <see cref="ForbiddenAttribute{TValue}"/>. | ||
/// </summary> | ||
protected override bool OnEqual => 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
35 changes: 35 additions & 0 deletions
35
src/Qowaiv.Validation.DataAnnotations/Attributes/SetOfAttribute.cs
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,35 @@ | ||
namespace Qowaiv.Validation.DataAnnotations; | ||
|
||
/// <summary>Base <see cref="ValidationAttribute"/> for allowing or forbidding a set of values.</summary> | ||
/// <typeparam name="TValue"> | ||
/// The type of the value. | ||
/// </typeparam> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] | ||
[CLSCompliant(false)] | ||
public abstract class SetOfAttribute<TValue> : ValidationAttribute | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="SetOfAttribute{TValue}"/> class.</summary> | ||
/// <param name="values"> | ||
/// String representations of the values. | ||
/// </param> | ||
protected SetOfAttribute(params object[] values) | ||
: base(() => QowaivValidationMessages.AllowedValuesAttribute_ValidationError) | ||
{ | ||
var converter = TypeDescriptor.GetConverter(typeof(TValue)); | ||
Values = new HashSet<TValue>(values.Select(converter.ConvertFrom).OfType<TValue>()); | ||
} | ||
|
||
/// <summary>The result to return when the value of <see cref="IsValid(object)"/> | ||
/// equals one of the values of the <see cref="SetOfValuesAttribute"/>. | ||
/// </summary> | ||
protected abstract bool OnEqual { get; } | ||
|
||
/// <summary>Gets the values.</summary> | ||
public IReadOnlyCollection<TValue> Values { get; } | ||
|
||
/// <summary>Returns true if the value is allowed.</summary> | ||
[Pure] | ||
public sealed override bool IsValid(object? value) | ||
=> value is null | ||
|| OnEqual == Values.Contains((TValue)value); | ||
} |
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
Oops, something went wrong.