generated from dailydevops/dotnet-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.
feat: Created library and added initial implementation (#1)
- Loading branch information
Showing
28 changed files
with
1,328 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ jobs: | |
name: Build & Tests | ||
uses: dailydevops/pipelines/.github/workflows/[email protected] | ||
with: | ||
disablePublish: true | ||
enableSonarQube: true | ||
dotnet-logging: ${{ inputs.dotnet-logging }} | ||
dotnet-version: ${{ vars.NE_DOTNET_TARGETFRAMEWORKS }} | ||
solution: ./FluentValue.sln | ||
|
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
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 |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# template-dotnet | ||
.NET template for repositories | ||
# NetEvolve.FluentValue | ||
|
||
This is a simple library that allows you to validate the given value against a set of constraints. It is designed to be used in a fluent way, so you can chain multiple constraints together. Most of the constraints have multipe execution paths, so that based on the value, delivers the most fitting result. | ||
|
||
## Installation | ||
|
||
```bash | ||
dotnet add package NetEvolve.FluentValue | ||
``` |
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,42 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Text; | ||
using NetEvolve.FluentValue; | ||
|
||
internal abstract class ConstraintBase : IConstraint | ||
{ | ||
[ThreadStatic] | ||
private static StringBuilder? _builder; | ||
|
||
private const int DefaultCapacity = 1024; | ||
|
||
public abstract bool IsSatisfiedBy(object? value); | ||
|
||
public abstract void SetDescription(StringBuilder builder); | ||
|
||
public override string ToString() | ||
{ | ||
var builder = _builder ?? new StringBuilder(capacity: DefaultCapacity); | ||
#pragma warning disable S2696 // Instance members should not write to "static" fields | ||
_builder = null; | ||
#pragma warning restore S2696 // Instance members should not write to "static" fields | ||
try | ||
{ | ||
_ = builder.Append('"').Append("{Value}"); | ||
|
||
SetDescription(builder); | ||
|
||
return builder.Append('.').Append('"').ToString(); | ||
} | ||
finally | ||
{ | ||
_ = builder.Clear(); | ||
if (builder.Capacity > DefaultCapacity) | ||
{ | ||
builder.Capacity = DefaultCapacity; | ||
} | ||
_builder = builder; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/NetEvolve.FluentValue/Constraints/ContainsConstraint.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,43 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
internal sealed class ContainsConstraint : ConstraintBase | ||
{ | ||
private readonly object? _compareValue; | ||
private readonly StringComparison? _comparison; | ||
|
||
public ContainsConstraint(char compareValue, StringComparison comparison) | ||
{ | ||
_compareValue = compareValue; | ||
_comparison = comparison; | ||
} | ||
|
||
public ContainsConstraint(string compareValue, StringComparison comparison) | ||
{ | ||
_compareValue = compareValue; | ||
_comparison = comparison; | ||
} | ||
|
||
public ContainsConstraint(object? compareValue) => _compareValue = compareValue; | ||
|
||
public override bool IsSatisfiedBy(object? value) => | ||
value switch | ||
{ | ||
null => false, | ||
string stringValue when _compareValue is string compareValue | ||
=> stringValue.Contains(compareValue, _comparison ?? default), | ||
string stringValue when _compareValue is char compareValue | ||
=> stringValue.Contains(compareValue, _comparison ?? default), | ||
IDictionary dictionary => dictionary.Contains(_compareValue!), | ||
IList list => list.Contains(_compareValue!), | ||
IEnumerable enumerable => enumerable.Cast<object?>().Contains(_compareValue), | ||
_ => throw new NotSupportedException($"Invalid type `{value!.GetType().FullName}`.") | ||
}; | ||
|
||
public override void SetDescription(StringBuilder builder) => | ||
builder.Append(" contains `").Append(_compareValue).Append('`'); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/NetEvolve.FluentValue/Constraints/DefaultConstraint.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,26 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Text; | ||
|
||
internal sealed class DefaultConstraint : ConstraintBase | ||
{ | ||
public override bool IsSatisfiedBy(object? value) => | ||
value?.GetType() switch | ||
{ | ||
{ IsValueType: true } valueType => GetDefault(valueType).Equals(value), | ||
_ => false | ||
}; | ||
|
||
public override void SetDescription(StringBuilder builder) => builder.Append(" is <default>"); | ||
|
||
private static object GetDefault(Type value) | ||
{ | ||
var underlying = Nullable.GetUnderlyingType(value); | ||
if (underlying is not null) | ||
{ | ||
return Activator.CreateInstance(underlying)!; | ||
} | ||
return Activator.CreateInstance(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Text; | ||
|
||
internal sealed class EmptyConstraint : ConstraintBase | ||
{ | ||
public override bool IsSatisfiedBy(object? value) => | ||
value switch | ||
{ | ||
string stringValue => stringValue.Length == 0, | ||
Guid guidValue => guidValue == Guid.Empty, | ||
ICollection collection => collection.Count == 0, | ||
IEnumerable enumerable => !enumerable.GetEnumerator().MoveNext(), | ||
_ => false | ||
}; | ||
|
||
public override void SetDescription(StringBuilder builder) => builder.Append(" is <empty>"); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/NetEvolve.FluentValue/Constraints/EndsWithConstraint.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,30 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Text; | ||
|
||
internal sealed class EndsWithConstraint : ConstraintBase | ||
{ | ||
private readonly object? _compareValue; | ||
private readonly StringComparison? _comparison; | ||
|
||
public EndsWithConstraint(char compareValue) => _compareValue = compareValue; | ||
|
||
public EndsWithConstraint(string compareValue, StringComparison comparison) | ||
{ | ||
_compareValue = compareValue; | ||
_comparison = comparison; | ||
} | ||
|
||
public override bool IsSatisfiedBy(object? value) => | ||
value switch | ||
{ | ||
string stringValue when _compareValue is string compare | ||
=> stringValue.EndsWith(compare, _comparison ?? default), | ||
string stringValue when _compareValue is char compare => stringValue.EndsWith(compare), | ||
_ => false | ||
}; | ||
|
||
public override void SetDescription(StringBuilder builder) => | ||
builder.Append(" ends with `").Append(_compareValue).Append('`'); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/NetEvolve.FluentValue/Constraints/EqualToConstraint.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,31 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Text; | ||
|
||
internal sealed class EqualToConstraint : ConstraintBase | ||
{ | ||
private readonly object? _compareValue; | ||
private readonly StringComparison? _comparison; | ||
|
||
public EqualToConstraint(object? compareValue) => _compareValue = compareValue; | ||
|
||
public EqualToConstraint(string compareValue, StringComparison comparison) | ||
{ | ||
_compareValue = compareValue; | ||
_comparison = comparison; | ||
} | ||
|
||
public override bool IsSatisfiedBy(object? value) => | ||
value switch | ||
{ | ||
string stringValue when _compareValue is string compareValue | ||
=> stringValue.Equals(compareValue, _comparison ?? default), | ||
string stringValue when _compareValue is IConvertible convertible | ||
=> stringValue.Equals(convertible.ToString(), _comparison ?? default), | ||
_ => false | ||
}; | ||
|
||
public override void SetDescription(StringBuilder builder) => | ||
builder.Append(" is equal to `").Append(_compareValue).Append('`'); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/NetEvolve.FluentValue/Constraints/MatchesConstraint.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,28 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
internal sealed class MatchesConstraint : ConstraintBase | ||
{ | ||
private readonly string _pattern; | ||
private readonly Regex _regex; | ||
|
||
public MatchesConstraint(string pattern, RegexOptions? options) | ||
{ | ||
_pattern = pattern; | ||
_regex = new Regex(pattern, options ?? default); | ||
Check warning on line 15 in src/NetEvolve.FluentValue/Constraints/MatchesConstraint.cs GitHub Actions / Build & Tests / Tests / Testing .NET solution
Check warning on line 15 in src/NetEvolve.FluentValue/Constraints/MatchesConstraint.cs GitHub Actions / Build & Tests / Tests / Testing .NET solution
Check warning on line 15 in src/NetEvolve.FluentValue/Constraints/MatchesConstraint.cs GitHub Actions / Build & Tests / Tests / Testing .NET solution
|
||
} | ||
|
||
public override bool IsSatisfiedBy(object? value) => | ||
value switch | ||
{ | ||
string stringValue => _regex.IsMatch(stringValue), | ||
IConvertible convertible => _regex.IsMatch(convertible.ToString()!), | ||
_ => false | ||
}; | ||
|
||
public override void SetDescription(StringBuilder builder) => | ||
builder.Append(" matches `").Append(_pattern).Append('`'); | ||
} |
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,10 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System.Text; | ||
|
||
internal sealed class NullConstraint : ConstraintBase | ||
{ | ||
public override bool IsSatisfiedBy(object? value) => value is null; | ||
|
||
public override void SetDescription(StringBuilder builder) => builder.Append(" is <null>"); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/NetEvolve.FluentValue/Constraints/ParenthesisConstraint.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,31 @@ | ||
namespace NetEvolve.FluentValue.Constraints; | ||
|
||
using System.Text; | ||
using NetEvolve.Arguments; | ||
using NetEvolve.FluentValue; | ||
|
||
internal sealed class ParenthesisConstraint : ConstraintBase | ||
{ | ||
private readonly ConstraintBase _constraint; | ||
|
||
public ParenthesisConstraint(IConstraint constraint) | ||
{ | ||
Argument.ThrowIfNull(constraint); | ||
|
||
_constraint = (ConstraintBase)constraint; | ||
} | ||
|
||
public override bool IsSatisfiedBy(object? value) | ||
{ | ||
Argument.ThrowIfNull(_constraint); | ||
|
||
return _constraint.IsSatisfiedBy(value); | ||
} | ||
|
||
public override void SetDescription(StringBuilder builder) | ||
{ | ||
_ = builder.Append(" ("); | ||
_constraint.SetDescription(builder); | ||
_ = builder.Replace("( ", "(").Append(')'); | ||
} | ||
} |
Oops, something went wrong.