Skip to content

Commit 88abefa

Browse files
committed
Add mapper
1 parent 49c9418 commit 88abefa

File tree

4 files changed

+208
-9
lines changed

4 files changed

+208
-9
lines changed

src/.editorconfig

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,15 +728,204 @@ dotnet_diagnostic.CS8847.severity = error
728728
#### performance warnings ####
729729
# https://learn.microsoft.com/zh-cn/dotnet/fundamentals/code-analysis/quality-rules/performance-warnings
730730

731+
732+
# CA1848: Use the LoggerMessage delegates
733+
dotnet_diagnostic.CA1848.severity = error
734+
735+
# CA1802: Use Literals Where Appropriate
736+
dotnet_diagnostic.CA1802.severity = error
737+
738+
# CA1805: Do not initialize unnecessarily
739+
dotnet_diagnostic.CA1805.severity = error
740+
741+
# CA1806: Do not ignore method results
742+
dotnet_diagnostic.CA1806.severity = error
743+
744+
# CA1810: Initialize reference type static fields inline
745+
dotnet_diagnostic.CA1810.severity = error
746+
747+
# CA1812: Avoid uninstantiated internal classes
748+
dotnet_diagnostic.CA1812.severity = error
749+
750+
# CA1813: Avoid unsealed attributes
751+
dotnet_diagnostic.CA1813.severity = error
752+
753+
# CA1814: Prefer jagged arrays over multidimensional
754+
dotnet_diagnostic.CA1814.severity = error
755+
731756
# CA1815:重写值类型上的 Equals 和相等运算符 改为 none 防止 cleanup 自动,使用 S3898 警告
732757
dotnet_diagnostic.CA1815.severity = none
733758

759+
# CA1819: Properties should not return arrays
760+
dotnet_diagnostic.CA1819.severity = error
761+
762+
# CA1820: Test for empty strings using string length
763+
dotnet_diagnostic.CA1820.severity = error
764+
765+
# CA1821: Remove empty finalizers
766+
dotnet_diagnostic.CA1821.severity = error
767+
734768
# CA1822: Mark members as static 改为 none 防止 cleanup 自动 static,使用 S2325 警告
735769
dotnet_diagnostic.CA1822.severity = none
736770

