From d6ec7cc792d715f77d946d5d6bcea9686b0f7d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Kukla?= Date: Wed, 5 Jul 2023 16:57:56 +0200 Subject: [PATCH] Added types used in rules to types hashset Assembly defines open generic only, so we have to add specialised types used in mappings Removed removing "Nullable" from type name --- src/Mapster.Tool/Program.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Mapster.Tool/Program.cs b/src/Mapster.Tool/Program.cs index bb69bb8f..0f03634b 100644 --- a/src/Mapster.Tool/Program.cs +++ b/src/Mapster.Tool/Program.cs @@ -80,7 +80,7 @@ private static void GenerateMappers(MapperOptions opt) { Implements = new[] {type}, Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace), - TypeName = attr.Name ?? GetImplName(type.Name), + TypeName = attr.Name ?? GetImplName(GetCodeFriendlyTypeName(type)), IsInternal = attr.IsInternal, PrintFullTypeName = opt.PrintFullTypeName, }; @@ -399,6 +399,10 @@ private static void GenerateExtensions(ExtensionOptions opt) } } var types = assemblies.SelectMany(it => it.GetTypes()).ToHashSet(); + + // assemblies defines open generic only, so we have to add specialised types used in mappings + foreach (var (key, _) in config.RuleMap) types.Add(key.Source); + var configDict = new Dictionary(); foreach (var builder in codeGenConfig.AdaptAttributeBuilders) { @@ -443,7 +447,7 @@ private static void GenerateExtensions(ExtensionOptions opt) { IsStatic = true, Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace), - TypeName = mapperAttr.Name.Replace("[name]", type.Name), + TypeName = mapperAttr.Name.Replace("[name]", GetCodeFriendlyTypeName(type)), IsInternal = mapperAttr.IsInternal, PrintFullTypeName = opt.PrintFullTypeName, }; @@ -498,7 +502,7 @@ private static void GenerateExtensionMethods(MapType mapType, TypeAdapterConfig { //add type name to prevent duplication translator.Translate(entityType); - var destName = GetMethodNameFromType(tuple.Destination); + var destName = GetCodeFriendlyTypeName(tuple.Destination); var name = tuple.Destination.Name == entityType.Name ? destName @@ -525,18 +529,18 @@ private static void GenerateExtensionMethods(MapType mapType, TypeAdapterConfig } } - private static string GetMethodNameFromType(Type type) => GetMethodNameFromType(new StringBuilder(), type).ToString(); + private static string GetCodeFriendlyTypeName(Type type) => GetCodeFriendlyTypeName(new StringBuilder(), type).ToString(); - private static StringBuilder GetMethodNameFromType(StringBuilder sb, Type type) + private static StringBuilder GetCodeFriendlyTypeName(StringBuilder sb, Type type) { foreach (var subType in type.GenericTypeArguments) { - GetMethodNameFromType(sb, subType); + GetCodeFriendlyTypeName(sb, subType); } if (type.IsArray) { - GetMethodNameFromType(sb, type.GetElementType()!); + GetCodeFriendlyTypeName(sb, type.GetElementType()!); sb.Append("Array"); return sb; } @@ -546,7 +550,6 @@ private static StringBuilder GetMethodNameFromType(StringBuilder sb, Type type) if (i>0) name = name.Remove(i); name = name switch { - "Nullable" => "", "SByte" => "Sbyte", "Int16" => "Short", "UInt16" => "Ushort",