Skip to content

Commit

Permalink
Renamed generic type parameter from TContext to TState
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Nov 6, 2024
1 parent 2000026 commit 7f6fd79
Show file tree
Hide file tree
Showing 11 changed files with 1,024 additions and 1,024 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static void Demo(ILogger logger)
text: s => logger.Information("[SwitchPartially] String Action: {Text}", s));

textOrNumberFromString.Switch(logger,
text: static (l, s) => l.Information("[Switch] String Action with context: {Text}", s),
number: static (l, i) => l.Information("[Switch] Int Action with context: {Number}", i));
text: static (l, s) => l.Information("[Switch] String Action with state: {Text}", s),
number: static (l, i) => l.Information("[Switch] Int Action with state: {Number}", i));

textOrNumberFromString.SwitchPartially(logger,
@default: static (l, i) => l.Information("[SwitchPartially] Default Action with context: {Number}", i),
text: static (l, s) => l.Information("[SwitchPartially] String Action with context: {Text}", s));
@default: static (l, i) => l.Information("[SwitchPartially] Default Action with state: {Number}", i),
text: static (l, s) => l.Information("[SwitchPartially] String Action with state: {Text}", s));

var switchResponse = textOrNumberFromInt.Switch(text: static s => $"[Switch] String Func: {s}",
number: static i => $"[Switch] Int Func: {i}");
Expand All @@ -53,13 +53,13 @@ public static void Demo(ILogger logger)
logger.Information("{Response}", switchPartiallyResponse);

var switchResponseWithContext = textOrNumberFromInt.Switch(123.45,
text: static (ctx, s) => $"[Switch] String Func with context: {ctx} | {s}",
number: static (ctx, i) => $"[Switch] Int Func with context: {ctx} | {i}");
text: static (state, s) => $"[Switch] String Func with state: {state} | {s}",
number: static (state, i) => $"[Switch] Int Func with state: {state} | {i}");
logger.Information("{Response}", switchResponseWithContext);

var switchPartiallyResponseWithContext = textOrNumberFromInt.SwitchPartially(123.45,
text: static (ctx, s) => $"[SwitchPartially] String Func with context: {ctx} | {s}",
@default: static (ctx, i) => $"[SwitchPartially] Default Func with context: {ctx} | {i}");
text: static (state, s) => $"[SwitchPartially] String Func with state: {state} | {s}",
@default: static (state, i) => $"[SwitchPartially] Default Func with state: {state} | {i}");
logger.Information("{Response}", switchPartiallyResponseWithContext);

var mapResponse = textOrNumberFromString.Map(text: "[Map] Mapped string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ private static void SwitchWithFunc(ILogger logger)
housewares: static () => "Switch with Func<TResult>: Housewares");
logger.Information("{ReturnValue}", returnValue);

// Switch with Func<TContext, TResult>
// Switch with Func<TState, TResult>
returnValue = productType.Switch(logger,
groceries: static _ => "Switch with Func<TContext, TResult>: Groceries",
housewares: static _ => "Switch with Func<TContext, TResult>: Housewares");
groceries: static _ => "Switch with Func<TState, TResult>: Groceries",
housewares: static _ => "Switch with Func<TState, TResult>: Housewares");
logger.Information("{ReturnValue}", returnValue);

// SwitchPartially with Func<TResult>
Expand All @@ -139,19 +139,19 @@ private static void SwitchWithFunc(ILogger logger)
groceries: () => "SwitchPartially with Func<TResult>: Groceries");
logger.Information("{ReturnValue}", returnValue);

// SwitchPartially with Func<TContext, TResult>
// SwitchPartially with Func<TState, TResult>
returnValue = productType.SwitchPartially(logger,
@default: static (_, item) => $"SwitchPartially with Func<TContext, TResult>: default ('{item}')",
groceries: static _ => "SwitchPartially with Func<TContext, TResult>: Groceries");
@default: static (_, item) => $"SwitchPartially with Func<TState, TResult>: default ('{item}')",
groceries: static _ => "SwitchPartially with Func<TState, TResult>: Groceries");
logger.Information("{ReturnValue}", returnValue);

returnValue = productType.SwitchPartially(logger,
@default: static (_, item) => $"SwitchPartially with Func<TContext, TResult>: {item} (default only)");
@default: static (_, item) => $"SwitchPartially with Func<TState, TResult>: {item} (default only)");
logger.Information("{ReturnValue}", returnValue);

