Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating valid, meaningful method name from destination type #586

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
42 changes: 40 additions & 2 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using CommandLine;
using ExpressionDebugger;
using Mapster.Models;
Expand Down Expand Up @@ -497,8 +498,7 @@ private static void GenerateExtensionMethods(MapType mapType, TypeAdapterConfig
{
//add type name to prevent duplication
translator.Translate(entityType);
var destName = translator.Translate(tuple.Destination);
destName = destName.Split('.').Last();
var destName = GetMethodNameFromType(tuple.Destination);

var name = tuple.Destination.Name == entityType.Name
? destName
Expand All @@ -524,5 +524,43 @@ private static void GenerateExtensionMethods(MapType mapType, TypeAdapterConfig
"ProjectTo" + name);
}
}

private static string GetMethodNameFromType(Type type) => GetMethodNameFromType(new StringBuilder(), type).ToString();

private static StringBuilder GetMethodNameFromType(StringBuilder sb, Type type)
{
foreach (var subType in type.GenericTypeArguments)
{
GetMethodNameFromType(sb, subType);
}

if (type.IsArray)
{
GetMethodNameFromType(sb, type.GetElementType()!);
sb.Append("Array");
return sb;
}

var name = type.Name;
var i = name.IndexOf('`');
if (i>0) name = name.Remove(i);
name = name switch
{
"Nullable" => "",
"SByte" => "Sbyte",
"Int16" => "Short",
"UInt16" => "Ushort",
"Int32" => "Int",
"UInt32" => "Uint",
"Int64" => "Long",
"UInt64" => "Ulong",
"Single" => "Float",
"Boolean" => "Bool",
_ => name,
};

if (!string.IsNullOrEmpty(name)) sb.Append(name);
return sb;
}
}
}