771+
# CA1823: Avoid unused private fields
772+
dotnet_diagnostic.CA1823.severity = error
773+
774+
# CA1824: Mark assemblies with NeutralResourcesLanguageAttribute
775+
dotnet_diagnostic.CA1824.severity = error
776+
777+
# CA1825: Avoid zero-length array allocations
778+
dotnet_diagnostic.CA1825.severity = error
779+
780+
# CA1826: Use property instead of Linq Enumerable method
781+
dotnet_diagnostic.CA1826.severity = error
782+
783+
# CA1827: Do not use Count/LongCount when Any can be used
784+
dotnet_diagnostic.CA1827.severity = error
785+
786+
# CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used
787+
dotnet_diagnostic.CA1828.severity = error
788+
789+
# CA1829: Use Length/Count property instead of Enumerable.Count method
790+
dotnet_diagnostic.CA1829.severity = error
791+
792+
# CA1830: Prefer strongly-typed Append and Insert method overloads on StringBuilder
793+
dotnet_diagnostic.CA1830.severity = error
794+
795+
# CA1831: Use AsSpan instead of Range-based indexers for string when appropriate
796+
dotnet_diagnostic.CA1831.severity = error
797+
798+
# CA1832: Use AsSpan or AsMemory instead of Range-based indexers for getting ReadOnlySpan or ReadOnlyMemory portion of an array
799+
dotnet_diagnostic.CA1832.severity = error
800+
801+
# CA1833: Use AsSpan or AsMemory instead of Range-based indexers for getting Span or Memory portion of an array
802+
dotnet_diagnostic.CA1833.severity = error
803+
804+
# CA1834: Use StringBuilder.Append(char) for single character strings
805+
dotnet_diagnostic.CA1834.severity = error
806+
807+
# CA1835: Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync'
808+
dotnet_diagnostic.CA1835.severity = error
809+
810+
# CA1836: Prefer IsEmpty over Count when available
811+
dotnet_diagnostic.CA1836.severity = error
812+
813+
# CA1837: Use Environment.ProcessId instead of Process.GetCurrentProcess().Id
814+
dotnet_diagnostic.CA1837.severity = error
815+
816+
# CA1838: Avoid StringBuilder parameters for P/Invokes
817+
dotnet_diagnostic.CA1838.severity = error
818+
819+
# CA1839: Use Environment.ProcessPath instead of Process.GetCurrentProcess().MainModule.FileName
820+
dotnet_diagnostic.CA1839.severity = error
821+
822+
# CA1840: Use Environment.CurrentManagedThreadId instead of Thread.CurrentThread.ManagedThreadId
823+
dotnet_diagnostic.CA1840.severity = error
824+
825+
# CA1841: Prefer Dictionary Contains methods
826+
dotnet_diagnostic.CA1841.severity = error
827+
828+
# CA1842: Do not use 'WhenAll' with a single task
829+
dotnet_diagnostic.CA1842.severity = error
830+
831+
# CA1843: Do not use 'WaitAll' with a single task
832+
dotnet_diagnostic.CA1843.severity = error
833+
834+
# CA1844: Provide memory-based overrides of async methods when subclassing 'Stream'
835+
dotnet_diagnostic.CA1844.severity = error
836+
837+
# CA1845: Use span-based 'string.Concat'
838+
dotnet_diagnostic.CA1845.severity = error
839+
840+
# CA1846: Prefer AsSpan over Substring
841+
dotnet_diagnostic.CA1846.severity = error
842+
843+
# CA1847: Use char literal for a single character lookup
844+
dotnet_diagnostic.CA1847.severity = error
845+
737846
# CA1848: Use the LoggerMessage delegates
738847
dotnet_diagnostic.CA1848.severity = warning
739848

849+
# CA1849: Call async methods when in an async method
850+
dotnet_diagnostic.CA1849.severity = error
851+
852+
# CA1850: Prefer static HashData method over ComputeHash
853+
dotnet_diagnostic.CA1850.severity = error
854+
855+
# CA1851: Possible multiple enumerations of IEnumerable collection
856+
dotnet_diagnostic.CA1851.severity = error
857+
858+
# CA1852: Seal internal types
859+
dotnet_diagnostic.CA1852.severity = error
860+
861+
# CA1853: Unnecessary call to 'Dictionary.ContainsKey(key)'
862+
dotnet_diagnostic.CA1853.severity = error
863+
864+
# CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method
865+
dotnet_diagnostic.CA1854.severity = error
866+
867+
# CA1855: Use Span.Clear() instead of Span.Fill()
868+
dotnet_diagnostic.CA1855.severity = error
869+
870+
# CA1856: Incorrect usage of ConstantExpected attribute
871+
dotnet_diagnostic.CA1856.severity = error
872+
873+
# CA1857: The parameter expects a constant for optimal performance
874+
dotnet_diagnostic.CA1857.severity = error
875+
876+
# CA1858: Use StartsWith instead of IndexOf
877+
dotnet_diagnostic.CA1858.severity = error
878+
879+
# CA1859: Use concrete types when possible for improved performance
880+
dotnet_diagnostic.CA1859.severity = error
881+
882+
# CA1860: Avoid using 'Enumerable.Any()' extension method
883+
dotnet_diagnostic.CA1860.severity = error
884+
885+
# CA1861: Avoid constant arrays as arguments
886+
dotnet_diagnostic.CA1861.severity = error
887+
888+
# CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
889+
dotnet_diagnostic.CA1862.severity = error
890+
891+
# CA1863: Use 'CompositeFormat'
892+
dotnet_diagnostic.CA1863.severity = error
893+
894+
# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
895+
dotnet_diagnostic.CA1864.severity = error
896+
897+
# CA1865-CA1867: Use char overload
898+
dotnet_diagnostic.CA1865.severity = error
899+
dotnet_diagnostic.CA1866.severity = error
900+
dotnet_diagnostic.CA1867.severity = error
901+
902+
# CA1868: Unnecessary call to 'Contains' for sets
903+
dotnet_diagnostic.CA1868.severity = error
904+
905+
# CA1869: Cache and reuse 'JsonSerializerOptions' instances
906+
dotnet_diagnostic.CA1869.severity = error
907+
908+
# CA1870: Use a cached 'SearchValues' instance
909+
dotnet_diagnostic.CA1870.severity = error
910+
911+
# CA1871: Do not pass a nullable struct to 'ArgumentNullException.ThrowIfNull'
912+
dotnet_diagnostic.CA1871.severity = error
913+
914+
# CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'
915+
dotnet_diagnostic.CA1872.severity = error
916+
917+
# CA1873: Avoid potentially expensive logging
918+
dotnet_diagnostic.CA1873.severity = error
919+
920+
# CA1874: Use 'Regex.IsMatch'
921+
dotnet_diagnostic.CA1874.severity = error
922+
923+
# CA1875: Use 'Regex.Count'
924+
dotnet_diagnostic.CA1875.severity = error
925+
926+
# CA1877: Use 'Path.Combine' or 'Path.Join' overloads
927+
dotnet_diagnostic.CA1877.severity = error
928+
740929
#### compile warning ####
741930

