From fd9780a7db7e9a22a1206af41e29496249efa2b6 Mon Sep 17 00:00:00 2001 From: Nogic <24802730+nogic1008@users.noreply.github.com> Date: Mon, 28 Oct 2019 09:10:45 +0900 Subject: [PATCH 1/5] enable Nullable option at MicroBatchFramework --- src/MicroBatchFramework/MicroBatchFramework.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MicroBatchFramework/MicroBatchFramework.csproj b/src/MicroBatchFramework/MicroBatchFramework.csproj index 0beb66b2..a4ca1252 100644 --- a/src/MicroBatchFramework/MicroBatchFramework.csproj +++ b/src/MicroBatchFramework/MicroBatchFramework.csproj @@ -3,7 +3,7 @@ netstandard2.0 8.0 - warnings + enable true release.snk true From f127c88ebc3e8483e89531f846c9b417ee9f8a0a Mon Sep 17 00:00:00 2001 From: Nogic <24802730+nogic1008@users.noreply.github.com> Date: Mon, 28 Oct 2019 09:45:18 +0900 Subject: [PATCH 2/5] fix Warnings caused by NRT --- src/MicroBatchFramework/BatchBase.cs | 2 +- src/MicroBatchFramework/BatchEngine.cs | 13 +++++++------ .../BatchEngineHostBuilderExtensions.cs | 18 +++++++++--------- src/MicroBatchFramework/BatchEngineService.cs | 6 +++--- src/MicroBatchFramework/CommandAttribute.cs | 4 ++-- .../CompositeBatchInterceptor.cs | 2 +- src/MicroBatchFramework/IBatchInterceptor.cs | 4 ++-- src/MicroBatchFramework/OptionAttribute.cs | 4 ++-- 8 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/MicroBatchFramework/BatchBase.cs b/src/MicroBatchFramework/BatchBase.cs index 44c0efff..ab628e7a 100644 --- a/src/MicroBatchFramework/BatchBase.cs +++ b/src/MicroBatchFramework/BatchBase.cs @@ -2,6 +2,6 @@ { public abstract class BatchBase { - public BatchContext Context { get; set; } + public BatchContext? Context { get; set; } } } diff --git a/src/MicroBatchFramework/BatchEngine.cs b/src/MicroBatchFramework/BatchEngine.cs index 15ac5a1e..b2ad892a 100644 --- a/src/MicroBatchFramework/BatchEngine.cs +++ b/src/MicroBatchFramework/BatchEngine.cs @@ -38,7 +38,7 @@ public async Task RunAsync(Type type, string[] args) logger.LogTrace("BatchEngine.Run Start"); int argsOffset = 0; - MethodInfo method = null; + MethodInfo? method = null; var ctx = new BatchContext(args, DateTime.UtcNow, cancellationToken, logger); try { @@ -57,7 +57,7 @@ public async Task RunAsync(Type type, string[] args) return; } - MethodInfo helpMethod = null; + MethodInfo? helpMethod = null; foreach (var item in methods) { var command = item.GetCustomAttribute(); @@ -118,8 +118,8 @@ public async Task RunAsync(Type type, string[] args) async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string[] args, int argsOffset) { - object instance = null; - object[] invokeArgs = null; + object instance; + object[] invokeArgs; try { @@ -199,7 +199,7 @@ async ValueTask SetFailAsync(BatchContext context, string message, Exception ex) await interceptor.OnBatchRunCompleteAsync(context, message, ex); } - static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int argsOffset, out object[] invokeArgs, out string errorMessage) + static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int argsOffset, out object[] invokeArgs, out string? errorMessage) { var argumentDictionary = ParseArgument(args, argsOffset); invokeArgs = new object[parameters.Length]; @@ -362,7 +362,8 @@ internal static string BuildHelpParameter(MethodInfo[] methods) } else { - sb.Append("-" + option.ShortName.Trim('-') + ", "); + // If Index is -1, ShortName is initialized at Constractor. + sb.Append("-" + option.ShortName!.Trim('-') + ", "); } } diff --git a/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs b/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs index 9511a386..c6392a28 100644 --- a/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs +++ b/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs @@ -13,7 +13,7 @@ public static class BatchEngineHostBuilderExtensions const string ListCommand = "list"; const string HelpCommand = "help"; - public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null) + public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor? interceptor = null) { if (args.Length == 0 || (args.Length == 1 && args[0].Equals(ListCommand, StringComparison.OrdinalIgnoreCase))) { @@ -44,8 +44,8 @@ public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[ return hostBuilder; } - Type type = null; - MethodInfo methodInfo = null; + Type? type = null; + MethodInfo? methodInfo = null; if (args.Length >= 1) { (type, methodInfo) = GetTypeFromAssemblies(args[0]); @@ -77,12 +77,12 @@ public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[ return hostBuilder.UseConsoleLifetime(); } - public static Task RunBatchEngineAsync(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null) + public static Task RunBatchEngineAsync(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor? interceptor = null) { return UseBatchEngine(hostBuilder, args, interceptor).Build().RunAsync(); } - public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null) + public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor? interceptor = null) where T : BatchBase { var method = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); @@ -147,7 +147,7 @@ public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, stri return hostBuilder.UseConsoleLifetime(); } - public static Task RunBatchEngineAsync(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null) + public static Task RunBatchEngineAsync(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor? interceptor = null) where T : BatchBase { return UseBatchEngine(hostBuilder, args, interceptor).Build().RunAsync(); @@ -196,7 +196,7 @@ static List GetBatchTypes() return batchBaseTypes; } - static (Type, MethodInfo) GetTypeFromAssemblies(string arg0) + static (Type?, MethodInfo?) GetTypeFromAssemblies(string arg0) { var batchBaseTypes = GetBatchTypes(); if (batchBaseTypes == null) @@ -205,8 +205,8 @@ static List GetBatchTypes() } var split = arg0.Split('.'); - Type foundType = null; - MethodInfo foundMethod = null; + Type? foundType = null; + MethodInfo? foundMethod = null; foreach (var baseType in batchBaseTypes) { bool isFound = false; diff --git a/src/MicroBatchFramework/BatchEngineService.cs b/src/MicroBatchFramework/BatchEngineService.cs index fac00996..1f2d7c35 100644 --- a/src/MicroBatchFramework/BatchEngineService.cs +++ b/src/MicroBatchFramework/BatchEngineService.cs @@ -12,12 +12,12 @@ public sealed class BatchEngineService : IHostedService { string[] args; Type type; - MethodInfo methodInfo; + MethodInfo? methodInfo; IHostApplicationLifetime appLifetime; ILogger logger; IServiceScope scope; IBatchInterceptor interceptor; - Task runningTask; + Task? runningTask; CancellationTokenSource cancellationTokenSource; public BatchEngineService(IHostApplicationLifetime appLifetime, Type type, string[] args, ILogger logger, IServiceProvider provider) @@ -25,7 +25,7 @@ public BatchEngineService(IHostApplicationLifetime appLifetime, Type type, strin { } - public BatchEngineService(IHostApplicationLifetime appLifetime, Type type, MethodInfo methodInfo, string[] args, ILogger logger, IServiceProvider provider) + public BatchEngineService(IHostApplicationLifetime appLifetime, Type type, MethodInfo? methodInfo, string[] args, ILogger logger, IServiceProvider provider) { this.args = args; this.type = type; diff --git a/src/MicroBatchFramework/CommandAttribute.cs b/src/MicroBatchFramework/CommandAttribute.cs index 1ed5b576..d24513f5 100644 --- a/src/MicroBatchFramework/CommandAttribute.cs +++ b/src/MicroBatchFramework/CommandAttribute.cs @@ -5,7 +5,7 @@ namespace MicroBatchFramework public class CommandAttribute : Attribute { public string[] CommandNames { get; } - public string Description { get; } + public string? Description { get; } public CommandAttribute(string commandName) : this(new[] { commandName }, null) @@ -22,7 +22,7 @@ public CommandAttribute(string[] commandNames) { } - public CommandAttribute(string[] commandNames, string description) + public CommandAttribute(string[] commandNames, string? description) { this.CommandNames = commandNames; this.Description = description; diff --git a/src/MicroBatchFramework/CompositeBatchInterceptor.cs b/src/MicroBatchFramework/CompositeBatchInterceptor.cs index 98cdd728..513f8cb4 100644 --- a/src/MicroBatchFramework/CompositeBatchInterceptor.cs +++ b/src/MicroBatchFramework/CompositeBatchInterceptor.cs @@ -67,7 +67,7 @@ public async ValueTask OnBatchRunBeginAsync(BatchContext context) exceptions.ThrowIfExists(); } - public async ValueTask OnBatchRunCompleteAsync(BatchContext context, string errorMessageIfFailed, Exception exceptionIfExists) + public async ValueTask OnBatchRunCompleteAsync(BatchContext context, string? errorMessageIfFailed, Exception? exceptionIfExists) { var exceptions = new AggregateExceptionHolder(); foreach (var item in interceptors) diff --git a/src/MicroBatchFramework/IBatchInterceptor.cs b/src/MicroBatchFramework/IBatchInterceptor.cs index 33b0be3b..caa6e66d 100644 --- a/src/MicroBatchFramework/IBatchInterceptor.cs +++ b/src/MicroBatchFramework/IBatchInterceptor.cs @@ -24,7 +24,7 @@ public interface IBatchInterceptor /// /// Called when BatchMethod is error or completed. /// - ValueTask OnBatchRunCompleteAsync(BatchContext context, string errorMessageIfFailed, Exception exceptionIfExists); + ValueTask OnBatchRunCompleteAsync(BatchContext context, string? errorMessageIfFailed, Exception? exceptionIfExists); } public class NullBatchInterceptor : IBatchInterceptor @@ -47,7 +47,7 @@ public ValueTask OnBatchRunBeginAsync(BatchContext context) return Empty; } - public ValueTask OnBatchRunCompleteAsync(BatchContext context, string errorMessageIfFailed, Exception exceptionIfExists) + public ValueTask OnBatchRunCompleteAsync(BatchContext context, string? errorMessageIfFailed, Exception? exceptionIfExists) { return Empty; } diff --git a/src/MicroBatchFramework/OptionAttribute.cs b/src/MicroBatchFramework/OptionAttribute.cs index 6cc1cb34..f3e679fa 100644 --- a/src/MicroBatchFramework/OptionAttribute.cs +++ b/src/MicroBatchFramework/OptionAttribute.cs @@ -6,8 +6,8 @@ namespace MicroBatchFramework public class OptionAttribute : Attribute { public int Index { get; } - public string ShortName { get; } - public string Description { get; } + public string? ShortName { get; } + public string? Description { get; } public OptionAttribute(int index) { From 1f0b9af57ccc3c7b1f7ea1a17c49b739ac81b643 Mon Sep 17 00:00:00 2001 From: Nogic <24802730+nogic1008@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:36:32 +0900 Subject: [PATCH 3/5] enable Nullable option at WebHosting --- .../MicroBatchFramework.WebHosting.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj b/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj index 73316d64..e4b6876d 100644 --- a/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj +++ b/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj @@ -3,7 +3,7 @@ netcoreapp3.0 8.0 - warnings + enable From 9a9ecfc5ffe35f8ea78ece5a33d30d29fb3895d1 Mon Sep 17 00:00:00 2001 From: Nogic <24802730+nogic1008@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:37:30 +0900 Subject: [PATCH 4/5] fix Warnings caused by NRT --- .../BatchEngineHostingExtensions.cs | 8 +-- .../BatchEngineMiddleware.cs | 8 +-- .../Swagger/Schemas/SwaggerDocument.cs | 2 +- .../Swagger/SwaggerDefinitionBuilder.cs | 68 +++++++++++-------- .../Swagger/SwaggerOptions.cs | 6 +- src/MicroBatchFramework/BatchContext.cs | 4 +- src/MicroBatchFramework/BatchEngine.cs | 27 +++++--- 7 files changed, 70 insertions(+), 53 deletions(-) diff --git a/src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs b/src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs index 91ee63fa..24ab9f23 100644 --- a/src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs +++ b/src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs @@ -14,7 +14,7 @@ namespace MicroBatchFramework // .WebHosting { public static class BatchEngineHostingExtensions { - public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder builder, IBatchInterceptor interceptor = null) + public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder builder, IBatchInterceptor? interceptor = null) { var batchTypes = CollectBatchTypes(); var target = new TargetBatchTypeCollection(batchTypes); @@ -31,7 +31,7 @@ public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder }); } - public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string urls, SwaggerOptions swaggerOptions = null, IBatchInterceptor interceptor = null) + public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string urls, SwaggerOptions? swaggerOptions = null, IBatchInterceptor? interceptor = null) { return builder .PrepareBatchEngineMiddleware(interceptor) @@ -43,7 +43,7 @@ public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string var entryAsm = Assembly.GetEntryAssembly()!; var xmlName = entryAsm.GetName().Name + ".xml"; var xmlPath = Path.Combine(Path.GetDirectoryName(entryAsm.Location) ?? "", xmlName); - swaggerOptions = new SwaggerOptions(entryAsm.GetName().Name, "", "/") { XmlDocumentPath = xmlPath }; + swaggerOptions = new SwaggerOptions(entryAsm.GetName().Name!, "", "/") { XmlDocumentPath = xmlPath }; } services.AddSingleton(swaggerOptions); }) @@ -105,7 +105,7 @@ static List CollectBatchTypes() if (!(asm.FullName is null) && (asm.FullName.StartsWith("System") || asm.FullName.StartsWith("Microsoft.Extensions"))) continue; - Type[] types; + Type[]? types; try { types = asm.GetTypes(); diff --git a/src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs b/src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs index cb0cb941..3b3b2221 100644 --- a/src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs +++ b/src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs @@ -14,8 +14,8 @@ internal class WebHostingInterceptor : IBatchInterceptor readonly IBatchInterceptor innerInterceptor; public bool CompleteSuccessfully { get; private set; } - public string ErrorMessage { get; private set; } - public Exception Exception { get; private set; } + public string? ErrorMessage { get; private set; } + public Exception? Exception { get; private set; } public WebHostingInterceptor(IBatchInterceptor innerInterceptor) { @@ -37,7 +37,7 @@ public ValueTask OnBatchRunBeginAsync(BatchContext context) return innerInterceptor.OnBatchRunBeginAsync(context); } - public ValueTask OnBatchRunCompleteAsync(BatchContext context, string errorMessageIfFailed, Exception exceptionIfExists) + public ValueTask OnBatchRunCompleteAsync(BatchContext context, string? errorMessageIfFailed, Exception? exceptionIfExists) { this.CompleteSuccessfully = (errorMessageIfFailed == null && exceptionIfExists == null); this.ErrorMessage = errorMessageIfFailed; @@ -115,7 +115,7 @@ public async Task Invoke(HttpContext httpContext) } // create args - string[] args = null; + string?[] args; try { if (httpContext.Request.HasFormContentType) diff --git a/src/MicroBatchFramework.WebHosting/Swagger/Schemas/SwaggerDocument.cs b/src/MicroBatchFramework.WebHosting/Swagger/Schemas/SwaggerDocument.cs index ee80cd06..09883af0 100644 --- a/src/MicroBatchFramework.WebHosting/Swagger/Schemas/SwaggerDocument.cs +++ b/src/MicroBatchFramework.WebHosting/Swagger/Schemas/SwaggerDocument.cs @@ -1,5 +1,5 @@ // This definition is borrowed from Swashbuckle. - +#nullable disable annotations using Newtonsoft.Json; using System.Collections.Generic; diff --git a/src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs b/src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs index e86fd504..5303a765 100644 --- a/src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs +++ b/src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs @@ -20,7 +20,7 @@ public class SwaggerDefinitionBuilder readonly HttpContext httpContext; readonly IEnumerable handlers; - ILookup, XmlCommentStructure> xDocLookup; + ILookup, XmlCommentStructure>? xDocLookup; public SwaggerDefinitionBuilder(SwaggerOptions options, HttpContext httpContext, IEnumerable handlers) { @@ -33,15 +33,13 @@ public byte[] BuildSwaggerJson() { try { - if (options.XmlDocumentPath != null && !File.Exists(options.XmlDocumentPath)) + if (options.XmlDocumentPath != null && File.Exists(options.XmlDocumentPath)) { - xDocLookup = null; + xDocLookup = BuildXmlMemberCommentStructure(options.XmlDocumentPath); } else { - xDocLookup = (options.XmlDocumentPath != null) - ? BuildXmlMemberCommentStructure(options.XmlDocumentPath) - : null; + xDocLookup = null; } var doc = new SwaggerDocument(); @@ -54,7 +52,7 @@ public byte[] BuildSwaggerJson() // tags. var xmlServiceName = (xDocLookup != null) - ? BuildXmlTypeSummary(options.XmlDocumentPath) + ? BuildXmlTypeSummary(options.XmlDocumentPath!) // xDocLookup is not null if XmlDocumentPath is not null. : null; doc.tags = handlers @@ -63,7 +61,7 @@ public byte[] BuildSwaggerJson() .Distinct() .Select(x => { - string desc = null; + string? desc = null; if (xmlServiceName != null) { xmlServiceName.TryGetValue(x, out desc); @@ -80,7 +78,7 @@ public byte[] BuildSwaggerJson() { // MemberInfo.DeclaringType is null only if it is a member of a VB Module. string declaringTypeName = item.DeclaringType!.Name; - XmlCommentStructure xmlComment = null; + XmlCommentStructure? xmlComment = null; if (xDocLookup != null) { // ParameterInfo.Name will be null only it is ReturnParameter. @@ -123,7 +121,7 @@ public byte[] BuildSwaggerJson() } } - Schemas.Parameter[] BuildParameters(IDictionary definitions, XmlCommentStructure xmlComment, MethodInfo method) + Schemas.Parameter[] BuildParameters(IDictionary definitions, XmlCommentStructure? xmlComment, MethodInfo method) { var parameterInfos = method.GetParameters(); var parameters = parameterInfos @@ -132,7 +130,8 @@ Schemas.Parameter[] BuildParameters(IDictionary definitions, Xml var parameterXmlComment = UnwrapTypeName(x.ParameterType); if (xmlComment != null) { - xmlComment.Parameters.TryGetValue(x.Name, out parameterXmlComment!); + // Name is null only if Parameter is ReturnParameter. + xmlComment.Parameters.TryGetValue(x.Name!, out parameterXmlComment!); parameterXmlComment = UnwrapTypeName(x.ParameterType) + " " + parameterXmlComment; } @@ -147,8 +146,8 @@ Schemas.Parameter[] BuildParameters(IDictionary definitions, Xml ? new PartialSchema { type = ToSwaggerDataType(collectionType) } : null; - string defaultObjectExample = null; - object[] enums = null; + string? defaultObjectExample = null; + object[]? enums = null; if (x.ParameterType.GetTypeInfo().IsEnum || (collectionType != null && collectionType.GetTypeInfo().IsEnum)) { // Compiler cannot understand collectionType is not null. @@ -169,7 +168,7 @@ Schemas.Parameter[] BuildParameters(IDictionary definitions, Xml } var swaggerDataType = ToSwaggerDataType(x.ParameterType); - Schema refSchema = null; + Schema? refSchema = null; if (swaggerDataType == "object") { BuildSchema(definitions, x.ParameterType); @@ -203,7 +202,7 @@ string BuildSchema(IDictionary definitions, Type type) var fullName = type.FullName; if (fullName == null) return ""; // safety(TODO:IDictionary<> is not supported) - Schema schema; + Schema? schema; if (definitions.TryGetValue(fullName, out schema)) return "#/definitions/" + fullName; var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); @@ -229,10 +228,11 @@ string BuildSchema(IDictionary definitions, Type type) } else { - Schema items = null; + Schema? items = null; if (swaggerDataType == "array") { - var collectionType = GetCollectionType(memberType); + // If swaggerDataType is array, it will be Collection. + Type collectionType = GetCollectionType(memberType)!; var dataType = ToSwaggerDataType(collectionType); if (dataType == "object") { @@ -258,7 +258,7 @@ string BuildSchema(IDictionary definitions, Type type) } } - IList schemaEnum = null; + IList? schemaEnum = null; if (memberType.GetTypeInfo().IsEnum) { schemaEnum = Enum.GetNames(memberType); @@ -298,7 +298,7 @@ static Type GetMemberType(MemberInfo memberInfo) throw new Exception(); } - static Type GetCollectionType(Type type) + static Type? GetCollectionType(Type type) { if (type.IsArray) return type.GetElementType(); @@ -338,14 +338,14 @@ static ILookup, XmlCommentStructure> BuildXmlMemberComment .ToDictionary(e => e.Item1, e => e.Item2.Value.Trim()); return new XmlCommentStructure - { - ClassName = match.Groups[1].Value, - MethodName = match.Groups[2].Value, - Summary = summary.Trim(), - Remarks = remarks.Trim(), - Parameters = parameters, - Returns = returns.Trim() - }; + ( + className: match.Groups[1].Value, + methodName: match.Groups[2].Value, + summary: summary.Trim(), + remarks: remarks.Trim(), + parameters: parameters, + returns: returns.Trim() + ); }) .ToLookup(x => Tuple.Create(x.ClassName, x.MethodName)); @@ -422,7 +422,7 @@ static string UnwrapTypeName(Type t) return Regex.Replace(t.GetGenericTypeDefinition().Name, @"`.+$", "") + "<" + innerFormat + ">"; } - class Item1EqualityCompaerer : EqualityComparer> + class Item1EqualityCompaerer : EqualityComparer> where T1 : class { public override bool Equals(Tuple x, Tuple y) { @@ -443,6 +443,16 @@ class XmlCommentStructure public string Remarks { get; set; } public Dictionary Parameters { get; set; } public string Returns { get; set; } + + public XmlCommentStructure(string className, string methodName, string summary, string remarks, Dictionary parameters, string returns) + { + ClassName = className; + MethodName = methodName; + Summary = summary; + Remarks = remarks; + Parameters = parameters; + Returns = returns; + } } } @@ -459,7 +469,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ { property.ShouldSerialize = instance => { - IEnumerable enumerable = null; + IEnumerable? enumerable = null; // this value could be in a public field or public property switch (member.MemberType) diff --git a/src/MicroBatchFramework.WebHosting/Swagger/SwaggerOptions.cs b/src/MicroBatchFramework.WebHosting/Swagger/SwaggerOptions.cs index b32ef3ad..6b3ec2d6 100644 --- a/src/MicroBatchFramework.WebHosting/Swagger/SwaggerOptions.cs +++ b/src/MicroBatchFramework.WebHosting/Swagger/SwaggerOptions.cs @@ -12,9 +12,9 @@ public class SwaggerOptions /// /// (FilePath, LoadedEmbeddedBytes) => CustomBytes) /// - public Func ResolveCustomResource { get; set; } - public Func CustomHost { get; set; } - public string XmlDocumentPath { get; set; } + public Func? ResolveCustomResource { get; set; } + public Func? CustomHost { get; set; } + public string? XmlDocumentPath { get; set; } public string JsonName { get; set; } public string[] ForceSchemas { get; set; } diff --git a/src/MicroBatchFramework/BatchContext.cs b/src/MicroBatchFramework/BatchContext.cs index 5d284bb0..62f853c6 100644 --- a/src/MicroBatchFramework/BatchContext.cs +++ b/src/MicroBatchFramework/BatchContext.cs @@ -6,12 +6,12 @@ namespace MicroBatchFramework { public class BatchContext { - public string[] Arguments { get; private set; } + public string?[] Arguments { get; private set; } public DateTime Timestamp { get; private set; } public CancellationToken CancellationToken { get; private set; } public ILogger Logger { get; private set; } - public BatchContext(string[] arguments, DateTime timestamp, CancellationToken cancellationToken, ILogger logger) + public BatchContext(string?[] arguments, DateTime timestamp, CancellationToken cancellationToken, ILogger logger) { Arguments = arguments; Timestamp = timestamp; diff --git a/src/MicroBatchFramework/BatchEngine.cs b/src/MicroBatchFramework/BatchEngine.cs index b2ad892a..e8aae4e0 100644 --- a/src/MicroBatchFramework/BatchEngine.cs +++ b/src/MicroBatchFramework/BatchEngine.cs @@ -26,7 +26,7 @@ public BatchEngine(ILogger logger, IServiceProvider provider, IBatc this.cancellationToken = cancellationToken; } - public async Task RunAsync(Type type, MethodInfo method, string[] args) + public async Task RunAsync(Type type, MethodInfo method, string?[] args) { logger.LogTrace("BatchEngine.Run Start"); var ctx = new BatchContext(args, DateTime.UtcNow, cancellationToken, logger); @@ -116,7 +116,7 @@ public async Task RunAsync(Type type, string[] args) await RunCore(ctx, type, method, args, argsOffset); } - async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string[] args, int argsOffset) + async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string?[] args, int argsOffset) { object instance; object[] invokeArgs; @@ -199,7 +199,7 @@ async ValueTask SetFailAsync(BatchContext context, string message, Exception ex) await interceptor.OnBatchRunCompleteAsync(context, message, ex); } - static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int argsOffset, out object[] invokeArgs, out string? errorMessage) + static bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsOffset, out object[] invokeArgs, out string? errorMessage) { var argumentDictionary = ParseArgument(args, argsOffset); invokeArgs = new object[parameters.Length]; @@ -294,22 +294,29 @@ static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int return true; } - static ReadOnlyDictionary ParseArgument(string[] args, int argsOffset) + static ReadOnlyDictionary ParseArgument(string?[] args, int argsOffset) { var dict = new Dictionary(args.Length, StringComparer.OrdinalIgnoreCase); for (int i = argsOffset; i < args.Length;) { - if (!args[i].StartsWith("-")) + var key = args[i++]; + if (key is null || !key.StartsWith("-")) { - i++; continue; // not key } - var key = args[i++].TrimStart('-'); - if (i < args.Length && !args[i].StartsWith("-")) + key = key.TrimStart('-'); + if (i >= args.Length) + { + dict.Add(key, new OptionParameter { BooleanSwitch = true }); // Last parameter + break; + } + + var value = args[i]; + if (value != null && !value.StartsWith("-")) { - var value = args[i++]; dict.Add(key, new OptionParameter { Value = value }); + i++; } else { @@ -322,7 +329,7 @@ static ReadOnlyDictionary ParseArgument(string[] args, struct OptionParameter { - public string Value; + public string? Value; public bool BooleanSwitch; } From 040d11e123f1d9b12e5e50bc6ed32e6ad0894125 Mon Sep 17 00:00:00 2001 From: Nogic <24802730+nogic1008@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:52:47 +0900 Subject: [PATCH 5/5] disable warn at BatchBase.Context --- src/MicroBatchFramework/BatchBase.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/MicroBatchFramework/BatchBase.cs b/src/MicroBatchFramework/BatchBase.cs index ab628e7a..b7453b10 100644 --- a/src/MicroBatchFramework/BatchBase.cs +++ b/src/MicroBatchFramework/BatchBase.cs @@ -2,6 +2,10 @@ { public abstract class BatchBase { - public BatchContext? Context { get; set; } + // Context will be set non-null value by BatchEngine, + // but it might be null because it has public setter. + #nullable disable warnings + public BatchContext Context { get; set; } + #nullable restore warnings } }