returnValue = ProductType.Housewares.SwitchPartially(logger,
@default: static (_, item) => $"SwitchPartially with Func<TContext, TResult>: default ('{item}')",
groceries: static _ => "SwitchPartially with Func<TContext, TResult>: Groceries");
@default: static (_, item) => $"SwitchPartially with Func<TState, TResult>: default ('{item}')",
groceries: static _ => "SwitchPartially with Func<TState, TResult>: Groceries");
logger.Information("{ReturnValue}", returnValue);
}

Expand All @@ -163,7 +163,7 @@ private static void SwitchWithAction(ILogger logger)
productType.Switch(groceries: () => logger.Information("Switch with Action: Groceries"),
housewares: () => logger.Information("Switch with Action: Housewares"));

// Switch with Action<TContext>
// Switch with Action<TState>
productType.Switch(logger,
groceries: static l => l.Information("Switch with Action: Groceries"),
housewares: static l => l.Information("Switch with Action: Housewares"));
Expand All @@ -188,20 +188,20 @@ private static void SwitchWithAction(ILogger logger)
apple: () => Console.WriteLine("SwitchPartially with Action: apple"),
orange: () => Console.WriteLine("SwitchPartially with Action: orange"));

// SwitchPartially with Action<TContext>
// SwitchPartially with Action<TState>
productType.SwitchPartially(logger,
@default: static (l, item) => l.Information("SwitchPartially with Action<TContext>: default ('{Item}')", item),
groceries: static l => l.Information("SwitchPartially with Action<TContext>: Groceries"));
@default: static (l, item) => l.Information("SwitchPartially with Action<TState>: default ('{Item}')", item),
groceries: static l => l.Information("SwitchPartially with Action<TState>: Groceries"));

productType.SwitchPartially(logger,
groceries: static l => l.Information("SwitchPartially with Action<TContext>: Groceries (no default)"));
groceries: static l => l.Information("SwitchPartially with Action<TState>: Groceries (no default)"));

productType.SwitchPartially(logger,
@default: static (l, item) => l.Information("SwitchPartially with Action<TContext>: {Item} (default only)", item));
@default: static (l, item) => l.Information("SwitchPartially with Action<TState>: {Item} (default only)", item));

ProductType.Housewares.SwitchPartially(logger,
@default: static (l, item) => l.Information("SwitchPartially with Action<TContext>: default ('{Item}')", item),
groceries: static l => l.Information("SwitchPartially with Action<TContext>: Groceries"));
@default: static (l, item) => l.Information("SwitchPartially with Action<TState>: default ('{Item}')", item),
groceries: static l => l.Information("SwitchPartially with Action<TState>: Groceries"));
}

private static void DemoForSmartEnumWithCustomComparer(ILogger logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,18 @@ public bool Equals(").AppendTypeFullyQualifiedNullAnnotated(_state).Append(@" ot
}");
}

