aboutsummaryrefslogtreecommitdiff
path: root/ErrorResponseCodeGenerator
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-18 00:50:31 +0800
committercrupest <crupest@outlook.com>2020-01-18 00:50:31 +0800
commitc5f3c69b3a008ab87542e523e2a59f37801bd65a (patch)
tree8479cc9af91a7e7b1c0c60a0abc778244359bdbd /ErrorResponseCodeGenerator
parent289c7e1fada1f4dae6ce5e421e997ebddd55c2df (diff)
downloadtimeline-c5f3c69b3a008ab87542e523e2a59f37801bd65a.tar.gz
timeline-c5f3c69b3a008ab87542e523e2a59f37801bd65a.tar.bz2
timeline-c5f3c69b3a008ab87542e523e2a59f37801bd65a.zip
...
Diffstat (limited to 'ErrorResponseCodeGenerator')
-rw-r--r--ErrorResponseCodeGenerator/ErrorResponseCodeGenerator.csproj12
-rw-r--r--ErrorResponseCodeGenerator/Program.cs62
2 files changed, 74 insertions, 0 deletions
diff --git a/ErrorResponseCodeGenerator/ErrorResponseCodeGenerator.csproj b/ErrorResponseCodeGenerator/ErrorResponseCodeGenerator.csproj
new file mode 100644
index 00000000..e77a1ba3
--- /dev/null
+++ b/ErrorResponseCodeGenerator/ErrorResponseCodeGenerator.csproj
@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>netcoreapp3.1</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\Timeline.ErrorCodes\Timeline.ErrorCodes.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/ErrorResponseCodeGenerator/Program.cs b/ErrorResponseCodeGenerator/Program.cs
new file mode 100644
index 00000000..cf021927
--- /dev/null
+++ b/ErrorResponseCodeGenerator/Program.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Linq;
+using System.Reflection;
+
+namespace ErrorResponseCodeGenerator
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ var code = "";
+
+ void RecursiveAddErrorCode(Type type, bool root)
+ {
+ code += $@"
+ public static class {(root ? "ErrorResponse" : type.Name)}
+ {{
+";
+
+ 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));
+ }}
+";
+ }
+
+ foreach (var nestedType in type.GetNestedTypes())
+ {
+ RecursiveAddErrorCode(nestedType, false);
+ }
+
+ code += @"
+ }
+";
+ }
+
+ RecursiveAddErrorCode(typeof(Timeline.Models.Http.ErrorCodes), true);
+
+ code = @"
+using static Timeline.Resources.Messages;
+
+namespace Timeline.Models.Http
+{
+$
+}
+".Replace("$", code);
+
+ Console.WriteLine(code);
+ }
+ }
+}