diff options
author | crupest <crupest@outlook.com> | 2020-10-27 15:39:09 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-10-27 15:39:09 +0800 |
commit | 0c4caaebe2480e77918d5d7df234f0edaeab74ba (patch) | |
tree | 6205c8ffd13a00aa2e6045413577bcf81d899650 | |
parent | fe1ca658ca5afbe7cd74303bb9b75f1e735c0762 (diff) | |
download | timeline-0c4caaebe2480e77918d5d7df234f0edaeab74ba.tar.gz timeline-0c4caaebe2480e77918d5d7df234f0edaeab74ba.tar.bz2 timeline-0c4caaebe2480e77918d5d7df234f0edaeab74ba.zip |
refactor(back): Refactor error codes code generator.
-rw-r--r-- | Timeline.ErrorCodes.CodeGenerator/Program.cs | 65 | ||||
-rw-r--r-- | Timeline/Models/Http/ErrorResponse.cs | 13 |
2 files changed, 38 insertions, 40 deletions
diff --git a/Timeline.ErrorCodes.CodeGenerator/Program.cs b/Timeline.ErrorCodes.CodeGenerator/Program.cs index d182de7c..84ab5908 100644 --- a/Timeline.ErrorCodes.CodeGenerator/Program.cs +++ b/Timeline.ErrorCodes.CodeGenerator/Program.cs @@ -1,6 +1,7 @@ using System;
using System.Linq;
using System.Reflection;
+using System.Text;
namespace Timeline.ErrorCodes.CodeGenerator
{
@@ -8,57 +9,65 @@ namespace Timeline.ErrorCodes.CodeGenerator {
static void Main(string[] args)
{
- var code = "";
+ string Indent(int n)
+ {
+ const string indent = " ";
+ return string.Concat(Enumerable.Repeat(indent, n));
+ }
+
+ StringBuilder code = new StringBuilder();
+
+ code.AppendLine("using static Timeline.Resources.Messages;");
+ code.AppendLine();
+ code.AppendLine("namespace Timeline.Models.Http");
+ code.AppendLine("{");
+
+ int depth = 1;
void RecursiveAddErrorCode(Type type, bool root)
{
- code += $@"
- public static class {(root ? "ErrorResponse" : type.Name)}
- {{
-";
+ code.AppendLine($"{Indent(depth)}public static class {(root ? "ErrorResponse" : type.Name)}");
+ code.AppendLine($"{Indent(depth)}{{");
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(int)))
{
var path = type.FullName.Replace("+", ".").Replace("Timeline.Models.Http.ErrorCodes.", "") + "." + field.Name;
- code += $@"
- public static CommonResponse {field.Name}(params object?[] formatArgs)
- {{
- return new CommonResponse({"ErrorCodes." + path}, string.Format({path.Replace(".", "_")}, formatArgs));
- }}
-
- public static CommonResponse CustomMessage_{field.Name}(string message, params object?[] formatArgs)
- {{
- return new CommonResponse({"ErrorCodes." + path}, string.Format(message, formatArgs));
- }}
-";
+ code.AppendLine($"{Indent(depth + 1)}public static CommonResponse {field.Name}(params object?[] formatArgs)");
+ code.AppendLine($"{Indent(depth + 1)}{{");
+ code.AppendLine($"{Indent(depth + 2)}return new CommonResponse({"ErrorCodes." + path}, string.Format({path.Replace(".", "_")}, formatArgs));");
+ code.AppendLine($"{Indent(depth + 1)}}}");
+ code.AppendLine();
+ code.AppendLine($"{Indent(depth + 1)}public static CommonResponse CustomMessage_{field.Name}(string message, params object?[] formatArgs)");
+ code.AppendLine($"{Indent(depth + 1)}{{");
+ code.AppendLine($"{Indent(depth + 2)}return new CommonResponse({"ErrorCodes." + path}, string.Format(message, formatArgs));");
+ code.AppendLine($"{Indent(depth + 1)}}}");
+ code.AppendLine();
}
+ depth += 1;
+
foreach (var nestedType in type.GetNestedTypes())
{
RecursiveAddErrorCode(nestedType, false);
}
- code += @"
- }
-";
+ depth -= 1;
+
+ code.AppendLine($"{Indent(depth)}}}");
+ code.AppendLine();
}
RecursiveAddErrorCode(typeof(Timeline.Models.Http.ErrorCodes), true);
- code = @"
-using static Timeline.Resources.Messages;
+ code.AppendLine("}");
-namespace Timeline.Models.Http
-{
-$
-}
-".Replace("$", code);
+ var generatedCode = code.ToString();
- Console.WriteLine(code);
+ Console.WriteLine(generatedCode);
- TextCopy.ClipboardService.SetText(code);
+ TextCopy.ClipboardService.SetText(generatedCode);
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Code has copied to clipboard!");
diff --git a/Timeline/Models/Http/ErrorResponse.cs b/Timeline/Models/Http/ErrorResponse.cs index 7ba536f9..ac86481f 100644 --- a/Timeline/Models/Http/ErrorResponse.cs +++ b/Timeline/Models/Http/ErrorResponse.cs @@ -1,15 +1,11 @@ -
-using static Timeline.Resources.Messages;
+using static Timeline.Resources.Messages;
namespace Timeline.Models.Http
{
-
public static class ErrorResponse
{
-
public static class Common
{
-
public static CommonResponse InvalidModel(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.Common.InvalidModel, string.Format(Common_InvalidModel, formatArgs));
@@ -42,7 +38,6 @@ namespace Timeline.Models.Http public static class Header
{
-
public static CommonResponse IfNonMatch_BadFormat(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.Common.Header.IfNonMatch_BadFormat, string.Format(Common_Header_IfNonMatch_BadFormat, formatArgs));
@@ -57,7 +52,6 @@ namespace Timeline.Models.Http public static class Content
{
-
public static CommonResponse TooBig(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.Common.Content.TooBig, string.Format(Common_Content_TooBig, formatArgs));
@@ -74,7 +68,6 @@ namespace Timeline.Models.Http public static class UserCommon
{
-
public static CommonResponse NotExist(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.UserCommon.NotExist, string.Format(UserCommon_NotExist, formatArgs));
@@ -89,7 +82,6 @@ namespace Timeline.Models.Http public static class TokenController
{
-
public static CommonResponse Create_BadCredential(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.TokenController.Create_BadCredential, string.Format(TokenController_Create_BadCredential, formatArgs));
@@ -144,7 +136,6 @@ namespace Timeline.Models.Http public static class UserController
{
-
public static CommonResponse UsernameConflict(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.UserController.UsernameConflict, string.Format(UserController_UsernameConflict, formatArgs));
@@ -169,7 +160,6 @@ namespace Timeline.Models.Http public static class UserAvatar
{
-
public static CommonResponse BadFormat_CantDecode(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_CantDecode, string.Format(UserAvatar_BadFormat_CantDecode, formatArgs));
@@ -204,7 +194,6 @@ namespace Timeline.Models.Http public static class TimelineController
{
-
public static CommonResponse NameConflict(params object?[] formatArgs)
{
return new CommonResponse(ErrorCodes.TimelineController.NameConflict, string.Format(TimelineController_NameConflict, formatArgs));
|