private void GenerateSwitchForAction(bool withContext, bool isPartially)
private void GenerateSwitchForAction(bool withState, bool isPartially)
{
_sb.Append(@"
/// <summary>
/// Executes an action depending on the current value.
/// </summary>");

if (withContext)
if (withState)
{
_sb.Append(@"
/// <param name=""context"">Context to be passed to the callbacks.</param>");
/// <param name=""state"">State to be passed to the callbacks.</param>");
}

if (isPartially)
Expand All @@ -400,11 +400,11 @@ private void GenerateSwitchForAction(bool withContext, bool isPartially)

var methodName = isPartially ? "SwitchPartially" : "Switch";

if (withContext)
if (withState)
{
_sb.Append(@"
public void ").Append(methodName).Append(@"<TContext>(
TContext context,");
public void ").Append(methodName).Append(@"<TState>(
TState state,");
}
else
{
Expand All @@ -417,8 +417,8 @@ private void GenerateSwitchForAction(bool withContext, bool isPartially)
_sb.Append(@"
global::System.Action<");

if (withContext)
_sb.Append("TContext, ");
if (withState)
_sb.Append("TState, ");

_sb.Append("object?>? @default = null,");
}
Expand All @@ -433,8 +433,8 @@ private void GenerateSwitchForAction(bool withContext, bool isPartially)
_sb.Append(@"
global::System.Action<");

if (withContext)
_sb.Append("TContext, ");
if (withState)
_sb.Append("TState, ");

_sb.AppendTypeFullyQualified(memberType).Append(">");

Expand All @@ -450,13 +450,13 @@ private void GenerateSwitchForAction(bool withContext, bool isPartially)
_sb.Append(@")
{");

GenerateIndexBasedActionSwitchBody(withContext, isPartially);
GenerateIndexBasedActionSwitchBody(withState, isPartially);

_sb.Append(@"
}");
}

private void GenerateIndexBasedActionSwitchBody(bool withContext, bool isPartially)
private void GenerateIndexBasedActionSwitchBody(bool withState, bool isPartially)
{
_sb.Append(@"
switch (this._valueIndex)
Expand Down Expand Up @@ -487,8 +487,8 @@ private void GenerateIndexBasedActionSwitchBody(bool withContext, bool isPartial
_sb.Append(@"
").AppendEscaped(memberType.ArgumentName).Append("(");

if (withContext)
_sb.Append("context, ");
if (withState)
_sb.Append("state, ");

_sb.Append("this._").Append(memberType.BackingFieldName).Append(memberType.IsReferenceType && memberType.NullableAnnotation != NullableAnnotation.Annotated ? "!" : null).Append(@");
return;");
Expand All @@ -505,25 +505,25 @@ private void GenerateIndexBasedActionSwitchBody(bool withContext, bool isPartial
@default?.Invoke(");

if (withContext)
_sb.Append("context, ");
if (withState)
_sb.Append("state, ");

_sb.Append("this.Value);");
}
}

private void GenerateSwitchForFunc(bool withContext, bool isPartially)
private void GenerateSwitchForFunc(bool withState, bool isPartially)
{
_sb.Append(@"
/// <summary>
/// Executes a function depending on the current value.
/// </summary>");

if (withContext)
if (withState)
{
_sb.Append(@"
/// <param name=""context"">Context to be passed to the callbacks.</param>");
/// <param name=""state"">State to be passed to the callbacks.</param>");
}

if (isPartially)
Expand All @@ -548,11 +548,11 @@ private void GenerateSwitchForFunc(bool withContext, bool isPartially)

var methodName = isPartially ? "SwitchPartially" : "Switch";

if (withContext)
if (withState)
{
_sb.Append(@"
public TResult ").Append(methodName).Append(@"<TContext, TResult>(
TContext context,");
public TResult ").Append(methodName).Append(@"<TState, TResult>(
TState state,");
}
else
{
Expand All @@ -565,8 +565,8 @@ private void GenerateSwitchForFunc(bool withContext, bool isPartially)
_sb.Append(@"
global::System.Func<");

if (withContext)
_sb.Append("TContext, ");
if (withState)
_sb.Append("TState, ");

_sb.Append("object?, TResult> @default,");
}
Expand All @@ -583,8 +583,8 @@ private void GenerateSwitchForFunc(bool withContext, bool isPartially)

_sb.Append("global::System.Func<");

if (withContext)
_sb.Append("TContext, ");
if (withState)
_sb.Append("TState, ");

_sb.AppendTypeFullyQualified(memberType).Append(", TResult>");

Expand All @@ -600,13 +600,13 @@ private void GenerateSwitchForFunc(bool withContext, bool isPartially)
_sb.Append(@")
{");

GenerateIndexBasedFuncSwitchBody(withContext, isPartially);
GenerateIndexBasedFuncSwitchBody(withState, isPartially);

_sb.Append(@"
}");
}

private void GenerateIndexBasedFuncSwitchBody(bool withContext, bool isPartially)
private void GenerateIndexBasedFuncSwitchBody(bool withState, bool isPartially)
{
_sb.Append(@"
switch (this._valueIndex)
Expand Down Expand Up @@ -637,8 +637,8 @@ private void GenerateIndexBasedFuncSwitchBody(bool withContext, bool isPartially
_sb.Append(@"
return ").AppendEscaped(memberType.ArgumentName).Append("(");

if (withContext)
_sb.Append("context, ");
if (withState)
_sb.Append("state, ");

_sb.Append("this._").Append(memberType.BackingFieldName).Append(memberType is { IsReferenceType: true, Setting.IsNullableReferenceType: false } ? "!" : null).Append(");");
}
Expand All @@ -654,8 +654,8 @@ private void GenerateIndexBasedFuncSwitchBody(bool withContext, bool isPartially
return @default(");

if (withContext)
_sb.Append("context, ");
if (withState)
_sb.Append("state, ");

_sb.Append("this.Value);");
}
Expand Down
Loading

0 comments on commit 7f6fd79

Please sign in to comment.