Skip to content

Commit

Permalink
Added types used in rules to types hashset
Browse files Browse the repository at this point in the history
Assembly defines open generic only, so we have to add specialised types used in mappings

Removed removing "Nullable" from type name
  • Loading branch information
Rafał Kukla committed Jul 5, 2023
1 parent cc81763 commit d6ec7cc
Showing 1 changed file with 11 additions and 8 deletions.
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 GenerateExtensions(ExtensionOptions opt)
{
//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 GenerateExtensions(ExtensionOptions opt)
}
}

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

0 comments on commit d6ec7cc

Please sign in to comment.