Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Try... pattern to ParameterConversion, modify Guard wrapping with… #451

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Stateless/ParameterConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,64 @@ public static object Unpack(object[] args, Type argType, int index)

return arg;
}

public static bool TryUnpack(object[] args, Type argType, int index, out object result)
{
if (args == null)
{
result = null;
return false;
}

if (args.Length == 0)
{
result = null;
return false;
}

if (args.Length <= index)
{
result = null;
return false;
}

var arg = args[index];

if (arg != null && !argType.IsAssignableFrom(arg.GetType()))
{
result = null;
return false;
}

result = arg;
return true;
}

public static TArg Unpack<TArg>(object[] args, int index)
{
if (args.Length == 0) return default;

return (TArg)Unpack(args, typeof(TArg), index);
}

public static bool TryUnpack<TArg>(object[] args, int index, out TArg result)
{
if (args.Length == 0)
{
result = default;
return true;
}

object rawResult;
if (!TryUnpack(args, typeof(TArg), index, out rawResult))
{
result = default;
return false;
}

result = (TArg) rawResult;
return true;
}

public static void Validate(object[] args, Type[] expected)
{
Expand Down
18 changes: 10 additions & 8 deletions src/Stateless/TransitionGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ internal class TransitionGuard

public static Func<object[], bool> ToPackedGuard<TArg0>(Func<TArg0, bool> guard)
{
return args => guard(ParameterConversion.Unpack<TArg0>(args, 0));
return args => ParameterConversion.TryUnpack<TArg0>(args, 0, out var arg) && guard(arg);
}

public static Func<object[], bool> ToPackedGuard<TArg0, TArg1>(Func<TArg0, TArg1, bool> guard)
{
return args => guard(
ParameterConversion.Unpack<TArg0>(args, 0),
ParameterConversion.Unpack<TArg1>(args, 1));
return args =>
ParameterConversion.TryUnpack<TArg0>(args, 0, out var arg0)
&& ParameterConversion.TryUnpack<TArg1>(args, 1, out var arg1)
&& guard(arg0, arg1);
}

public static Func<object[], bool> ToPackedGuard<TArg0, TArg1, TArg2>(Func<TArg0, TArg1, TArg2, bool> guard)
{
return args => guard(
ParameterConversion.Unpack<TArg0>(args, 0),
ParameterConversion.Unpack<TArg1>(args, 1),
ParameterConversion.Unpack<TArg2>(args, 2));
return args =>
ParameterConversion.TryUnpack<TArg0>(args, 0, out var arg0)
&& ParameterConversion.TryUnpack<TArg1>(args, 1, out var arg1)
&& ParameterConversion.TryUnpack<TArg2>(args, 2, out var arg2)
&& guard(arg0, arg1, arg2);
}

public static Tuple<Func<object[], bool>, string>[] ToPackedGuards<TArg0>(Tuple<Func<TArg0, bool>, string>[] guards)
Expand Down