Skip to content

Commit 859661e

Browse files
committed
GenerateMapper setting, apply ExplicitMapping to AdaptAttribute, fix tool duplicate name
1 parent 0bcafbb commit 859661e

File tree

14 files changed

+119
-36
lines changed

14 files changed

+119
-36
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"mapster.tool": {
6-
"version": "2.0.47",
6+
"version": "6.5.2",
77
"commands": [
88
"dotnet-mapster"
99
]

src/Mapster.Core/Attributes/BaseAdaptAttribute.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,14 @@ protected BaseAdaptAttribute(string name)
3131

3232
public class AdaptFromAttribute : BaseAdaptAttribute
3333
{
34-
public AdaptFromAttribute(Type type) : base(type)
35-
{
36-
this.MapType = MapType.Map | MapType.MapToTarget;
37-
}
38-
39-
public AdaptFromAttribute(string name) : base(name)
40-
{
41-
this.MapType = MapType.Map | MapType.MapToTarget;
42-
}
34+
public AdaptFromAttribute(Type type) : base(type) { }
35+
public AdaptFromAttribute(string name) : base(name) { }
4336
}
4437

4538
public class AdaptToAttribute : BaseAdaptAttribute
4639
{
47-
public AdaptToAttribute(Type type) : base(type)
48-
{
49-
this.MapType = MapType.Map | MapType.MapToTarget | MapType.Projection;
50-
}
51-
52-
public AdaptToAttribute(string name) : base(name)
53-
{
54-
this.MapType = MapType.Map | MapType.MapToTarget | MapType.Projection;
55-
}
40+
public AdaptToAttribute(Type type) : base(type) { }
41+
public AdaptToAttribute(string name) : base(name) { }
5642
}
5743

5844
public class AdaptTwoWaysAttribute : AdaptToAttribute

src/Mapster.Tool/Mapster.Tool.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<SignAssembly>true</SignAssembly>
1616
<AssemblyOriginatorKeyFile>Mapster.Tool.snk</AssemblyOriginatorKeyFile>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
18-
<Version>6.5.1</Version>
18+
<Version>6.5.2</Version>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<Copyright>Copyright (c) 2020 Chaowlert Chaisrichalermpol</Copyright>
2121
<PackageIcon>icon.png</PackageIcon>
@@ -30,7 +30,7 @@
3030

3131
<ItemGroup>
3232
<PackageReference Include="CommandLineParser" Version="2.8.0" />
33-
<PackageReference Include="ExpressionTranslator" Version="2.3.1" />
33+
<PackageReference Include="ExpressionTranslator" Version="2.3.2" />
3434
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.6" />
3535
</ItemGroup>
3636

src/Mapster.Tool/Program.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,20 @@ private static void GenerateExtensions(ExtensionOptions opt)
252252
var attrs = type.SafeGetCustomAttributes();
253253
var mapperAttr = attrs.OfType<GenerateMapperAttribute>()
254254
.FirstOrDefault();
255-
if (mapperAttr == null)
255+
var ruleMaps = config.RuleMap
256+
.Where(it => it.Key.Source == type &&
257+
it.Value.Settings.GenerateMapper is MapType)
258+
.ToList();
259+
if (mapperAttr == null && ruleMaps.Count == 0)
256260
continue;
257261

262+
mapperAttr ??= new GenerateMapperAttribute();
258263
var set = mapperAttr.ForAttributes?.ToHashSet();
259264
var adaptAttrs = attrs
260265
.OfType<BaseAdaptAttribute>()
261266
.Where(it => set?.Contains(it.GetType()) != false)
262267
.ToList();
263-
if (adaptAttrs.Count == 0)
268+
if (adaptAttrs.Count == 0 && ruleMaps.Count == 0)
264269
continue;
265270

266271
Console.WriteLine($"Processing: {type.FullName}");
@@ -289,7 +294,8 @@ private static void GenerateExtensions(ExtensionOptions opt)
289294
continue;
290295

291296
var tuple = new TypeTuple(fromType, type);
292-
GenerateExtensionMethods(attr.MapType, config, tuple, translator, type, mapperAttr.IsHelperClass);
297+
var mapType = attr.MapType == 0 ? MapType.Map | MapType.MapToTarget : attr.MapType;
298+
GenerateExtensionMethods(mapType, config, tuple, translator, type, mapperAttr.IsHelperClass);
293299
}
294300

