Skip to content

Commit

Permalink
Upgraded package to .net 6 With minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bargross committed Jul 7, 2023
1 parent 6fc6f51 commit db2c6d3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 74 deletions.
110 changes: 62 additions & 48 deletions Either/Either/Either.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,54 @@ public class Either<TLeft, TRight> : IEither<TLeft, TRight>

private Type _currentType;
private bool _isLeft;
private bool _disposed = false;
private bool _disposed;

public bool IsValid { get; private set; }
public bool IsPresent { get; private set; }

public bool IsLeftValid
{
get
{
return _ruleValidator.ValidateRuleFor(_root.Left);
}
}

public bool IsRightValid
{
get
{
return _ruleValidator.ValidateRuleFor(_root.Right);
}
}

public Either() => _ruleValidator = new RuleValidator<TLeft, TRight>();

private Either(TLeft left, RuleValidator<TLeft, TRight> validator)
{
_root = left;

AssignScopeValues(
_ruleValidator == null ? new RuleValidator<TLeft, TRight>() : validator,
typeof(TLeft),
true,
true,
IsLeftValid()
);

SetValidator(validator);

AssignScopeValues(typeof(TLeft),true, true, IsLeftValid);
}

private Either(TRight right, RuleValidator<TLeft, TRight> validator)
{
_root = right;

SetValidator(validator);

AssignScopeValues(
_ruleValidator == null ? new RuleValidator<TLeft, TRight>() : validator,
typeof(TRight),
false,
true,
IsRightValid()
);
AssignScopeValues(typeof(TRight),false,true, IsRightValid);
}

~Either() => Dispose(false);

private void SetValidator(RuleValidator<TLeft, TRight> validator = null)
{
_ruleValidator = _ruleValidator == null ? new RuleValidator<TLeft, TRight>() : validator;
}

public void ReplaceRule(string ruleName, Func<TLeft, bool> replacement)
{
if(string.IsNullOrWhiteSpace(ruleName))
Expand All @@ -68,9 +81,6 @@ public void ReplaceRule(string ruleName, Func<TRight, bool> replacement)
_ruleValidator.Replace(ruleName, replacement);
}

public bool IsLeftValid() => _ruleValidator.ValidateRuleFor(_root.Left);
public bool IsRightValid() => _ruleValidator.ValidateRuleFor(_root.Right);

public T GetValue<T>()
{
var type = typeof(T);
Expand All @@ -93,13 +103,13 @@ public T GetValue<T>()
throw new InvalidCastException($"Either {typeof(TLeft)} nor {typeof(TRight)} match type: {typeof(T)}");
}

public void ResetRulesForLeft() => _ruleValidator.ResetRulesForLeft();
public void ResetRulesForRight() => _ruleValidator.ResetRulesForRight();
public void ResetRulesForLeftValue() => _ruleValidator.ResetRulesForLeftValue();
public void ResetRulesForRightValue() => _ruleValidator.ResetRulesForRightValue();

public void ResetRules()
{
ResetRulesForLeft();
ResetRulesForRight();
ResetRulesForLeftValue();
ResetRulesForRightValue();
}

public bool GetValidationResultForRule(string ruleName) => _ruleValidator.GetRuleValidationResult(ruleName);
Expand All @@ -113,13 +123,39 @@ public void SetValidatorOptions(Action<IRuleValidator<TLeft, TRight>> setOptions

setOptions.Invoke(_ruleValidator);

IsValid = IsPresent ?
_isLeft ? IsLeftValid() : IsRightValid()
: false;
if (IsPresent)
{
IsValid = _isLeft ? IsLeftValid : IsRightValid;
}
else
{
IsValid = false;
}
}

public bool ContainsRule(string ruleName) => _ruleValidator.ContainsRule(ruleName);

public static Either<TLeft, TRight> Of(TLeft value) => new Either<TLeft, TRight>(value, _ruleValidator);
public static Either<TLeft, TRight> Of(TRight value) => new Either<TLeft, TRight>(value, _ruleValidator);

// private methods

private void AssignScopeValues(Type type, bool isLeft, bool isPresent, bool isValid)
{
_currentType = type;
_isLeft = isLeft;
IsPresent = isPresent;
IsValid = isValid;
}

// Assignment & Cast Operators

public static implicit operator Either<TLeft, TRight>(TRight right) => new Either<TLeft, TRight>(right, _ruleValidator);
public static implicit operator Either<TLeft, TRight>(TLeft left) => new Either<TLeft, TRight>(left, _ruleValidator);

public static explicit operator TLeft(Either<TLeft, TRight> either) => either.GetValue<TLeft>();
public static explicit operator TRight(Either<TLeft, TRight> either) => either.GetValue<TRight>();

// IDisposable implementation

public void Dispose()
Expand All @@ -140,28 +176,6 @@ protected virtual void Dispose(bool disposing)
_disposed = true;
}
}

public static Either<TLeft, TRight> Of(TLeft value) => new Either<TLeft, TRight>(value, _ruleValidator);
public static Either<TLeft, TRight> Of(TRight value) => new Either<TLeft, TRight>(value, _ruleValidator);

// private methods

private void AssignScopeValues(RuleValidator<TLeft, TRight> validator, Type type, bool isLeft, bool isPresent, bool isValid)
{
_ruleValidator = validator;
_currentType = type;
_isLeft = isLeft;
IsPresent = isPresent;
IsValid = isValid;
}

// Assignment & Cast Operators

