diff options
Diffstat (limited to 'ErrorResponseCodeGenerator')
-rw-r--r-- | ErrorResponseCodeGenerator/ErrorResponseCodeGenerator.csproj | 12 | ||||
-rw-r--r-- | ErrorResponseCodeGenerator/Program.cs | 62 |
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);
+ }
+ }
+}
|