Skip to content

Commit

Permalink
Update Optional mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
alexinea committed Dec 5, 2022
1 parent 7718372 commit 54bd2e0
Show file tree
Hide file tree
Showing 28 changed files with 848 additions and 1,831 deletions.
10 changes: 5 additions & 5 deletions src/Cosmos.Extensions.Optionals/Cosmos/BooleanUtilities.Bv1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void Ifttt<T>(this BooleanVal<T> condition, Action @this, Action t
/// <param name="this"></param>
/// <param name="that"></param>
/// <returns></returns>
public static TValue Ifttt<T, TValue>(this BooleanVal<T> condition, Func<T, TValue> @this, Func<T, TValue> @that)
public static TValue Ifttt<T, TValue>(this BooleanVal<T> condition, Func<T, TValue> @this, Func<T, TValue> that)
{
if (condition.Value)
{
Expand Down Expand Up @@ -242,7 +242,7 @@ public static BooleanVal<T> And<T>(this bool current, Func<bool> next, T context
public static BooleanVal<T> And<T>(this bool current, Func<(bool, T)> next)
{
if (!current) return false;
return next?.Invoke() ?? false.ToBooleanVal<T>();
return next?.Invoke() ?? false.ToBooleanVal<T>()!;
}

/// <summary>
Expand Down Expand Up @@ -285,7 +285,7 @@ public static BooleanVal<T> And<T>(this bool current, Func<T, BooleanVal<T>> nex
public static BooleanVal<T> And<T>(this BooleanVal<T> current, Func<(bool, T)> next)
{
if (!current.Value) return current; // means false
return next?.Invoke() ?? current;
return next?.Invoke() ?? current!;
}

/// <summary>
Expand Down Expand Up @@ -357,7 +357,7 @@ public static BooleanVal<T> Or<T>(this bool current, Func<bool> next, T context)
public static BooleanVal<T> Or<T>(this bool current, Func<(bool, T)> next)
{
if (current) return true;
return next?.Invoke() ?? false.ToBooleanVal<T>();
return next?.Invoke() ?? false.ToBooleanVal<T>()!;
}

/// <summary>
Expand Down Expand Up @@ -400,7 +400,7 @@ public static BooleanVal<T> Or<T>(this bool current, Func<T, BooleanVal<T>> next
public static BooleanVal<T> Or<T>(this BooleanVal<T> current, Func<(bool, T)> next)
{
if (current.Value) return current; // means true
return next?.Invoke() ?? false.ToBooleanVal(current.Object);
return next?.Invoke() ?? false.ToBooleanVal(current.Object)!;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void Ifttt(this BooleanVal2 condition, Action @this, Action that)
/// <param name="this"></param>
/// <param name="that"></param>
/// <returns></returns>
public static TValue Ifttt<TValue>(this BooleanVal2 condition, Func<TValue> @this, Func<TValue> @that)
public static TValue Ifttt<TValue>(this BooleanVal2 condition, Func<TValue> @this, Func<TValue> that)
{
if (condition is null)
return default;
Expand Down
2 changes: 1 addition & 1 deletion src/Cosmos.Extensions.Optionals/Cosmos/BooleanUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void Ifttt(this bool condition, Action @this, Action that)
/// <param name="that"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TValue Ifttt<TValue>(this bool condition, Func<TValue> @this, Func<TValue> @that)
public static TValue Ifttt<TValue>(this bool condition, Func<TValue> @this, Func<TValue> that)
{
if (condition)
{
Expand Down
10 changes: 7 additions & 3 deletions src/Cosmos.Extensions.Optionals/Cosmos/BooleanVal2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ private BooleanVal2(UnionType<True, False> input) : base(input) { }
/// </summary>
public class True : ValueOf<bool, True>
{
public True() : base(true) { }

public static True Instance { get; } = From(true);

public static implicit operator bool(True _) => true;
Expand All @@ -25,6 +27,8 @@ public class True : ValueOf<bool, True>
/// </summary>
public class False : ValueOf<bool, False>
{
public False() : base(false) { }

public static False Instance { get; } = From(false);

public static implicit operator bool(False _) => false;
Expand Down Expand Up @@ -70,8 +74,8 @@ public class False : ValueOf<bool, False>

public static implicit operator bool(BooleanVal2 value)
{
if (value.IsT0()) return value.AsT0();
if (value.IsT1()) return value.AsT1();
if (value.IsT0()) return value.AsT0()!;
if (value.IsT1()) return value.AsT1()!;
return false;
}

Expand Down Expand Up @@ -172,5 +176,5 @@ public static class BooleanVal2Extensions
/// <param name="value"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BooleanVal2 ToBooleanVal2(this bool? value) => value.HasValue ? value.Value : BooleanVal2.FalseVal;
public static BooleanVal2 ToBooleanVal2(this bool? value) => value ?? BooleanVal2.FalseVal;
}
12 changes: 9 additions & 3 deletions src/Cosmos.Extensions.Optionals/Cosmos/BooleanVal3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ private BooleanVal3(UnionType<True, False, Null> input) : base(input) { }
/// </summary>
public class True : ValueOf<bool?, True>
{
public True() : base(true) { }

public static True Instance { get; } = From(true);

public static implicit operator bool?(True _) => true;
Expand All @@ -25,6 +27,8 @@ public class True : ValueOf<bool?, True>
/// </summary>
public class False : ValueOf<bool?, False>
{
public False() : base(false) { }

public static False Instance { get; } = From(false);

public static implicit operator bool?(False _) => false;
Expand All @@ -35,6 +39,8 @@ public class False : ValueOf<bool?, False>
/// </summary>
public class Null : ValueOf<bool?, Null>
{
public Null() : base(null) { }

public static Null Instance { get; } = From(null);

public static implicit operator bool?(Null _) => null;
Expand Down Expand Up @@ -98,9 +104,9 @@ public class Null : ValueOf<bool?, Null>
public static implicit operator bool?(BooleanVal3 value)
{
if (value is null) return null;
if (value.IsT0()) return value.AsT0();
if (value.IsT1()) return value.AsT1();
if (value.IsT2()) return value.AsT2();
if (value.IsT0()) return value.AsT0()!;
if (value.IsT1()) return value.AsT1()!;
if (value.IsT2()) return value.AsT2()!;
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ public IDictionary<string, dynamic> ToDictionary()
/// </summary>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
#pragma warning disable CS8633
public IDictionary<T, V> ToDictionary<T, V>(Func<KeyValuePair<string, dynamic>, T> keySelector, Func<KeyValuePair<string, dynamic>, V> valueSelector) where T : notnull
#pragma warning restore CS8633
{
if (keySelector is null)
throw new ArgumentNullException(nameof(keySelector));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public dynamic this[int index]
/// Indexer
/// </summary>
/// <param name="key"></param>
public dynamic this[string key] => _dynamicDictionary.TryGetValue(key, out var result) ? result : default;
public dynamic this[string key] => _dynamicDictionary.TryGetValue(key, out var result) ? result : default!;

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ internal static class CollectionHelper
/// </summary>
/// <param name="coll"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(IEnumerable coll)
{
if (coll is null)
return true;

return !coll.Cast<object>().Any();
}
public static bool IsNullOrEmpty(IEnumerable coll) => coll is null || !coll.Cast<object>().Any();

/// <summary>
/// To judge whether the collection is null or empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public Maybe<T> MatchType<TTarget>(Action<TTarget> action) where TTarget : T

if (UnderlyingType == typeof(TTarget))
{
action((TTarget)Value);
action((TTarget)Value!);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal Maybe(T1 value1, T2 value2, bool hasValue)
internal Maybe(T1 value1, string key1, T2 value2, string key2, bool hasValue)
{
_o1 = Optional.From(value1);
_o2 = Cosmos.Optionals.Optional.From(value2);
_o2 = Optional.From(value2);
_hasValue = hasValue;
_optionalIndexCache = NamedMaybeHelper.CreateIndexCache(2, key1, key2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ReSharper disable MemberHidesStaticFromOuterClass

namespace Cosmos.Optionals;

/// <summary>
Expand Down Expand Up @@ -96,8 +98,7 @@ public static IOptional<T> From<T>(T value)
// ReSharper disable once MemberHidesStaticFromOuterClass
public static IOptional<T> From<T>(T value, Func<T, bool> condition)
{
if (condition is null)
throw new ArgumentNullException(nameof(condition));
ArgumentNullException.ThrowIfNull(condition);
if (value is null)
return None<T>();
return condition(value) ? Some(value) : None<T>();
Expand Down
3 changes: 1 addition & 2 deletions src/Cosmos.Extensions.Optionals/Cosmos/Optionals/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public static Maybe<T> From<T>(T value)
/// <exception cref="ArgumentNullException"></exception>
public static Maybe<T> From<T>(T value, Func<T, bool> condition)
{
if (condition is null)
throw new ArgumentNullException(nameof(condition));
ArgumentNullException.ThrowIfNull(condition);
if (value is null)
return None<T>();
return condition(value) ? Some(value) : None<T>();
Expand Down
Loading

0 comments on commit 54bd2e0

Please sign in to comment.