Added RouteTypeName attribute that can be used to rename a type when generating client code.

This commit is contained in:
MattMo 2023-03-31 07:02:44 -07:00
parent 4bc388d86b
commit fe99ba9b9d
5 changed files with 44 additions and 7 deletions

View File

@ -30,6 +30,7 @@ namespace MontoyaTech.Rest.Net.Example
} }
} }
[RouteTypeName("UserDto")]
public class User : BaseUser public class User : BaseUser
{ {
public PlatformID MachineType; public PlatformID MachineType;

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName> <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace> <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile> <GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.4.5</Version> <Version>1.4.6</Version>
<PackageReleaseNotes></PackageReleaseNotes> <PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon> <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup> </PropertyGroup>

View File

@ -184,7 +184,9 @@ namespace MontoyaTech.Rest.Net
{ {
writer.WriteBreak(); writer.WriteBreak();
writer.Write($"public {(type.IsEnum ? "enum" : "class")} {type.Name}"); var newName = type.GetCustomAttribute<RouteTypeName>();
writer.Write($"public {(type.IsEnum ? "enum" : "class")} {(newName != null ? newName.Name : type.Name)}");
if (type.IsEnum) if (type.IsEnum)
writer.Write(" : ").Write(this.GetTypeFullyResolvedName(Enum.GetUnderlyingType(type))); writer.Write(" : ").Write(this.GetTypeFullyResolvedName(Enum.GetUnderlyingType(type)));
@ -193,7 +195,7 @@ namespace MontoyaTech.Rest.Net
writer.NewLine().WriteLine("{").Indent(); writer.NewLine().WriteLine("{").Indent();
FieldInfo[] fields = null; FieldInfo[] fields;
if (type.IsEnum) if (type.IsEnum)
fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static); fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);

View File

@ -285,12 +285,21 @@ namespace MontoyaTech.Rest.Net
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
int genericSymbol = type.Name.IndexOf('`'); var newName = type.GetCustomAttribute<RouteTypeName>();
if (genericSymbol == -1) if (newName != null)
builder.Append(type.Name); {
builder.Append(newName.Name);
}
else else
builder.Append(type.Name.Substring(0, genericSymbol)); {
int genericSymbol = type.Name.IndexOf('`');
if (genericSymbol == -1)
builder.Append(type.Name);
else
builder.Append(type.Name.Substring(0, genericSymbol));
}
var genericArguments = type.GetGenericArguments(); var genericArguments = type.GetGenericArguments();

25
Rest.Net/RouteTypeName.cs Normal file
View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MontoyaTech.Rest.Net
{
/// <summary>
/// The outline of an attribute that can be used to rename a type when it's used in routes.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public class RouteTypeName : Attribute
{
/// <summary>
/// The new name for this type.
/// </summary>
public string Name;
public RouteTypeName(string name)
{
this.Name = name;
}
}
}