Skip to content

Commit

Permalink
Merge pull request #586 from rafalka/bugfix/580-incorrect_func_name_w…
Browse files Browse the repository at this point in the history
…hen_generics_or_nullable

Generating valid, meaningful method name from destination type
  • Loading branch information
andrerav committed May 29, 2023
2 parents 1821035 + 502186c commit c2d370f
Showing 1 changed file with 40 additions and 2 deletions.
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;
}
}
}

0 comments on commit c2d370f

Please sign in to comment.