Autogenerate switch case labels
This commit is contained in:
parent
f048e6d3ef
commit
05c15a2eaf
|
|
@ -15,11 +15,24 @@ namespace Elsa.Activities.ControlFlow
|
|||
Cases.Add(new SwitchCaseDescriptor(name, condition, builder => branch(builder.When(name))));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SwitchCaseBuilder Add(Func<ActivityExecutionContext, ValueTask<bool>> condition, Action<IOutcomeBuilder> branch)
|
||||
{
|
||||
var caseLabel = GetNextCaseLabel();
|
||||
Cases.Add(new SwitchCaseDescriptor(caseLabel, condition, builder => branch(builder.When(caseLabel))));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SwitchCaseBuilder Add(string name, Func<ActivityExecutionContext, bool> condition, Action<IOutcomeBuilder> branch) => Add(name, context => new ValueTask<bool>(condition(context)), branch);
|
||||
public SwitchCaseBuilder Add(string name, Func<ValueTask<bool>> condition, Action<IOutcomeBuilder> branch) => Add(name, _ => condition(), branch);
|
||||
public SwitchCaseBuilder Add(string name, Func<bool> condition, Action<IOutcomeBuilder> branch) => Add(name, _ => condition(), branch);
|
||||
public SwitchCaseBuilder Add(string name, bool condition, Action<IOutcomeBuilder> branch) => Add(name, _ => condition, branch);
|
||||
public SwitchCaseBuilder Add(Func<ActivityExecutionContext, bool> condition, Action<IOutcomeBuilder> branch) => Add(GetNextCaseLabel(), context => new ValueTask<bool>(condition(context)), branch);
|
||||
public SwitchCaseBuilder Add(Func<ValueTask<bool>> condition, Action<IOutcomeBuilder> branch) => Add(GetNextCaseLabel(), _ => condition(), branch);
|
||||
public SwitchCaseBuilder Add(Func<bool> condition, Action<IOutcomeBuilder> branch) => Add(GetNextCaseLabel(), _ => condition(), branch);
|
||||
public SwitchCaseBuilder Add(bool condition, Action<IOutcomeBuilder> branch) => Add(GetNextCaseLabel(), _ => condition, branch);
|
||||
|
||||
private string GetNextCaseLabel() => $"Case {Cases.Count + 1}";
|
||||
}
|
||||
|
||||
public record SwitchCaseDescriptor(string Name, Func<ActivityExecutionContext, ValueTask<bool>> Condition, Action<IActivityBuilder> OutcomeBuilder);
|
||||
|
|
|
|||
|
|
@ -21,16 +21,16 @@ namespace Elsa.Samples.SwitchConsole
|
|||
.WriteLine("--Grayscale Calculator--").WithName("Start")
|
||||
.WriteLine("Enter a number between 0 and 100.")
|
||||
.ReadLine()
|
||||
.Switch( cases =>
|
||||
{
|
||||
cases.Add("Black", context => GetNumber(context) >= 0 && GetNumber(context) < 20, @case => @case.WriteLine("That number is black"));
|
||||
cases.Add("Dark Gray", context => GetNumber(context) >= 20 && GetNumber(context) < 50, @case => @case.WriteLine("That number is dark gray"));
|
||||
cases.Add("Light Gray", context => GetNumber(context) >= 50 && GetNumber(context) < 70, @case => @case.WriteLine("That number is light gray"));
|
||||
cases.Add("Gray", context => GetNumber(context) >= 20 && GetNumber(context) < 70, @case => @case.WriteLine("That number is gray"));
|
||||
cases.Add("White", context => GetNumber(context) >= 70 && GetNumber(context) <= 100, @case => @case.WriteLine("That number is white"));
|
||||
cases.Add("Invalid", context => GetNumber(context) < 0 || GetNumber(context) > 100, @case => @case.WriteLine("That number is invalid"));
|
||||
})
|
||||
.WriteLine("Goodbye");
|
||||
.Switch(cases =>
|
||||
{
|
||||
cases.Add(context => GetNumber(context) >= 0 && GetNumber(context) < 20, @case => @case.WriteLine("That number is black"));
|
||||
cases.Add(context => GetNumber(context) >= 20 && GetNumber(context) < 50, @case => @case.WriteLine("That number is dark gray"));
|
||||
cases.Add(context => GetNumber(context) >= 50 && GetNumber(context) < 70, @case => @case.WriteLine("That number is light gray"));
|
||||
cases.Add(context => GetNumber(context) >= 20 && GetNumber(context) < 70, @case => @case.WriteLine("That number is gray"));
|
||||
cases.Add(context => GetNumber(context) >= 70 && GetNumber(context) <= 100, @case => @case.WriteLine("That number is white"));
|
||||
cases.Add(context => GetNumber(context) < 0 || GetNumber(context) > 100, @case => @case.WriteLine("That number is invalid"));
|
||||
})
|
||||
.WriteLine("Thanks for playing!");
|
||||
}
|
||||
|
||||
private static int GetNumber(ActivityExecutionContext context) => context.GetInput<int>();
|
||||
|
|
|
|||
Loading…
Reference in a new issue