Skip to content

Commit 11122cb

Browse files
committed
formatted
1 parent 54005a1 commit 11122cb

21 files changed

+420
-287
lines changed

examples/Linqraft.Benchmark/AutoMapperConfig.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,45 @@ public AutoMapperProfile()
1313
{
1414
// Map SampleClass to ManualSampleClassDto
1515
CreateMap<SampleClass, ManualSampleClassDto>()
16-
.ForMember(dest => dest.Child2Id, opt => opt.MapFrom(src => src.Child2 != null ? src.Child2.Id : (int?)null))
17-
.ForMember(dest => dest.Child2Quux, opt => opt.MapFrom(src => src.Child2 != null ? src.Child2.Quux : null))
16+
.ForMember(
17+
dest => dest.Child2Id,
18+
opt => opt.MapFrom(src => src.Child2 != null ? src.Child2.Id : (int?)null)
19+
)
20+
.ForMember(
21+
dest => dest.Child2Quux,
22+
opt => opt.MapFrom(src => src.Child2 != null ? src.Child2.Quux : null)
23+
)
1824
.ForMember(dest => dest.Child3Id, opt => opt.MapFrom(src => src.Child3.Id))
1925
.ForMember(dest => dest.Child3Corge, opt => opt.MapFrom(src => src.Child3.Corge))
20-
.ForMember(dest => dest.Child3ChildId, opt => opt.MapFrom(src => src.Child3 != null && src.Child3.Child != null ? src.Child3.Child.Id : (int?)null))
21-
.ForMember(dest => dest.Child3ChildGrault, opt => opt.MapFrom(src => src.Child3 != null && src.Child3.Child != null ? src.Child3.Child.Grault : null));
26+
.ForMember(
27+
dest => dest.Child3ChildId,
28+
opt =>
29+
opt.MapFrom(src =>
30+
src.Child3 != null && src.Child3.Child != null
31+
? src.Child3.Child.Id
32+
: (int?)null
33+
)
34+
)
35+
.ForMember(
36+
dest => dest.Child3ChildGrault,
37+
opt =>
38+
opt.MapFrom(src =>
39+
src.Child3 != null && src.Child3.Child != null
40+
? src.Child3.Child.Grault
41+
: null
42+
)
43+
);
2244

2345
// Map SampleChildClass to ManualSampleChildDto
2446
CreateMap<SampleChildClass, ManualSampleChildDto>()
25-
.ForMember(dest => dest.ChildId, opt => opt.MapFrom(src => src.Child != null ? src.Child.Id : (int?)null))
26-
.ForMember(dest => dest.ChildQux, opt => opt.MapFrom(src => src.Child != null ? src.Child.Qux : null));
47+
.ForMember(
48+
dest => dest.ChildId,
49+
opt => opt.MapFrom(src => src.Child != null ? src.Child.Id : (int?)null)
50+
)
51+
.ForMember(
52+
dest => dest.ChildQux,
53+
opt => opt.MapFrom(src => src.Child != null ? src.Child.Qux : null)
54+
);
2755
}
2856
}
2957

examples/Linqraft.Benchmark/FacetDtos.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,53 @@ namespace Linqraft.Benchmark;
55
/// <summary>
66
/// Facet-generated DTO for SampleChildChildClass (grandchild).
77
/// </summary>
8-
[Facet(typeof(SampleChildChildClass),
9-
exclude: ["SampleChildClassId", "SampleChildClass"])]
8+
[Facet(typeof(SampleChildChildClass), exclude: ["SampleChildClassId", "SampleChildClass"])]
109
public partial record FacetSampleChildChildDto;
1110

1211
/// <summary>
1312
/// Facet-generated DTO for SampleChildClass.
1413
/// Maps the child entity with nested grandchild.
1514
/// </summary>
16-
[Facet(typeof(SampleChildClass),
15+
[Facet(
16+
typeof(SampleChildClass),
1717
exclude: ["SampleClassId", "SampleClass"],
18-
NestedFacets = [typeof(FacetSampleChildChildDto)])]
18+
NestedFacets = [typeof(FacetSampleChildChildDto)]
19+
)]
1920
public partial record FacetSampleChildDto;
2021

