From 650af7ef1ac1957919ab19ec3d06af97792c54f8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 9 Apr 2022 13:59:56 -0400 Subject: enable nullable annotations in log parser (#837) --- src/SMAPI.Web/ViewModels/LogParserModel.cs | 51 +++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) (limited to 'src/SMAPI.Web/ViewModels') diff --git a/src/SMAPI.Web/ViewModels/LogParserModel.cs b/src/SMAPI.Web/ViewModels/LogParserModel.cs index c768a08c..d7e4d810 100644 --- a/src/SMAPI.Web/ViewModels/LogParserModel.cs +++ b/src/SMAPI.Web/ViewModels/LogParserModel.cs @@ -1,9 +1,9 @@ -#nullable disable - using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text.RegularExpressions; +using Newtonsoft.Json; using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Web.Framework.LogParsing.Models; @@ -23,40 +23,41 @@ namespace StardewModdingAPI.Web.ViewModels ** Accessors *********/ /// The paste ID. - public string PasteID { get; set; } + public string? PasteID { get; } /// The viewer's detected OS, if known. - public Platform? DetectedPlatform { get; set; } + public Platform? DetectedPlatform { get; } /// The parsed log info. - public ParsedLog ParsedLog { get; set; } + public ParsedLog? ParsedLog { get; private set; } /// Whether to show the raw unparsed log. - public bool ShowRaw { get; set; } + public bool ShowRaw { get; private set; } /// A non-blocking warning while uploading the log. - public string UploadWarning { get; set; } + public string? UploadWarning { get; set; } /// An error which occurred while uploading the log. - public string UploadError { get; set; } + public string? UploadError { get; set; } /// An error which occurred while parsing the log file. - public string ParseError => this.ParsedLog?.Error; + public string? ParseError => this.ParsedLog?.Error; /// When the uploaded file will no longer be available. public DateTime? Expiry { get; set; } + /// Whether parsed log data is available. + [MemberNotNullWhen(true, nameof(LogParserModel.PasteID), nameof(LogParserModel.ParsedLog))] + public bool HasLog => this.ParsedLog != null; + /********* ** Public methods *********/ - /// Construct an instance. - public LogParserModel() { } - /// Construct an instance. /// The paste ID. /// The viewer's detected OS, if known. - public LogParserModel(string pasteID, Platform? platform) + public LogParserModel(string? pasteID, Platform? platform) { this.PasteID = pasteID; this.DetectedPlatform = platform; @@ -64,6 +65,26 @@ namespace StardewModdingAPI.Web.ViewModels this.ShowRaw = false; } + /// Construct an instance. + /// The paste ID. + /// The viewer's detected OS, if known. + /// The parsed log info. + /// Whether to show the raw unparsed log. + /// A non-blocking warning while uploading the log. + /// An error which occurred while uploading the log. + /// When the uploaded file will no longer be available. + [JsonConstructor] + public LogParserModel(string pasteId, Platform? detectedPlatform, ParsedLog? parsedLog, bool showRaw, string? uploadWarning, string? uploadError, DateTime? expiry) + { + this.PasteID = pasteId; + this.DetectedPlatform = detectedPlatform; + this.ParsedLog = parsedLog; + this.ShowRaw = showRaw; + this.UploadWarning = uploadWarning; + this.UploadError = uploadError; + this.Expiry = expiry; + } + /// Set the log parser result. /// The parsed log info. /// Whether to show the raw unparsed log. @@ -79,14 +100,14 @@ namespace StardewModdingAPI.Web.ViewModels public IDictionary GetContentPacksByMod() { // get all mods & content packs - LogModInfo[] mods = this.ParsedLog?.Mods; + LogModInfo[]? mods = this.ParsedLog?.Mods; if (mods == null || !mods.Any()) return new Dictionary(); // group by mod return mods .Where(mod => mod.IsContentPack) - .GroupBy(mod => mod.ContentPackFor) + .GroupBy(mod => mod.ContentPackFor!) .ToDictionary(group => group.Key, group => group.ToArray()); } -- cgit