Skip to content

Commit

Permalink
feat: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Nov 19, 2023
1 parent 9e5ca4f commit 175df38
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,49 @@ IReadOnlyCollection<IExistingTargetMapping> existingTargetTypeMappings
public override IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx, ExpressionSyntax target)
{
var sourceExpression = TupleExpression(CommaSeparatedList(Argument(ctx.Source), Argument(target)));
var sections = _existingTargetTypeMappings.Select(x => BuildSwitchSection(ctx, x)).Append(BuildDefaultSwitchSection(ctx));
var sections = _existingTargetTypeMappings.Select(x => BuildSwitchSection(ctx, x)).Append(BuildDefaultSwitchSection(ctx, target));

yield return ctx.SyntaxFactory.SwitchStatement(sourceExpression, List(sections)).AddLeadingLineFeed(ctx.SyntaxFactory.Indentation);
}

private SwitchSectionSyntax BuildSwitchSection(TypeMappingBuildContext ctx, IExistingTargetMapping mapping)
{
// MapToB(source, target);
var (sectionCtx, sourceVariableName) = ctx.WithNewScopedSource(SourceName);
var targetVariableName = sectionCtx.NameBuilder.New(TargetName);
sectionCtx = sectionCtx.AddIndentation();

// (A source, B target)
var positionalTypeMatch = PositionalPatternClause(
CommaSeparatedList(
Subpattern(DeclarationPattern(mapping.SourceType, sourceVariableName)),
Subpattern(DeclarationPattern(mapping.TargetType, targetVariableName))
)
);

var pattern = RecursivePattern().WithPositionalPatternClause(positionalTypeMatch);
var caseLabel = CasePatternSwitchLabel(pattern).AddLeadingLineFeed(sectionCtx.SyntaxFactory.Indentation);

var target = IdentifierName(targetVariableName);
// case (A source, B target):
var caseLabel = CasePatternSwitchLabel(pattern).AddLeadingLineFeed(sectionCtx.SyntaxFactory.Indentation);

var statementContext = sectionCtx.AddIndentation();
var statements = List(
mapping.Build(statementContext, target).Append(BreakStatement().AddLeadingLineFeed(statementContext.SyntaxFactory.Indentation))
);
var section = SwitchSection().WithLabels(SingletonList<SwitchLabelSyntax>(caseLabel)).WithStatements(statements);

return section;
// break;
var breakStatement = BreakStatement().AddLeadingLineFeed(statementContext.SyntaxFactory.Indentation);
var target = IdentifierName(targetVariableName);
var statements = List(mapping.Build(statementContext, target).Append(breakStatement));

return SwitchSection().WithLabels(SingletonList<SwitchLabelSyntax>(caseLabel)).WithStatements(statements);
}

private SwitchSectionSyntax BuildDefaultSwitchSection(TypeMappingBuildContext ctx)
private SwitchSectionSyntax BuildDefaultSwitchSection(TypeMappingBuildContext ctx, ExpressionSyntax target)
{
// default:
var defaultCaseLabel = DefaultSwitchLabel().AddLeadingLineFeed(ctx.SyntaxFactory.Indentation + 1);

// throw new ArgumentException(msg, nameof(ctx.Source)),
var sourceType = Invocation(MemberAccess(ctx.Source, GetTypeMethodName));
var targetType = Invocation(MemberAccess(target, GetTypeMethodName));
var throwExpression = ThrowArgumentExpression(
InterpolatedString($"Cannot map {sourceType} to {TargetType.ToDisplayString()} as there is no known derived type mapping"),
InterpolatedString($"Cannot map {sourceType} to {targetType} as there is no known derived type mapping"),
ctx.Source
)
.AddLeadingLineFeed(ctx.SyntaxFactory.Indentation + 2);
Expand Down

0 comments on commit 175df38

Please sign in to comment.