-
Notifications
You must be signed in to change notification settings - Fork 5
Conditional Code Action
<map:Conditional>
<map:Case>
<map:Case.Condition>
... MAPPING-CONDITION ...
</map:Case.Condition>
<map:Case.Action>
... MAPPING-ACTION ...
</map:Case.Action>
</map:Case>
...
<map:Case>
<map:Case.Condition>
... MAPPING-CONDITION ...
</map:Case.Condition>
<map:Case.Action>
... MAPPING-ACTION ...
</map:Case.Action>
</map:Case>
<map:Default>
... MAPPING-ACTION ...
</map:Default>
</map:Conditional>
Applies mapping actions depending on conditions.
See [Writing case-based conditional mappings]( Writing case-based conditional mappings).
Property | Usage | Description |
---|---|---|
CaseConditions | Content property | A sequence of conditions |
A Case
element is used to specify one possible conversion for an API.
Property | Usage | Description |
---|---|---|
Condition | Required | A code mapping condition instance |
Action | Required | A code mapping mapping action to be executed if the condition succeeds |
The Default
element contains the code mapping action to be executed when no other Case
was successful. This element is required.
The Conditional mapping action is useful when converting elements which vary given the origin expression.
For example the MessageBox.Show method has several overloads. We can define a separate conversion for each case, for example the following mapping will apply a different conversion if the original call expression one or three arguments.
Say that we are creating a mapping for MessageBox.Show. This method has several overloads. In this case we want to create a mapping that does something for the case when the method is called with one argument, and do something else when the method is called with three arguments.
Here's the mapping:
<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers">
<MapUnit.Elements>
<map:CodeMapPackage Type="System.Windows.MessageBox">
<map:CodeMapPackage.Maps>
<map:CodeMap Kind="Call" MemberName="Show">
<map:Conditional>
<!-- Case for one argument -->
<map:Case>
<map:Case.Condition>
<map:WithMethodCall>
<map:ArgumentCount>1</map:ArgumentCount>
<map:WithArgument Position="0">
<map:AssignName>$message</map:AssignName>
</map:WithArgument>
</map:WithMethodCall>
</map:Case.Condition>
<map:Case.Action>
<map:ReplaceWithTemplate>
await (new Windows.UI.Popups.MessageDialog($message)).ShowAsync()
</map:ReplaceWithTemplate>
</map:Case.Action>
</map:Case>
<!-- Case for three arguments with OK -->
<map:Case>
<map:Case.Condition>
<map:WithMethodCall>
<map:ArgumentCount>3</map:ArgumentCount>
<map:WithArgument Position="0">
<map:AssignName>$message</map:AssignName>
</map:WithArgument>
<map:WithArgument Position="1">
<map:AssignName>$title</map:AssignName>
</map:WithArgument>
<map:WithArgument Position="2">
<map:Equals>MessageBoxButton.OK</map:Equals>
</map:WithArgument>
</map:WithMethodCall>
</map:Case.Condition>
<map:Case.Action>
<map:ReplaceWithTemplate>
await (new Windows.UI.Popups.MessageDialog($message, $title)).ShowAsync()
</map:ReplaceWithTemplate>
</map:Case.Action>
</map:Case>
<map:Default>
<map:MarkAsNotMapped />
</map:Default>
</map:Conditional>
</map:CodeMap>
</map:CodeMapPackage.Maps>
</map:CodeMapPackage>
</MapUnit.Elements>
</MapUnit>
- The cases are evaluated from top to bottom
- When the first case succeeds the conditional evaluation finishes
- The
Default
element is required, and its inner action will be executed when no otherCase
succeed.
Contact us for more information
Overview
Writing mappings
Code Mapping Actions
- ActionSequence
- AddHelper
- AddNamespaceImport
- AddPreStatementFromTemplate
- CommentOut
- Conditional
- Keep Code Mapping Action
- MarkAsNotMapped
- RedirectCall
- RedirectCallToInnerMember
- RedirectIndexer
- RedirectProperty
- RemoveCurrentStatement
- RemoveParameter
- ReplaceClassUsage
- ReplaceMethodBodyWithTemplate
- ReplaceParameterDeclarationType
- ReplaceParameterMember
- ReplaceParameterValue
- ReplaceWithMethodCall
- ReplaceWithProperty
- ReplaceWithTemplate
Code Mapping Conditions
- AllConditionsApply
- ArgumentCount
- AssignName
- AssignNameToArgumentRange
- IsExpressionOfType
- IsStringLiteralMatchingRegex
- WithArgument
- WithAssignment
- WithAssignmentLeftSide
- WithAssignmentRightSide
- WithCalledMemberOwner
- WithCalledMethodExpression
- WithConstructorCall
- WithLambdaExpressionBody
- WithLambdaExpressionParameter
- WithLeftSideOfDottedAccess
- WithMemberInitValue
- WithMethodCall
XAML mapping actions
- ActionSequence
- AddStatementToConstructorFromTemplate
- BindPropertyValueElement Xaml mapping action
- ChangeEventHandlerEventArgsType
- CommentOutElement
- CommentOutProperty
- MarkAsNotMapped
- MoveValueToContentProperty
- RemoveNamespaceDeclaration
- RenameElement
- RenameProperty
- ReplaceAttributeValue
- ReplaceEventHandlerBodyWithTemplate
- ReplaceEventHandlerParameterMember
- ReplaceNamespaceDeclaration
- ReplacePropertyValueWithParentResource
- ReplaceStaticResourceWithThemeResource
- SetPropertyValueToComplexElement
- SetPropertyValueToSimpleValue
- WrapContent
XAML mapping conditions
Misc