2122
/// <summary>
2223
/// Facet-generated DTO for SampleChildClass2 (optional second child).
2324
/// </summary>
24-
[Facet(typeof(SampleChildClass2),
25-
exclude: ["SampleClassId", "SampleClass"])]
25+
[Facet(typeof(SampleChildClass2), exclude: ["SampleClassId", "SampleClass"])]
2626
public partial record FacetSampleChildClass2Dto;
2727

2828
/// <summary>
2929
/// Facet-generated DTO for SampleChildChildClass2 (grandchild of Child3).
3030
/// </summary>
31-
[Facet(typeof(SampleChildChildClass2),
32-
exclude: ["SampleChildClass3Id", "SampleChildClass3"])]
31+
[Facet(typeof(SampleChildChildClass2), exclude: ["SampleChildClass3Id", "SampleChildClass3"])]
3332
public partial record FacetSampleChildChildClass2Dto;
3433

3534
/// <summary>
3635
/// Facet-generated DTO for SampleChildClass3 (third child).
3736
/// </summary>
38-
[Facet(typeof(SampleChildClass3),
37+
[Facet(
38+
typeof(SampleChildClass3),
3939
exclude: ["SampleClassId", "SampleClass"],
40-
NestedFacets = [typeof(FacetSampleChildChildClass2Dto)])]
40+
NestedFacets = [typeof(FacetSampleChildChildClass2Dto)]
41+
)]
4142
public partial record FacetSampleChildClass3Dto;
4243

4344
/// <summary>
4445
/// Facet-generated DTO for SampleClass.
4546
/// The Facet source generator creates the mapping logic at compile time.
4647
/// Uses standard Facet NestedFacets for nested object mapping.
4748
/// </summary>
48-
[Facet(typeof(SampleClass),
49+
[Facet(
50+
typeof(SampleClass),
4951
NestedFacets = [
5052
typeof(FacetSampleChildDto),
5153
typeof(FacetSampleChildClass2Dto),
52-
typeof(FacetSampleChildClass3Dto)
53-
])]
54+
typeof(FacetSampleChildClass3Dto),
55+
]
56+
)]
5457
public partial record FacetSampleClassDto;

examples/Linqraft.Benchmark/MapperlyMapper.cs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,60 @@ public static partial class MapperlyMapper
1313
/// Projects IQueryable of SampleClass to ManualSampleClassDto.
1414
/// Mapperly generates an expression tree for efficient database projection.
1515
/// </summary>
16-
public static partial IQueryable<ManualSampleClassDto> ProjectToDto(this IQueryable<SampleClass> query);
16+
public static partial IQueryable<ManualSampleClassDto> ProjectToDto(
17+
this IQueryable<SampleClass> query
18+
);
1719

