diff options
author | 杨宇千 <crupest@outlook.com> | 2020-02-01 00:26:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-01 00:26:35 +0800 |
commit | d703269e06d4c9e254fe2d5589ff04cdd6a9b366 (patch) | |
tree | f02f8d57440c777d4732bc4439f82e8b25c6732c /ErrorResponseCodeGenerator/Program.cs | |
parent | 631731e5c2253116a53fdc435afca184251a34fc (diff) | |
parent | bddf1d6eaac782672071df6527c40c81c3123f3a (diff) | |
download | timeline-d703269e06d4c9e254fe2d5589ff04cdd6a9b366.tar.gz timeline-d703269e06d4c9e254fe2d5589ff04cdd6a9b366.tar.bz2 timeline-d703269e06d4c9e254fe2d5589ff04cdd6a9b366.zip |
Merge pull request #56 from crupest/dev
Refactor API to be RESTful.
Diffstat (limited to 'ErrorResponseCodeGenerator/Program.cs')
-rw-r--r-- | ErrorResponseCodeGenerator/Program.cs | 62 |
1 files changed, 62 insertions, 0 deletions
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);
+ }
+ }
+}
|