742931
# CS0162: 检测到无法访问的代码

src/plugins/MapperlyTest/CarMapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Car
1717

1818
}
1919

20-
internal record CarDto(string Name, int NumberOfWheels)
20+
internal sealed record CarDto(string Name, int NumberOfWheels)
2121
{
2222

2323
#region Properties
@@ -33,27 +33,27 @@ internal record CarDto(string Name, int NumberOfWheels)
3333
}
3434

3535
[Mapper]
36-
internal partial class CarMapper
36+
internal sealed partial class CarMapper
3737
{
3838

3939
#region Methods
4040

4141
[MapperIgnoreSource(nameof(Car.NullStr))]
4242
[MapperIgnoreTarget(nameof(CarDto.IntNull))]
43-
internal partial CarDto CarToCarDto(Car car);
43+
internal partial CarDto ToCarDto(Car car);
4444

4545
#endregion
4646

4747
}
4848

4949
[Mapper]
50-
internal partial class CarNullThrowMapper
50+
internal sealed partial class CarNullThrowMapper
5151
{
5252

5353
#region Methods
5454

5555
[MapProperty(nameof(Car.NullStr), nameof(CarDto.StringNull))]
56-
internal partial CarDto CarToCarDto(Car car);
56+
internal partial CarDto ToCarDto(Car car);
5757

5858
#endregion
5959

src/plugins/MapperlyTest/MapperTest.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ public static class MapperTest
55

66
#region Constants & Statics
77

8-
public static void Test()
8+
public static void CarMapper_Test()
99
{
1010
var mapper = new CarMapper();
11+
12+
var car = new Car { Name = "Fiat", NumberOfWheels = 4 };
13+
var dto = mapper.ToCarDto(car);
14+
Console.WriteLine($"Car: {dto.Name}, Wheels: {dto.NumberOfWheels}");
15+
}
16+
17+
public static void CarNullThrowMapper_Test()
18+
{
19+
var mapper = new CarNullThrowMapper();
20+
1121
var car = new Car { Name = "Fiat", NumberOfWheels = 4 };
12-
var dto = mapper.CarToCarDto(car);
22+
var dto = mapper.ToCarDto(car);
1323
Console.WriteLine($"Car: {dto.Name}, Wheels: {dto.NumberOfWheels}");
1424
}
1525

src/plugins/MapperlyTest/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
namespace MapperlyTest;
22

3-
internal class Program
3+
internal static class Program
44
{
55

66
#region Constants & Statics
77

88
private static void Main(string[] args)
99
{
10-
MapperTest.Test();
10+
MapperTest.CarMapper_Test();
1111
}
1212

1313
#endregion

0 commit comments

Comments
 (0)