1820
/// <summary>
1921
/// Maps a single SampleClass to ManualSampleClassDto.
2022
/// Used internally by the IQueryable projection.
2123
/// </summary>
22-
[MapProperty(nameof(SampleClass.Child2) + "." + nameof(SampleChildClass2.Id), nameof(ManualSampleClassDto.Child2Id))]
23-
[MapProperty(nameof(SampleClass.Child2) + "." + nameof(SampleChildClass2.Quux), nameof(ManualSampleClassDto.Child2Quux))]
24-
[MapProperty(nameof(SampleClass.Child3) + "." + nameof(SampleChildClass3.Id), nameof(ManualSampleClassDto.Child3Id))]
25-
[MapProperty(nameof(SampleClass.Child3) + "." + nameof(SampleChildClass3.Corge), nameof(ManualSampleClassDto.Child3Corge))]
26-
[MapProperty(nameof(SampleClass.Child3) + "." + nameof(SampleChildClass3.Child) + "." + nameof(SampleChildChildClass2.Id), nameof(ManualSampleClassDto.Child3ChildId))]
27-
[MapProperty(nameof(SampleClass.Child3) + "." + nameof(SampleChildClass3.Child) + "." + nameof(SampleChildChildClass2.Grault), nameof(ManualSampleClassDto.Child3ChildGrault))]
24+
[MapProperty(
25+
nameof(SampleClass.Child2) + "." + nameof(SampleChildClass2.Id),
26+
nameof(ManualSampleClassDto.Child2Id)
27+
)]
28+
[MapProperty(
29+
nameof(SampleClass.Child2) + "." + nameof(SampleChildClass2.Quux),
30+
nameof(ManualSampleClassDto.Child2Quux)
31+
)]
32+
[MapProperty(
33+
nameof(SampleClass.Child3) + "." + nameof(SampleChildClass3.Id),
34+
nameof(ManualSampleClassDto.Child3Id)
35+
)]
36+
[MapProperty(
37+
nameof(SampleClass.Child3) + "." + nameof(SampleChildClass3.Corge),
38+
nameof(ManualSampleClassDto.Child3Corge)
39+
)]
40+
[MapProperty(
41+
nameof(SampleClass.Child3)
42+
+ "."
43+
+ nameof(SampleChildClass3.Child)
44+
+ "."
45+
+ nameof(SampleChildChildClass2.Id),
46+
nameof(ManualSampleClassDto.Child3ChildId)
47+
)]
48+
[MapProperty(
49+
nameof(SampleClass.Child3)
50+
+ "."
51+
+ nameof(SampleChildClass3.Child)
52+
+ "."
53+
+ nameof(SampleChildChildClass2.Grault),
54+
nameof(ManualSampleClassDto.Child3ChildGrault)
55+
)]
2856
private static partial ManualSampleClassDto MapSampleClass(SampleClass source);
2957

3058
/// <summary>
3159
/// Maps a single SampleChildClass to ManualSampleChildDto.
3260
/// </summary>
3361
[MapperIgnoreSource(nameof(SampleChildClass.SampleClassId))]
3462
[MapperIgnoreSource(nameof(SampleChildClass.SampleClass))]
35-
[MapProperty(nameof(SampleChildClass.Child) + "." + nameof(SampleChildChildClass.Id), nameof(ManualSampleChildDto.ChildId))]
36-
[MapProperty(nameof(SampleChildClass.Child) + "." + nameof(SampleChildChildClass.Qux), nameof(ManualSampleChildDto.ChildQux))]
63+
[MapProperty(
64+
nameof(SampleChildClass.Child) + "." + nameof(SampleChildChildClass.Id),
65+
nameof(ManualSampleChildDto.ChildId)
66+
)]
67+
[MapProperty(
68+
nameof(SampleChildClass.Child) + "." + nameof(SampleChildChildClass.Qux),
69+
nameof(ManualSampleChildDto.ChildQux)
70+
)]
3771
private static partial ManualSampleChildDto MapSampleChild(SampleChildClass source);
3872
}