public static implicit operator Either<TLeft, TRight>(TRight right) => new Either<TLeft, TRight>(right, _ruleValidator);
public static implicit operator Either<TLeft, TRight>(TLeft left) => new Either<TLeft, TRight>(left, _ruleValidator);

public static explicit operator TLeft(Either<TLeft, TRight> either) => either.GetValue<TLeft>();
public static explicit operator TRight(Either<TLeft, TRight> either) => either.GetValue<TRight>();
}

}
4 changes: 2 additions & 2 deletions Either/Either/Either.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>

<PropertyGroup>
<PackageId>ControlFlow.Type.Either</PackageId>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Leo M.</Authors>
</PropertyGroup>

Expand Down
12 changes: 6 additions & 6 deletions Either/Either/IEither.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace Either
{
public interface IEither<TLeft, TRight> : IDisposable
public interface IEither<TLeft, TRight>: IDisposable
{
public bool IsValid { get; }

bool IsLeftValid { get; }
bool IsRightValid { get; }

T GetValue<T>();
bool IsLeftValid();
bool IsRightValid();
void ResetRulesForLeft();
void ResetRulesForRight();
void ResetRulesForLeftValue();
void ResetRulesForRightValue();
void ResetRules();
bool GetValidationResultForRule(string ruleName);
void SetValidatorOptions(Action<IRuleValidator<TLeft, TRight>> validator);
Expand Down
16 changes: 8 additions & 8 deletions Either/Either/Root/RootEither.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Either.Root
{
internal class RootEither<TLeft, TRight> : IRootEither<TLeft, TRight>, IDisposable
{
public bool IsLeftPresent { get; }
public bool IsRightPresent { get; }
public bool LeftPresent { get; }
public bool RightPresent { get; }

private TLeft _left;
private TRight _right;
Expand All @@ -15,7 +15,7 @@ public TLeft Left
{
get
{
if (IsLeftPresent && !IsRightPresent)
if (LeftPresent && !RightPresent)
{
return _left;
}
Expand All @@ -28,7 +28,7 @@ public TRight Right
{
get
{
if (IsRightPresent && !IsLeftPresent)
if (RightPresent && !LeftPresent)
{
return _right;
}
Expand All @@ -45,17 +45,17 @@ public RootEither(TLeft left)
_left = left;
_right = default;

IsLeftPresent = true;
IsRightPresent = false;
LeftPresent = true;
RightPresent = false;
}

public RootEither(TRight right)
{
_right = right;
_left = default;

IsLeftPresent = false;
IsRightPresent = true;
LeftPresent = false;
RightPresent = true;
}

~RootEither() => Dispose(false);
Expand Down
4 changes: 2 additions & 2 deletions Either/Either/Rule/IRuleValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public interface IRuleValidator<TLeft, TRight> : IDisposable

bool ValidateRuleFor(TLeft left);
bool ValidateRuleFor(TRight right);
void ResetRulesForLeft();
void ResetRulesForRight();
void ResetRulesForLeftValue();
void ResetRulesForRightValue();
bool GetRuleValidationResult(string ruleName);
}
}
16 changes: 9 additions & 7 deletions Either/Either/Rule/RuleValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class RuleValidator<TLeft, TRight> : IRuleValidator<TLeft, TRight>
public int RuleCount { get; private set; }

public RuleValidator() => Init();

// dispose destructor
~RuleValidator() => Dispose(false);

public IRuleValidator<TLeft, TRight> AddRule(string ruleName, Func<TLeft, bool> rule)
Expand Down Expand Up @@ -47,21 +49,21 @@ public class RuleValidator<TLeft, TRight> : IRuleValidator<TLeft, TRight>
public bool ValidateRuleFor(TLeft value) => ValidateRuleFor(value, _rulesForLeft);
public bool ValidateRuleFor(TRight value) => ValidateRuleFor(value, _rulesForRight);

public void ResetRulesForLeft() => _rulesForLeft.Clear();
public void ResetRulesForRight() => _rulesForRight.Clear();
public void ResetRulesForLeftValue() => _rulesForLeft.Clear();
public void ResetRulesForRightValue() => _rulesForRight.Clear();

public bool ContainsRule(string ruleName) => _rulesForLeft.ContainsKey(ruleName) || _rulesForRight.ContainsKey(ruleName);

public bool GetRuleValidationResult(string ruleName)
{
if(_rulesForLeft.ContainsKey(ruleName))
if (_rulesForLeft.TryGetValue(ruleName, out var leftRuleDetails))
{
return _rulesForLeft[ruleName].Item2;
return leftRuleDetails.Item2;
}

if(_rulesForRight.ContainsKey(ruleName))
if (_rulesForRight.TryGetValue(ruleName, out var rightRuleDetails))
{
return _rulesForRight[ruleName].Item2;
return rightRuleDetails.Item2;
}

throw new KeyNotFoundException("Rule not found");
Expand All @@ -75,7 +77,7 @@ private void Init()
{
_rulesForLeft = new Dictionary<string, (Func<TLeft, bool>, bool)>(_capacity);
_rulesForRight = new Dictionary<string, (Func<TRight, bool>, bool)>(_capacity);
FailedValidationMessages = new List<string>(_capacity) as IList<string>;
FailedValidationMessages = new List<string>(_capacity);
TerminateOnFail = false;

_initialized = true;
Expand Down
2 changes: 1 addition & 1 deletion Either/EitherTests/EitherTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit db2c6d3

Please sign in to comment.