Generate partial class and assign destination properties based on an attribute.
This commit is contained in:
commit
f607df6312
|
@ -0,0 +1,442 @@
|
||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.rsuser
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Mono auto generated files
|
||||||
|
mono_crash.*
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
[Rr]eleases/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
[Aa][Rr][Mm]/
|
||||||
|
[Aa][Rr][Mm]64/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Ll]og/
|
||||||
|
[Ll]ogs/
|
||||||
|
|
||||||
|
# Visual Studio 2015/2017 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# Visual Studio 2017 auto generated files
|
||||||
|
Generated\ Files/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUnit
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
nunit-*.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# Benchmark Results
|
||||||
|
BenchmarkDotNet.Artifacts/
|
||||||
|
|
||||||
|
# .NET Core
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
|
||||||
|
# StyleCop
|
||||||
|
StyleCopReport.xml
|
||||||
|
|
||||||
|
# Files built by Visual Studio
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_h.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.iobj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*_wpftmp.csproj
|
||||||
|
*.log
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# Visual Studio Trace Files
|
||||||
|
*.e2e
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# AxoCover is a Code Coverage Tool
|
||||||
|
.axoCover/*
|
||||||
|
!.axoCover/settings.json
|
||||||
|
|
||||||
|
# Coverlet is a free, cross platform Code Coverage Tool
|
||||||
|
coverage*[.json, .xml, .info]
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# NuGet Symbol Packages
|
||||||
|
*.snupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/[Pp]ackages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/[Pp]ackages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/[Pp]ackages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
*.appx
|
||||||
|
*.appxbundle
|
||||||
|
*.appxupload
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!?*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Including strong name files can present a security risk
|
||||||
|
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||||
|
#*.snk
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
ServiceFabricBackup/
|
||||||
|
*.rptproj.bak
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
*.ndf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
*.rptproj.rsuser
|
||||||
|
*- [Bb]ackup.rdl
|
||||||
|
*- [Bb]ackup ([0-9]).rdl
|
||||||
|
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# CodeRush personal settings
|
||||||
|
.cr/personal
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Tabs Studio
|
||||||
|
*.tss
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
|
|
||||||
|
# OpenCover UI analysis results
|
||||||
|
OpenCover/
|
||||||
|
|
||||||
|
# Azure Stream Analytics local run output
|
||||||
|
ASALocalRun/
|
||||||
|
|
||||||
|
# MSBuild Binary and Structured Log
|
||||||
|
*.binlog
|
||||||
|
|
||||||
|
# NVidia Nsight GPU debugger configuration file
|
||||||
|
*.nvuser
|
||||||
|
|
||||||
|
# MFractors (Xamarin productivity tool) working folder
|
||||||
|
.mfractor/
|
||||||
|
|
||||||
|
# Local History for Visual Studio
|
||||||
|
.localhistory/
|
||||||
|
|
||||||
|
# BeatPulse healthcheck temp database
|
||||||
|
healthchecksdb
|
||||||
|
|
||||||
|
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||||
|
MigrationBackup/
|
||||||
|
|
||||||
|
# Ionide (cross platform F# VS Code tools) working folder
|
||||||
|
.ionide/
|
||||||
|
|
||||||
|
##
|
||||||
|
## Visual studio for Mac
|
||||||
|
##
|
||||||
|
|
||||||
|
|
||||||
|
# globs
|
||||||
|
Makefile.in
|
||||||
|
*.userprefs
|
||||||
|
*.usertasks
|
||||||
|
config.make
|
||||||
|
config.status
|
||||||
|
aclocal.m4
|
||||||
|
install-sh
|
||||||
|
autom4te.cache/
|
||||||
|
*.tar.gz
|
||||||
|
tarballs/
|
||||||
|
test-results/
|
||||||
|
|
||||||
|
# Mac bundle stuff
|
||||||
|
*.dmg
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
|
||||||
|
# General
|
||||||
|
.DS_Store
|
||||||
|
.AppleDouble
|
||||||
|
.LSOverride
|
||||||
|
|
||||||
|
# Icon must end with two \r
|
||||||
|
Icon
|
||||||
|
|
||||||
|
|
||||||
|
# Thumbnails
|
||||||
|
._*
|
||||||
|
|
||||||
|
# Files that might appear in the root of a volume
|
||||||
|
.DocumentRevisions-V100
|
||||||
|
.fseventsd
|
||||||
|
.Spotlight-V100
|
||||||
|
.TemporaryItems
|
||||||
|
.Trashes
|
||||||
|
.VolumeIcon.icns
|
||||||
|
.com.apple.timemachine.donotpresent
|
||||||
|
|
||||||
|
# Directories potentially created on remote AFP share
|
||||||
|
.AppleDB
|
||||||
|
.AppleDesktop
|
||||||
|
Network Trash Folder
|
||||||
|
Temporary Items
|
||||||
|
.apdisk
|
||||||
|
|
||||||
|
# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
|
||||||
|
# Windows thumbnail cache files
|
||||||
|
Thumbs.db
|
||||||
|
ehthumbs.db
|
||||||
|
ehthumbs_vista.db
|
||||||
|
|
||||||
|
# Dump file
|
||||||
|
*.stackdump
|
||||||
|
|
||||||
|
# Folder config file
|
||||||
|
[Dd]esktop.ini
|
||||||
|
|
||||||
|
# Recycle Bin used on file shares
|
||||||
|
$RECYCLE.BIN/
|
||||||
|
|
||||||
|
# Windows Installer files
|
||||||
|
*.cab
|
||||||
|
*.msi
|
||||||
|
*.msix
|
||||||
|
*.msm
|
||||||
|
*.msp
|
||||||
|
|
||||||
|
# Windows shortcuts
|
||||||
|
*.lnk
|
||||||
|
|
||||||
|
# JetBrains Rider
|
||||||
|
.idea/
|
||||||
|
*.sln.iml
|
||||||
|
|
||||||
|
##
|
||||||
|
## Visual Studio Code
|
||||||
|
##
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/settings.json
|
||||||
|
!.vscode/tasks.json
|
||||||
|
!.vscode/launch.json
|
||||||
|
!.vscode/extensions.json
|
|
@ -0,0 +1,22 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapTo", "MapTo\MapTo.csproj", "{4DB371AC-48D0-4F01-8EF3-7707D06EF0A7}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapToTests", "MapToTests\MapToTests.csproj", "{797DA57B-AC7E-468B-8799-44C5A574C0E3}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{4DB371AC-48D0-4F01-8EF3-7707D06EF0A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{4DB371AC-48D0-4F01-8EF3-7707D06EF0A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{4DB371AC-48D0-4F01-8EF3-7707D06EF0A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{4DB371AC-48D0-4F01-8EF3-7707D06EF0A7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
|
@ -0,0 +1,3 @@
|
||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mapto/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Usings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
|
@ -0,0 +1,18 @@
|
||||||
|
// ReSharper disable UnusedType.Global
|
||||||
|
// ReSharper disable CheckNamespace
|
||||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||||
|
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
namespace System.Runtime.CompilerServices
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Reserved to be used by the compiler for tracking metadata.
|
||||||
|
/// This class should not be used by developers in source code.
|
||||||
|
/// </summary>
|
||||||
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
|
internal static class IsExternalInit
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
|
||||||
|
namespace MapTo.Extensions
|
||||||
|
{
|
||||||
|
internal static class RoslynExtensions
|
||||||
|
{
|
||||||
|
public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis(this ITypeSymbol type)
|
||||||
|
{
|
||||||
|
var current = type;
|
||||||
|
while (current != null)
|
||||||
|
{
|
||||||
|
yield return current;
|
||||||
|
|
||||||
|
current = current.BaseType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<ISymbol> GetAllMembers(this ITypeSymbol type)
|
||||||
|
{
|
||||||
|
return type.GetBaseTypesAndThis().SelectMany(n => n.GetMembers());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<T> GetAllMembersOfType<T>(this ITypeSymbol type) where T : ISymbol
|
||||||
|
{
|
||||||
|
return type.GetAllMembers().OfType<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompilationUnitSyntax? GetCompilationUnit(this SyntaxNode syntaxNode)
|
||||||
|
{
|
||||||
|
return syntaxNode.Ancestors().OfType<CompilationUnitSyntax>().FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetClassName(this ClassDeclarationSyntax classSyntax)
|
||||||
|
{
|
||||||
|
return classSyntax.Identifier.Text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetClassModifier(this ClassDeclarationSyntax classSyntax)
|
||||||
|
{
|
||||||
|
return classSyntax.Modifiers.ToFullString().Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool HaveAttribute(this ClassDeclarationSyntax classSyntax, string attributeName)
|
||||||
|
{
|
||||||
|
return classSyntax.AttributeLists.Count > 0 &&
|
||||||
|
classSyntax.AttributeLists.SelectMany(al => al.Attributes
|
||||||
|
.Where(a =>
|
||||||
|
(a.Name as IdentifierNameSyntax)?.Identifier.Text == attributeName ||
|
||||||
|
((a.Name as QualifiedNameSyntax)?.Right as IdentifierNameSyntax)?.Identifier.ValueText == attributeName))
|
||||||
|
.Any();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AttributeSyntax? GetAttribute(this ClassDeclarationSyntax classSyntax, string attributeName)
|
||||||
|
{
|
||||||
|
return classSyntax.AttributeLists
|
||||||
|
.SelectMany(al => al.Attributes)
|
||||||
|
.SingleOrDefault(a =>
|
||||||
|
(a.Name as IdentifierNameSyntax)?.Identifier.ValueText == attributeName ||
|
||||||
|
((a.Name as QualifiedNameSyntax)?.Right as IdentifierNameSyntax)?.Identifier.ValueText == attributeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string? GetNamespace(this CompilationUnitSyntax root)
|
||||||
|
{
|
||||||
|
return root.ChildNodes()
|
||||||
|
.OfType<NamespaceDeclarationSyntax>()
|
||||||
|
.FirstOrDefault()
|
||||||
|
?.Name
|
||||||
|
.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<string> GetUsings(this CompilationUnitSyntax root)
|
||||||
|
{
|
||||||
|
return root.ChildNodes()
|
||||||
|
.OfType<UsingDirectiveSyntax>()
|
||||||
|
.Select(n => n.Name.ToString())
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
namespace MapTo.Extensions
|
||||||
|
{
|
||||||
|
internal static class StringExtensions
|
||||||
|
{
|
||||||
|
public static string ToCamelCase(this string value)
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(value) ? value : $"{char.ToLower(value[0])}{value.Substring(1)}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using MapTo.Extensions;
|
||||||
|
using MapTo.Models;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
|
||||||
|
namespace MapTo
|
||||||
|
{
|
||||||
|
public class MapToGenerator : ISourceGenerator
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Initialize(GeneratorInitializationContext context)
|
||||||
|
{
|
||||||
|
context.RegisterForSyntaxNotifications(() => new MapToSyntaxReceiver());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Execute(GeneratorExecutionContext context)
|
||||||
|
{
|
||||||
|
context.AddMapToAttribute();
|
||||||
|
|
||||||
|
if (!(context.SyntaxReceiver is MapToSyntaxReceiver receiver) || !receiver.CandidateClasses.Any())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (classDeclarationSyntax, attributeSyntax) in receiver.CandidateClasses)
|
||||||
|
{
|
||||||
|
var model = GetModel(context.Compilation, classDeclarationSyntax);
|
||||||
|
if (model is null)
|
||||||
|
{
|
||||||
|
// TODO: Emit diagnostic info.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var (source, hintName) = SourceProvider.GenerateSource(model);
|
||||||
|
|
||||||
|
context.AddSource(hintName, source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MapModel? GetModel(Compilation compilation, ClassDeclarationSyntax classSyntax)
|
||||||
|
{
|
||||||
|
var root = classSyntax.GetCompilationUnit();
|
||||||
|
if (root is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var classSemanticModel = compilation.GetSemanticModel(classSyntax.SyntaxTree);
|
||||||
|
|
||||||
|
if (!(classSemanticModel.GetDeclaredSymbol(classSyntax) is INamedTypeSymbol classSymbol))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var destinationTypeSymbol = GetDestinationTypeSymbol(classSyntax, classSemanticModel);
|
||||||
|
if (destinationTypeSymbol is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new MapModel(
|
||||||
|
ns: root.GetNamespace(),
|
||||||
|
classModifiers: classSyntax.GetClassModifier(),
|
||||||
|
className: classSyntax.GetClassName(),
|
||||||
|
properties: classSymbol.GetAllMembersOfType<IPropertySymbol>(),
|
||||||
|
destinationClassName: destinationTypeSymbol.Name,
|
||||||
|
destinationTypeProperties: destinationTypeSymbol.GetAllMembersOfType<IPropertySymbol>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ITypeSymbol? GetDestinationTypeSymbol(ClassDeclarationSyntax classSyntax, SemanticModel model)
|
||||||
|
{
|
||||||
|
var destinationTypeExpressionSyntax = classSyntax
|
||||||
|
.GetAttribute(SourceProvider.MapFromAttributeName)
|
||||||
|
?.DescendantNodes()
|
||||||
|
.OfType<TypeOfExpressionSyntax>()
|
||||||
|
.SingleOrDefault();
|
||||||
|
|
||||||
|
return destinationTypeExpressionSyntax is not null ? model.GetTypeInfo(destinationTypeExpressionSyntax.Type).Type : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
|
||||||
|
namespace MapTo
|
||||||
|
{
|
||||||
|
internal class MapToSyntaxReceiver : ISyntaxReceiver
|
||||||
|
{
|
||||||
|
public List<(ClassDeclarationSyntax classDeclarationSyntax, AttributeSyntax attributeSyntax)> CandidateClasses { get; } = new();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||||
|
{
|
||||||
|
if (syntaxNode is not ClassDeclarationSyntax { AttributeLists: { Count: >= 1 } attributes } classDeclaration)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var attributeSyntax = attributes
|
||||||
|
.SelectMany(a => a.Attributes)
|
||||||
|
.SingleOrDefault(a => a.Name is
|
||||||
|
IdentifierNameSyntax { Identifier: { ValueText: SourceProvider.MapFromAttributeName } } // For: [MapFrom]
|
||||||
|
or
|
||||||
|
QualifiedNameSyntax // For: [MapTo.MapFrom]
|
||||||
|
{
|
||||||
|
Left: IdentifierNameSyntax { Identifier: { ValueText: SourceProvider.NamespaceName } },
|
||||||
|
Right: IdentifierNameSyntax { Identifier: { ValueText: SourceProvider.MapFromAttributeName } }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (attributeSyntax is not null)
|
||||||
|
{
|
||||||
|
CandidateClasses.Add((classDeclaration, attributeSyntax));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<LangVersion>9</LangVersion>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,33 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace MapTo.Models
|
||||||
|
{
|
||||||
|
public class MapModel
|
||||||
|
{
|
||||||
|
public MapModel(string? ns, string classModifiers, string className, IEnumerable<IPropertySymbol> properties, string destinationClassName, IEnumerable<IPropertySymbol> destinationTypeProperties)
|
||||||
|
{
|
||||||
|
Namespace = ns;
|
||||||
|
ClassModifiers = classModifiers;
|
||||||
|
ClassName = className;
|
||||||
|
Properties = properties;
|
||||||
|
DestinationClassName = destinationClassName;
|
||||||
|
DestinationTypeProperties = destinationTypeProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string? Namespace { get; }
|
||||||
|
|
||||||
|
public string ClassModifiers { get; }
|
||||||
|
|
||||||
|
public string ClassName { get; }
|
||||||
|
|
||||||
|
public IEnumerable<IPropertySymbol> Properties { get; }
|
||||||
|
|
||||||
|
public string DestinationClassName { get; }
|
||||||
|
|
||||||
|
public IEnumerable<IPropertySymbol> DestinationTypeProperties { get; }
|
||||||
|
|
||||||
|
public bool IsEmpty => !Properties.Any() || !DestinationTypeProperties.Any();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using MapTo.Extensions;
|
||||||
|
using MapTo.Models;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
using Microsoft.CodeAnalysis.Text;
|
||||||
|
|
||||||
|
namespace MapTo
|
||||||
|
{
|
||||||
|
internal static class SourceProvider
|
||||||
|
{
|
||||||
|
internal const string NamespaceName = "MapTo";
|
||||||
|
internal const string MapFromAttributeName = "MapFrom";
|
||||||
|
private const int Indent1 = 4; //" ";
|
||||||
|
private const int Indent2 = Indent1 * 2; // " ";
|
||||||
|
private const int Indent3 = Indent1 * 3; // " ";
|
||||||
|
|
||||||
|
internal static void AddMapToAttribute(this GeneratorExecutionContext context)
|
||||||
|
{
|
||||||
|
const string source = @"
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MapTo
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class MapFromAttribute : Attribute
|
||||||
|
{
|
||||||
|
public MapFromAttribute(Type sourceType)
|
||||||
|
{
|
||||||
|
SourceType = sourceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type SourceType { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
context.AddSource("MapFromAttribute", SourceText.From(source, Encoding.UTF8));
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static (string source, string hintName) GenerateSource(MapModel model)
|
||||||
|
{
|
||||||
|
var builder = new StringBuilder();
|
||||||
|
|
||||||
|
// Namespace declaration
|
||||||
|
builder
|
||||||
|
.AppendFileHeader()
|
||||||
|
.AppendFormat("namespace {0}", model.Namespace)
|
||||||
|
.AppendOpeningBracket();
|
||||||
|
|
||||||
|
// Class declaration
|
||||||
|
builder
|
||||||
|
.PadLeft(Indent1)
|
||||||
|
.AppendFormat("{0} class {1}", model.ClassModifiers, model.ClassName)
|
||||||
|
.AppendOpeningBracket(Indent1);
|
||||||
|
|
||||||
|
// Constructor declaration
|
||||||
|
builder.GenerateConstructor(model, out var mappedProperties);
|
||||||
|
|
||||||
|
// End class declaration
|
||||||
|
builder.AppendClosingBracket(Indent1);
|
||||||
|
|
||||||
|
// End namespace declaration
|
||||||
|
builder.AppendClosingBracket();
|
||||||
|
|
||||||
|
return (builder.ToString(), $"{model.ClassName}.cs");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model, out List<IPropertySymbol> mappedProperties)
|
||||||
|
{
|
||||||
|
var destinationClassParameterName = model.DestinationClassName.ToCamelCase();
|
||||||
|
|
||||||
|
builder
|
||||||
|
.PadLeft(Indent2)
|
||||||
|
.AppendFormat("public {0}({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName)
|
||||||
|
.AppendOpeningBracket(Indent2);
|
||||||
|
|
||||||
|
mappedProperties = new List<IPropertySymbol>();
|
||||||
|
foreach (var propertySymbol in model.DestinationTypeProperties)
|
||||||
|
{
|
||||||
|
if (model.Properties.Any(p => p.Name == propertySymbol.Name))
|
||||||
|
{
|
||||||
|
mappedProperties.Add(propertySymbol);
|
||||||
|
builder
|
||||||
|
.PadLeft(Indent3)
|
||||||
|
.AppendFormat("{0} = {1}.{2};{3}", propertySymbol.Name, destinationClassParameterName, propertySymbol.Name, Environment.NewLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// End constructor declaration
|
||||||
|
return builder.AppendClosingBracket(Indent2, padNewLine: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StringBuilder AppendOpeningBracket(this StringBuilder builder, int indent = 0)
|
||||||
|
{
|
||||||
|
return builder.AppendLine().PadLeft(indent).AppendFormat("{{{0}", Environment.NewLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StringBuilder AppendClosingBracket(this StringBuilder builder, int indent = 0, bool padNewLine = true)
|
||||||
|
{
|
||||||
|
if (padNewLine)
|
||||||
|
{
|
||||||
|
builder.AppendLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.PadLeft(indent).Append("}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StringBuilder PadLeft(this StringBuilder builder, int width)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < width; i++)
|
||||||
|
{
|
||||||
|
builder.Append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StringBuilder AppendFileHeader(this StringBuilder builder)
|
||||||
|
{
|
||||||
|
return builder
|
||||||
|
.AppendLine("// <auto-generated />")
|
||||||
|
.AppendLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetValueText(this NameSyntax? nameSyntax)
|
||||||
|
{
|
||||||
|
return (nameSyntax as IdentifierNameSyntax)?.Identifier.ValueText ?? string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using MapTo;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace MapToTests
|
||||||
|
{
|
||||||
|
internal static class CSharpGenerator
|
||||||
|
{
|
||||||
|
internal static string GetGeneratedOutput(this ITestOutputHelper outputHelper, string source)
|
||||||
|
{
|
||||||
|
var (compilation, diagnostics) = GetOutputCompilation(source);
|
||||||
|
diagnostics.ShouldBeSuccessful();
|
||||||
|
|
||||||
|
var generatedOutput = compilation.SyntaxTrees.Last().ToString();
|
||||||
|
outputHelper.WriteLine(generatedOutput);
|
||||||
|
|
||||||
|
return generatedOutput;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void ShouldBeSuccessful(this ImmutableArray<Diagnostic> diagnostics)
|
||||||
|
{
|
||||||
|
Assert.False(diagnostics.Any(d => d.Severity >= DiagnosticSeverity.Warning), $"Failed: {diagnostics.FirstOrDefault()?.GetMessage()}");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(string source)
|
||||||
|
{
|
||||||
|
var syntaxTree = CSharpSyntaxTree.ParseText(source);
|
||||||
|
var references = AppDomain.CurrentDomain.GetAssemblies()
|
||||||
|
.Where(a => !a.IsDynamic && !string.IsNullOrWhiteSpace(a.Location))
|
||||||
|
.Select(a => MetadataReference.CreateFromFile(a.Location))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var compilation = CSharpCompilation.Create("foo", new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
|
||||||
|
|
||||||
|
// NB: Uncomment this line if you want to fail tests when the injected program isn't valid _before_ running generators
|
||||||
|
// var compileDiagnostics = compilation.GetDiagnostics();
|
||||||
|
// Assert.False(compileDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), "Failed: " + compileDiagnostics.FirstOrDefault()?.GetMessage());
|
||||||
|
|
||||||
|
ISourceGenerator generator = new MapToGenerator();
|
||||||
|
var driver = CSharpGeneratorDriver.Create(generator);
|
||||||
|
driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics);
|
||||||
|
|
||||||
|
return (outputCompilation, generateDiagnostics);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||||
|
<PackageReference Include="Shouldly" Version="4.0.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="1.3.0">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MapTo\MapTo.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,200 @@
|
||||||
|
using System.Linq;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace MapToTests
|
||||||
|
{
|
||||||
|
public class Tests
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
private const string ExpectedAttribute = @"
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MapTo
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class MapFromAttribute : Attribute
|
||||||
|
{
|
||||||
|
public MapFromAttribute(Type sourceType)
|
||||||
|
{
|
||||||
|
SourceType = sourceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type SourceType { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
public Tests(ITestOutputHelper output)
|
||||||
|
{
|
||||||
|
_output = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void VerifyMapToAttribute()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string source = "";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
diagnostics.ShouldBeSuccessful();
|
||||||
|
compilation.SyntaxTrees.ShouldContain(c => c.ToString() == ExpectedAttribute);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void When_NoMapToAttributeFound_Should_GenerateOnlyTheAttribute()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string source = "";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
diagnostics.ShouldBeSuccessful();
|
||||||
|
compilation.SyntaxTrees.ShouldContain(s => s.ToString() == ExpectedAttribute);
|
||||||
|
compilation.SyntaxTrees.Select(s => s.ToString()).Where(s => s != string.Empty && s != ExpectedAttribute).ShouldBeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void When_MapToAttributeFound_Should_GenerateTheClass()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string source = @"
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
[MapFrom(typeof(Baz)]
|
||||||
|
public partial class Foo
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Baz
|
||||||
|
{
|
||||||
|
public int Prop1 { get; set; }
|
||||||
|
public int Prop2 { get; }
|
||||||
|
public int Prop3 { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
const string expectedResult = @"
|
||||||
|
// <auto-generated />
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
public partial class Foo
|
||||||
|
{
|
||||||
|
public Foo(Baz baz)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
diagnostics.ShouldBeSuccessful();
|
||||||
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||||||
|
compilation.SyntaxTrees.Last().ToString().ShouldBe(expectedResult.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void When_MapToAttributeWithNamespaceFound_Should_GenerateTheClass()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string source = @"
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
[MapTo.MapFrom(typeof(Baz)]
|
||||||
|
public partial class Foo
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Baz
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
const string expectedResult = @"
|
||||||
|
// <auto-generated />
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
public partial class Foo
|
||||||
|
{
|
||||||
|
public Foo(Baz baz)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
diagnostics.ShouldBeSuccessful();
|
||||||
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||||||
|
compilation.SyntaxTrees.Last().ToString().ShouldBe(expectedResult.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void When_SourceTypeHasMatchingProperties_Should_CreateConstructorAndAssignSrcToDest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var source = GetSourceText();
|
||||||
|
|
||||||
|
const string expectedResult = @"
|
||||||
|
public partial class Foo
|
||||||
|
{
|
||||||
|
public Foo(Baz baz)
|
||||||
|
{
|
||||||
|
Prop1 = baz.Prop1;
|
||||||
|
Prop2 = baz.Prop2;
|
||||||
|
Prop3 = baz.Prop3;
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
diagnostics.ShouldBeSuccessful();
|
||||||
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||||||
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetSourceText(bool includeAttributeNamespace = false)
|
||||||
|
{
|
||||||
|
return @$"
|
||||||
|
namespace Test
|
||||||
|
{{
|
||||||
|
{(includeAttributeNamespace ? "[MapTo.MapFrom(typeof(Baz))]" : "[MapFrom(typeof(Baz))]")}
|
||||||
|
public partial class Foo
|
||||||
|
{{
|
||||||
|
public int Prop1 {{ get; set; }}
|
||||||
|
public int Prop2 {{ get; }}
|
||||||
|
public int Prop3 {{ get; }}
|
||||||
|
}}
|
||||||
|
|
||||||
|
public class Baz
|
||||||
|
{{
|
||||||
|
public int Prop1 {{ get; set; }}
|
||||||
|
public int Prop2 {{ get; }}
|
||||||
|
public int Prop3 {{ get; set; }}
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue