From 05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 19:21:35 +0800 Subject: Split front and back end. --- .../DocumentDescriptionDocumentProcessor.cs | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs (limited to 'BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs') diff --git a/BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs b/BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs new file mode 100644 index 00000000..dc5ddd96 --- /dev/null +++ b/BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs @@ -0,0 +1,55 @@ +using NSwag.Generation.Processors; +using NSwag.Generation.Processors.Contexts; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Timeline.Models.Http; + +namespace Timeline.Swagger +{ + public class DocumentDescriptionDocumentProcessor : IDocumentProcessor + { + private static Dictionary GetAllErrorCodes() + { + var errorCodes = new Dictionary(); + + void RecursiveCheckErrorCode(Type type) + { + foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) + .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(int))) + { + var name = (type.FullName + "." + field.Name).Remove(0, typeof(ErrorCodes).FullName!.Length + 1).Replace("+", ".", StringComparison.OrdinalIgnoreCase); + int value = (int)field.GetRawConstantValue()!; + errorCodes.Add(name, value); + } + + foreach (var nestedType in type.GetNestedTypes()) + { + RecursiveCheckErrorCode(nestedType); + } + } + + RecursiveCheckErrorCode(typeof(ErrorCodes)); + + return errorCodes; + } + + public void Process(DocumentProcessorContext context) + { + StringBuilder description = new StringBuilder(); + description.AppendLine("# Error Codes"); + description.AppendLine("name | value"); + description.AppendLine("---- | -----"); + foreach (var (name, value) in GetAllErrorCodes()) + { + description.AppendLine($"`{name}` | `{value}`"); + } + + context.Document.Info.Description = description.ToString(); + } + } +} -- cgit v1.2.3