examples/Linqraft.Benchmark/MapsterConfig.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,32 @@ public static class MapsterConfig
1616
/// </summary>
1717
public static void Configure()
1818
{
19-
if (_configured) return;
19+
if (_configured)
20+
return;
2021

2122
// Configure SampleClass to ManualSampleClassDto mapping
22-
TypeAdapterConfig<SampleClass, ManualSampleClassDto>.NewConfig()
23+
TypeAdapterConfig<SampleClass, ManualSampleClassDto>
24+
.NewConfig()
2325
.Map(dest => dest.Child2Id, src => src.Child2 != null ? src.Child2.Id : (int?)null)
2426
.Map(dest => dest.Child2Quux, src => src.Child2 != null ? src.Child2.Quux : null)
2527
.Map(dest => dest.Child3Id, src => src.Child3.Id)
2628
.Map(dest => dest.Child3Corge, src => src.Child3.Corge)
27-
.Map(dest => dest.Child3ChildId, src => src.Child3 != null && src.Child3.Child != null ? src.Child3.Child.Id : (int?)null)
28-
.Map(dest => dest.Child3ChildGrault, src => src.Child3 != null && src.Child3.Child != null ? src.Child3.Child.Grault : null);
29+
.Map(
30+
dest => dest.Child3ChildId,
31+
src =>
32+
src.Child3 != null && src.Child3.Child != null
33+
? src.Child3.Child.Id
34+
: (int?)null
35+
)
36+
.Map(
37+
dest => dest.Child3ChildGrault,
38+
src =>
39+
src.Child3 != null && src.Child3.Child != null ? src.Child3.Child.Grault : null
40+
);
2941

3042
// Configure SampleChildClass to ManualSampleChildDto mapping
31-
TypeAdapterConfig<SampleChildClass, ManualSampleChildDto>.NewConfig()
43+
TypeAdapterConfig<SampleChildClass, ManualSampleChildDto>
44+
.NewConfig()
3245
.Map(dest => dest.ChildId, src => src.Child != null ? src.Child.Id : (int?)null)
3346
.Map(dest => dest.ChildQux, src => src.Child != null ? src.Child.Qux : null);
3447

examples/Linqraft.Benchmark/SelectBenchmark.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ public async Task<int> AutoMapper_ProjectTo()
253253
[Benchmark(Description = "Mapperly Projection")]
254254
public async Task<int> Mapperly_Projection()
255255
{
256-
var results = await _dbContext
257-
.SampleClasses.ProjectToDto()
258-
.ToListAsync();
256+
var results = await _dbContext.SampleClasses.ProjectToDto().ToListAsync();
259257
return results.Count;
260258
}
261259

@@ -279,8 +277,10 @@ public async Task<int> Mapster_ProjectToType()
279277
[Benchmark(Description = "Facet ToFacetsAsync")]
280278
public async Task<int> Facet_ToFacetsAsync()
281279
{
282-
var results = await _dbContext
283-
.SampleClasses.ToFacetsAsync<SampleClass, FacetSampleClassDto>();
280+
var results = await _dbContext.SampleClasses.ToFacetsAsync<
281+
SampleClass,
282+
FacetSampleClassDto
283+
>();
284284
return results.Count;
285285
}
286286
}

src/Linqraft.Analyzer/AutoGeneratedDtoUsageAnalyzer.cs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,25 @@ public class AutoGeneratedDtoUsageAnalyzer : BaseLinqraftAnalyzer
3838
protected override void RegisterActions(AnalysisContext context)
3939
{
4040
// Check object creation expressions (new GeneratedDto(...))
41-
context.RegisterSyntaxNodeAction(AnalyzeObjectCreation, SyntaxKind.ObjectCreationExpression);
41+
context.RegisterSyntaxNodeAction(
42+
AnalyzeObjectCreation,
43+
SyntaxKind.ObjectCreationExpression
44+
);
4245

4346
// Check parameter declarations (void Foo(GeneratedDto dto))
4447
context.RegisterSyntaxNodeAction(AnalyzeParameter, SyntaxKind.Parameter);
4548

4649
// Check variable declarations (GeneratedDto dto = ...)
47-
context.RegisterSyntaxNodeAction(AnalyzeVariableDeclaration, SyntaxKind.VariableDeclaration);
50+
context.RegisterSyntaxNodeAction(
51+
AnalyzeVariableDeclaration,
52+
SyntaxKind.VariableDeclaration
53+
);
4854

4955
// Check property declarations (public GeneratedDto MyDto { get; set; })
50-
context.RegisterSyntaxNodeAction(AnalyzePropertyDeclaration, SyntaxKind.PropertyDeclaration);
56+
context.RegisterSyntaxNodeAction(
57+
AnalyzePropertyDeclaration,
58+
SyntaxKind.PropertyDeclaration
59+
);
5160

5261
// Check field declarations (private GeneratedDto _myDto;)
5362
context.RegisterSyntaxNodeAction(AnalyzeFieldDeclaration, SyntaxKind.FieldDeclaration);
@@ -102,7 +111,10 @@ private void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context)
102111
return;
103112
}
104113

