Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<BaseAdaptAttribute, TypeAdapterConfig>();
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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",
Expand Down