295301
if (attr is AdaptToAttribute)
@@ -308,10 +314,17 @@ private static void GenerateExtensions(ExtensionOptions opt)
308314
continue;
309315

310316
var tuple = new TypeTuple(type, toType);
311-
GenerateExtensionMethods(attr.MapType, config, tuple, translator, type, mapperAttr.IsHelperClass);
317+
var mapType = attr.MapType == 0 ? MapType.Map | MapType.MapToTarget | MapType.Projection : attr.MapType;
318+
GenerateExtensionMethods(mapType, config, tuple, translator, type, mapperAttr.IsHelperClass);
312319
}
313320
}
314321

322+
foreach (var (tuple, rule) in ruleMaps)
323+
{
324+
var mapType = (MapType) rule.Settings.GenerateMapper!;
325+
GenerateExtensionMethods(mapType, config, tuple, translator, type, mapperAttr.IsHelperClass);
326+
}
327+
315328
var code = translator.ToString();
316329
var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");
317330
WriteFile(code, path);
@@ -321,9 +334,13 @@ private static void GenerateExtensions(ExtensionOptions opt)
321334
private static void GenerateExtensionMethods(MapType mapType, TypeAdapterConfig config, TypeTuple tuple,
322335
ExpressionTranslator translator, Type entityType, bool isHelperClass)
323336
{
324-
var name = tuple.Destination == entityType
325-
? "Entity"
326-
: tuple.Destination.Name.Replace(entityType.Name, "");
337+
//add type name to prevent duplication
338+
translator.Translate(entityType);
339+
var destName = translator.Translate(tuple.Destination);
340+
341+
var name = tuple.Destination.Name == entityType.Name
342+
? destName
343+
: destName.Replace(entityType.Name, "");
327344
if ((mapType & MapType.Map) > 0)
328345
{
329346
var expr = config.CreateMapExpression(tuple, MapType.Map);

src/Mapster.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
77
ProjectSection(SolutionItems) = preProject
88
.editorconfig = .editorconfig
99
..\.config\dotnet-tools.json = ..\.config\dotnet-tools.json
10+
global.json = global.json
1011
..\LICENSE = ..\LICENSE
1112
..\README.md = ..\README.md
1213
EndProjectSection
@@ -53,7 +54,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{EF7E
5354
EndProject
5455
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mapster.SourceGenerator", "Mapster.SourceGenerator\Mapster.SourceGenerator.csproj", "{BFAFD4DF-7530-46EA-866E-C2A74DAEBDB1}"
5556
EndProject
56-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mapster.Core", "Mapster.Core\Mapster.Core.csproj", "{1A7D2FD4-DDEC-4E11-93FF-1310F34F67CF}"
57+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mapster.Core", "Mapster.Core\Mapster.Core.csproj", "{1A7D2FD4-DDEC-4E11-93FF-1310F34F67CF}"
5758
EndProject
5859
Global
5960
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/Mapster/TypeAdapterConfig.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,10 @@ where typeof(AdaptToAttribute).IsAssignableFrom(o.GetAttributeType())
497497
return rules1;
498498
var rules2 = from type in tuple.Destination.GetAllTypes()
499499
from o in type.GetTypeInfo().GetCustomAttributesData()
500-
where typeof(BaseAdaptAttribute).IsAssignableFrom(o.GetAttributeType())
500+
where typeof(AdaptFromAttribute).IsAssignableFrom(o.GetAttributeType()) ||
501+
typeof(AdaptTwoWaysAttribute).IsAssignableFrom(o.GetAttributeType())
501502
let attr = o.CreateCustomAttribute<BaseAdaptAttribute>()
502-
where attr != null && (attr.MapType & mapType) != 0 && (attr is AdaptFromAttribute || attr is AdaptTwoWaysAttribute)
503+
where attr != null && (attr.MapType & mapType) != 0
503504
where attr.Type == null || attr.Type == tuple.Source
504505
where attr.Name == null || attr.Name.Replace("[name]", type.Name) == tuple.Source.Name
505506
let distance = GetSubclassDistance(tuple.Destination, type, true)
@@ -553,11 +554,19 @@ internal TypeAdapterSettings GetMergedSettings(TypeTuple tuple, MapType mapType)
553554
MapType = mapType,
554555
ExplicitMapping = this.RuleMap.ContainsKey(tuple),
555556
};
557+
558+
//auto add setting if there is attr setting
559+
var attrSettings = GetAttributeSettings(tuple, mapType).ToList();
560+
if (!arg.ExplicitMapping && attrSettings.Any(rule => rule.Priority(arg) == 100))
561+
{
562+
GetSettings(tuple);
563+
arg.ExplicitMapping = true;
564+
}
565+
556566
var result = new TypeAdapterSettings();
557567
lock (this.Rules)
558568
{
559-
var rules = this.Rules.Reverse<TypeAdapterRule>()
560-
.Concat(GetAttributeSettings(tuple, mapType));
569+
var rules = this.Rules.Reverse<TypeAdapterRule>().Concat(attrSettings);
561570
var settings = from rule in rules
562571
let priority = rule.Priority(arg)
563572
where priority != null

src/Mapster/TypeAdapterSetter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,14 @@ public TypeAdapterSetter<TSource, TDestination> Fork(Action<TypeAdapterConfig> a
648648
return this;
649649
}
650650

651+
public TypeAdapterSetter<TSource, TDestination> GenerateMapper(MapType mapType)
652+
{
653+
this.CheckCompiled();
654+
655+
Settings.GenerateMapper = mapType;
656+
return this;
657+
}
658+
651659
public TwoWaysTypeAdapterSetter<TSource, TDestination> TwoWays()
652660
{
653661
return new TwoWaysTypeAdapterSetter<TSource, TDestination>(this.Config);
@@ -879,5 +887,12 @@ public TwoWaysTypeAdapterSetter<TSource, TDestination> Fork(Action<TypeAdapterCo
879887
DestinationToSourceSetter.Fork(action);
880888
return this;
881889
}
890+
891+
public TwoWaysTypeAdapterSetter<TSource, TDestination> GenerateMapper(MapType mapType)
892+
{
893+
SourceToDestinationSetter.GenerateMapper(mapType);
894+
DestinationToSourceSetter.GenerateMapper(mapType);
895+
return this;
896+
}
882897
}
883898
}

src/Mapster/TypeAdapterSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public bool? EnableNonPublicMembers
7272
get => Get(nameof(EnableNonPublicMembers));
7373
set => Set(nameof(EnableNonPublicMembers), value);
7474
}
75+
public object? GenerateMapper
76+
{
77+
get => Get<object>(nameof(GenerateMapper));
78+
set => Set(nameof(GenerateMapper), value);
79+
}
7580

7681
public List<Func<IMemberModel, MemberSide, bool?>> ShouldMapMember
7782
{

src/Sample.CodeGen/Controllers/SchoolController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public IQueryable<Person> GetStudents()
4343
[HttpPost("student")]
4444
public async Task AddStudent([FromBody] StudentAdd data)
4545
{
46-
var student = data.AdaptToEntity();
46+
var student = data.AdaptToStudent();
4747
_context.Students.Add(student);
4848
await _context.SaveChangesAsync();
4949
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Mapster;
2+
using Sample.CodeGen.Models;
3+
4+
namespace Sample.CodeGen
5+
{
6+
public class MappingRegister : IRegister
7+
{
8+
public void Register(TypeAdapterConfig config)
9+
{
10+
config.ForType<Person, Person>()
11+
.GenerateMapper(MapType.Map | MapType.MapToTarget);
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)