-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
615 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<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/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=attributes/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=enums/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=mapcontext/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=mapcontext/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=register/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Mapster | ||
{ | ||
public class AdaptAttributeBuilder | ||
{ | ||
public BaseAdaptAttribute Attribute { get; } | ||
public Dictionary<Type, Dictionary<string, PropertySetting>> TypeSettings { get; } = new Dictionary<Type, Dictionary<string, PropertySetting>>(); | ||
public List<Func<Type, Type?>> AlterTypes { get; } = new List<Func<Type, Type?>>(); | ||
|
||
public AdaptAttributeBuilder(BaseAdaptAttribute attribute) | ||
{ | ||
this.Attribute = attribute; | ||
} | ||
|
||
public AdaptAttributeBuilder ForTypes(params Type[] types) | ||
{ | ||
foreach (var type in types) | ||
{ | ||
if (!this.TypeSettings.ContainsKey(type)) | ||
this.TypeSettings.Add(type, new Dictionary<string, PropertySetting>()); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace) | ||
{ | ||
foreach (var type in assembly.GetTypes()) | ||
{ | ||
if (type.Namespace == @namespace && !this.TypeSettings.ContainsKey(type)) | ||
this.TypeSettings.Add(type, new Dictionary<string, PropertySetting>()); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder ForType<T>(Action<PropertySettingBuilder<T>>? propertyConfig = null) | ||
{ | ||
if (!this.TypeSettings.TryGetValue(typeof(T), out var settings)) | ||
{ | ||
settings = new Dictionary<string, PropertySetting>(); | ||
this.TypeSettings.Add(typeof(T), settings); | ||
} | ||
|
||
propertyConfig?.Invoke(new PropertySettingBuilder<T>(settings)); | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder ExcludeTypes(params Type[] types) | ||
{ | ||
foreach (var type in types) | ||
{ | ||
this.TypeSettings.Remove(type); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder ExcludeTypes(Func<Type, bool> predicate) | ||
{ | ||
foreach (var type in this.TypeSettings.Keys.ToList()) | ||
{ | ||
if (predicate(type)) | ||
this.TypeSettings.Remove(type); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder IgnoreAttributes(params Type[] attributes) | ||
{ | ||
this.Attribute.IgnoreAttributes = attributes; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder IgnoreNoAttributes(params Type[] attributes) | ||
{ | ||
this.Attribute.IgnoreNoAttributes = attributes; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder IgnoreNamespaces(params string[] namespaces) | ||
{ | ||
this.Attribute.IgnoreNamespaces = namespaces; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder IgnoreNullValues(bool value) | ||
{ | ||
this.Attribute.IgnoreNullValues = value; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder MapToConstructor(bool value) | ||
{ | ||
this.Attribute.MapToConstructor = value; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder MaxDepth(int depth) | ||
{ | ||
this.Attribute.MaxDepth = depth; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder PreserveReference(bool value) | ||
{ | ||
this.Attribute.PreserveReference = value; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder ShallowCopyForSameType(bool value) | ||
{ | ||
this.Attribute.ShallowCopyForSameType = value; | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder AlterType<TFrom, TTo>() | ||
{ | ||
this.AlterTypes.Add(type => type == typeof(TFrom) ? typeof(TTo) : null); | ||
return this; | ||
} | ||
|
||
public AdaptAttributeBuilder AlterType(Func<Type, bool> predicate, Type toType) | ||
{ | ||
this.AlterTypes.Add(type => predicate(type) ? toType : null); | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Mapster | ||
{ | ||
public class CodeGenerationConfig | ||
{ | ||
public List<AdaptAttributeBuilder> AdaptAttributeBuilders { get; } = new List<AdaptAttributeBuilder>(); | ||
public List<GenerateMapperAttributeBuilder> GenerateMapperAttributeBuilders { get; } = new List<GenerateMapperAttributeBuilder>(); | ||
public AdaptAttributeBuilder Default { get; } = new AdaptAttributeBuilder(new AdaptFromAttribute("void")); | ||
|
||
public AdaptAttributeBuilder AdaptTo(string name, MapType? mapType = null) | ||
{ | ||
var builder = new AdaptAttributeBuilder(new AdaptToAttribute(name) {MapType = mapType ?? 0}); | ||
AdaptAttributeBuilders.Add(builder); | ||
return builder; | ||
} | ||
|
||
public AdaptAttributeBuilder AdaptFrom(string name, MapType? mapType = null) | ||
{ | ||
var builder = new AdaptAttributeBuilder(new AdaptFromAttribute(name) {MapType = mapType ?? 0}); | ||
AdaptAttributeBuilders.Add(builder); | ||
return builder; | ||
} | ||
|
||
public AdaptAttributeBuilder AdaptTwoWays(string name, MapType? mapType = null) | ||
{ | ||
var builder = new AdaptAttributeBuilder(new AdaptTwoWaysAttribute(name) {MapType = mapType ?? 0}); | ||
AdaptAttributeBuilders.Add(builder); | ||
return builder; | ||
} | ||
|
||
public GenerateMapperAttributeBuilder GenerateMapper(string name) | ||
{ | ||
var builder = new GenerateMapperAttributeBuilder(new GenerateMapperAttribute()); | ||
GenerateMapperAttributeBuilders.Add(builder); | ||
return builder; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Mapster.Core/Register/GenerateMapperAttributeBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Mapster | ||
{ | ||
public class GenerateMapperAttributeBuilder | ||
{ | ||
public GenerateMapperAttribute Attribute { get; } | ||
public HashSet<Type> Types { get; } = new HashSet<Type>(); | ||
|
||
public GenerateMapperAttributeBuilder(GenerateMapperAttribute attribute) | ||
{ | ||
this.Attribute = attribute; | ||
} | ||
|
||
public GenerateMapperAttributeBuilder ForTypes(params Type[] types) | ||
{ | ||
this.Types.UnionWith(types); | ||
return this; | ||
} | ||
|
||
public GenerateMapperAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace) | ||
{ | ||
this.Types.UnionWith(assembly.GetTypes().Where(it => it.Namespace == @namespace)); | ||
return this; | ||
} | ||
|
||
public GenerateMapperAttributeBuilder ForType<T>() | ||
{ | ||
this.Types.Add(typeof(T)); | ||
return this; | ||
} | ||
|
||
public GenerateMapperAttributeBuilder ExcludeTypes(params Type[] types) | ||
{ | ||
this.Types.ExceptWith(types); | ||
return this; | ||
} | ||
|
||
public GenerateMapperAttributeBuilder ExcludeTypes(Predicate<Type> predicate) | ||
{ | ||
this.Types.RemoveWhere(predicate); | ||
return this; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Mapster | ||
{ | ||
public interface ICodeGenerationRegister | ||
{ | ||
void Register(CodeGenerationConfig config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
|
||
namespace Mapster | ||
{ | ||
public class PropertySetting | ||
{ | ||
public bool Ignore { get; set; } | ||
public string? TargetPropertyName { get; set; } | ||
public Type? TargetPropertyType { get; set; } | ||
public LambdaExpression? MapFunc { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using Mapster.Utils; | ||
|
||
namespace Mapster | ||
{ | ||
public class PropertySettingBuilder<T> | ||
{ | ||
public Dictionary<string, PropertySetting> Settings { get; } | ||
public PropertySettingBuilder(Dictionary<string, PropertySetting> settings) | ||
{ | ||
this.Settings = settings; | ||
} | ||
|
||
private PropertySetting ForProperty(string name) | ||
{ | ||
if (!this.Settings.TryGetValue(name, out var setting)) | ||
{ | ||
setting = new PropertySetting(); | ||
this.Settings.Add(name, setting); | ||
} | ||
return setting; | ||
} | ||
|
||
public PropertySettingBuilder<T> Ignore<TReturn>(Expression<Func<T, TReturn>> member) | ||
{ | ||
var setting = ForProperty(member.GetMemberName()); | ||
setting.Ignore = true; | ||
return this; | ||
} | ||
|
||
public PropertySettingBuilder<T> Map<TReturn>(Expression<Func<T, TReturn>> member, string targetPropertyName) | ||
{ | ||
var setting = ForProperty(member.GetMemberName()); | ||
setting.TargetPropertyName = targetPropertyName; | ||
return this; | ||
} | ||
|
||
public PropertySettingBuilder<T> Map<TReturn>(Expression<Func<T, TReturn>> member, Type targetPropertyType, string? targetPropertyName = null) | ||
{ | ||
var setting = ForProperty(member.GetMemberName()); | ||
setting.TargetPropertyType = targetPropertyType; | ||
setting.TargetPropertyName = targetPropertyName; | ||
return this; | ||
} | ||
|
||
public PropertySettingBuilder<T> Map<TReturn, TReturn2>(Expression<Func<T, TReturn>> member, Expression<Func<T, TReturn2>> mapFunc, string? targetPropertyName = null) | ||
{ | ||
var setting = ForProperty(member.GetMemberName()); | ||
setting.MapFunc = mapFunc; | ||
setting.TargetPropertyName = targetPropertyName; | ||
return this; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.