105-
var typeInfo = context.SemanticModel.GetTypeInfo(variableDeclaration.Type, context.CancellationToken);
114+
var typeInfo = context.SemanticModel.GetTypeInfo(
115+
variableDeclaration.Type,
116+
context.CancellationToken
117+
);
106118
var type = typeInfo.Type;
107119

108120
if (type != null && HasAutoGeneratedDtoAttribute(type))
@@ -115,7 +127,10 @@ private void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
115127
{
116128
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;
117129

118-
var typeInfo = context.SemanticModel.GetTypeInfo(propertyDeclaration.Type, context.CancellationToken);
130+
var typeInfo = context.SemanticModel.GetTypeInfo(
131+
propertyDeclaration.Type,
132+
context.CancellationToken
133+
);
119134
var type = typeInfo.Type;
120135

121136
if (type != null && HasAutoGeneratedDtoAttribute(type))
@@ -128,7 +143,10 @@ private void AnalyzeFieldDeclaration(SyntaxNodeAnalysisContext context)
128143
{
129144
var fieldDeclaration = (FieldDeclarationSyntax)context.Node;
130145

131-
var typeInfo = context.SemanticModel.GetTypeInfo(fieldDeclaration.Declaration.Type, context.CancellationToken);
146+
var typeInfo = context.SemanticModel.GetTypeInfo(
147+
fieldDeclaration.Declaration.Type,
148+
context.CancellationToken
149+
);
132150
var type = typeInfo.Type;
133151

134152
if (type != null && HasAutoGeneratedDtoAttribute(type))
@@ -143,35 +161,36 @@ private void AnalyzeFieldDeclaration(SyntaxNodeAnalysisContext context)
143161
private static bool HasAutoGeneratedDtoAttribute(ITypeSymbol type)
144162
{
145163
// Check direct attributes on the type
146-
return type.GetAttributes().Any(attr =>
147-
{
148-
var attrClass = attr.AttributeClass;
149-
if (attrClass == null)
164+
return type.GetAttributes()
165+
.Any(attr =>
150166
{
151-
return false;
152-
}
153-
154-
// Optimized check: compare Name and ContainingNamespace instead of ToDisplayString()
155-
if (attrClass.Name != "LinqraftAutoGeneratedDtoAttribute")
156-
{
157-
return false;
158-
}
159-
160-
var containingNamespace = attrClass.ContainingNamespace;
161-
return containingNamespace != null
162-
&& !containingNamespace.IsGlobalNamespace
163-
&& containingNamespace.Name == "Linqraft"
164-
&& containingNamespace.ContainingNamespace?.IsGlobalNamespace == true;
165-
});
167+
var attrClass = attr.AttributeClass;
168+
if (attrClass == null)
169+
{
170+
return false;
171+
}
172+
173+
// Optimized check: compare Name and ContainingNamespace instead of ToDisplayString()
174+
if (attrClass.Name != "LinqraftAutoGeneratedDtoAttribute")
175+
{
176+
return false;
177+
}
178+
179+
var containingNamespace = attrClass.ContainingNamespace;
180+
return containingNamespace != null
181+
&& !containingNamespace.IsGlobalNamespace
182+
&& containingNamespace.Name == "Linqraft"
183+
&& containingNamespace.ContainingNamespace?.IsGlobalNamespace == true;
184+
});
166185
}
167186

168-
private void ReportDiagnostic(SyntaxNodeAnalysisContext context, Location location, string typeName)
187+
private void ReportDiagnostic(
188+
SyntaxNodeAnalysisContext context,
189+
Location location,
190+
string typeName
191+
)
169192
{
170-
var diagnostic = Diagnostic.Create(
171-
RuleInstance,
172-
location,
173-
typeName
174-
);
193+
var diagnostic = Diagnostic.Create(RuleInstance, location, typeName);
175194
context.ReportDiagnostic(diagnostic);
176195
}
177196
}

0 commit comments

Comments
 (0)