Skip to content

Commit

Permalink
Enable nullable reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang authored and ragnarstolsmark committed Oct 18, 2023
1 parent 333baa3 commit 3dbb512
Show file tree
Hide file tree
Showing 31 changed files with 102 additions and 103 deletions.
5 changes: 1 addition & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
<PropertyGroup>
<WarningsAsErrors>true</WarningsAsErrors>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Polyfill" Version="1.29.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
30 changes: 14 additions & 16 deletions src/Hangfire.Console/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static void ResetTextColor(this PerformContext context)
/// <param name="context">Context</param>
/// <param name="value">Initial value</param>
/// <param name="color">Progress bar color</param>
public static IProgressBar WriteProgressBar(this PerformContext context, int value = 0, ConsoleTextColor color = null) => ConsoleContext.FromPerformContext(context)?.WriteProgressBar(null, value, color) ?? new NoOpProgressBar();
public static IProgressBar WriteProgressBar(this PerformContext context, int value = 0, ConsoleTextColor? color = null)
=> ConsoleContext.FromPerformContext(context)?.WriteProgressBar(null, value, color) ?? new NoOpProgressBar();

/// <summary>
/// Adds an updateable named progress bar to console.
Expand All @@ -63,28 +64,25 @@ public static void ResetTextColor(this PerformContext context)
/// <param name="name">Name</param>
/// <param name="value">Initial value</param>
/// <param name="color">Progress bar color</param>
public static IProgressBar WriteProgressBar(this PerformContext context, string name, double value = 0, ConsoleTextColor color = null) => ConsoleContext.FromPerformContext(context)?.WriteProgressBar(name, value, color) ?? new NoOpProgressBar();
public static IProgressBar WriteProgressBar(this PerformContext context, string name, double value = 0, ConsoleTextColor? color = null)
=> ConsoleContext.FromPerformContext(context)?.WriteProgressBar(name, value, color) ?? new NoOpProgressBar();

/// <summary>
/// Adds a string to console.
/// </summary>
/// <param name="context">Context</param>
/// <param name="value">String</param>
public static void WriteLine(this PerformContext context, string value)
{
ConsoleContext.FromPerformContext(context)?.WriteLine(value, null);
}
public static void WriteLine(this PerformContext context, string? value)
=> ConsoleContext.FromPerformContext(context)?.WriteLine(value, null);

/// <summary>
/// Adds a string to console.
/// </summary>
/// <param name="context">Context</param>
/// <param name="color">Text color</param>
/// <param name="value">String</param>
public static void WriteLine(this PerformContext context, ConsoleTextColor color, string value)
{
ConsoleContext.FromPerformContext(context)?.WriteLine(value, color);
}
public static void WriteLine(this PerformContext context, ConsoleTextColor? color, string? value)
=> ConsoleContext.FromPerformContext(context)?.WriteLine(value, color);

/// <summary>
/// Adds an empty line to console.
Expand All @@ -98,7 +96,7 @@ public static void WriteLine(this PerformContext context)
/// </summary>
/// <param name="context">Context</param>
/// <param name="value">Value</param>
public static void WriteLine(this PerformContext context, object value)
public static void WriteLine(this PerformContext context, object? value)
=> WriteLine(context, value?.ToString());

/// <summary>
Expand Down Expand Up @@ -150,7 +148,7 @@ public static void WriteLine(this PerformContext context, string format, params
/// <param name="context">Context</param>
/// <param name="color">Text color</param>
/// <param name="value">Value</param>
public static void WriteLine(this PerformContext context, ConsoleTextColor color, object value)
public static void WriteLine(this PerformContext context, ConsoleTextColor? color, object? value)
=> WriteLine(context, color, value?.ToString());

/// <summary>
Expand All @@ -161,7 +159,7 @@ public static void WriteLine(this PerformContext context, ConsoleTextColor color
/// <param name="format">Format string</param>
/// <param name="arg0">Argument</param>
[StringFormatMethod("format")]
public static void WriteLine(this PerformContext context, ConsoleTextColor color, string format, object arg0)
public static void WriteLine(this PerformContext context, ConsoleTextColor? color, string format, object arg0)
=> WriteLine(context, color, string.Format(format, arg0));

/// <summary>
Expand All @@ -173,7 +171,7 @@ public static void WriteLine(this PerformContext context, ConsoleTextColor color
/// <param name="arg0">Argument</param>
/// <param name="arg1">Argument</param>
[StringFormatMethod("format")]
public static void WriteLine(this PerformContext context, ConsoleTextColor color, string format, object arg0, object arg1)
public static void WriteLine(this PerformContext context, ConsoleTextColor? color, string format, object arg0, object arg1)
=> WriteLine(context, color, string.Format(format, arg0, arg1));

/// <summary>
Expand All @@ -186,7 +184,7 @@ public static void WriteLine(this PerformContext context, ConsoleTextColor color
/// <param name="arg1">Argument</param>
/// <param name="arg2">Argument</param>
[StringFormatMethod("format")]
public static void WriteLine(this PerformContext context, ConsoleTextColor color, string format, object arg0, object arg1, object arg2)
public static void WriteLine(this PerformContext context, ConsoleTextColor? color, string format, object arg0, object arg1, object arg2)
=> WriteLine(context, color, string.Format(format, arg0, arg1, arg2));

/// <summary>
Expand All @@ -197,6 +195,6 @@ public static void WriteLine(this PerformContext context, ConsoleTextColor color
/// <param name="format">Format string</param>
/// <param name="args">Arguments</param>
[StringFormatMethod("format")]
public static void WriteLine(this PerformContext context, ConsoleTextColor color, string format, params object[] args)
public static void WriteLine(this PerformContext context, ConsoleTextColor? color, string format, params object[] args)
=> WriteLine(context, color, string.Format(format, args));
}
2 changes: 1 addition & 1 deletion src/Hangfire.Console/ConsoleTextColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ private ConsoleTextColor(string color)
/// <summary>
/// Implicitly converts <see cref="ConsoleTextColor" /> to <see cref="string" />.
/// </summary>
public static implicit operator string(ConsoleTextColor color) => color?._color;
public static implicit operator string?(ConsoleTextColor? color) => color?._color;
}
8 changes: 4 additions & 4 deletions src/Hangfire.Console/Dashboard/ConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void RenderLine(StringBuilder builder, ConsoleLine line, DateTime

if (isProgressBar)
{
builder.AppendFormat(CultureInfo.InvariantCulture, "<div class=\"pv\" style=\"width:{0:0.#}%\" data-value=\"{0:f0}\"></div>", line.ProgressValue.Value);
builder.AppendFormat(CultureInfo.InvariantCulture, "<div class=\"pv\" style=\"width:{0:0.#}%\" data-value=\"{0:f0}\"></div>", line.ProgressValue!.Value);
}
else
{
Expand All @@ -120,7 +120,7 @@ public static void RenderLine(StringBuilder builder, ConsoleLine line, DateTime
/// <param name="builder">Buffer</param>
/// <param name="lines">Lines</param>
/// <param name="timestamp">Reference timestamp for time offset</param>
public static void RenderLines(StringBuilder builder, IEnumerable<ConsoleLine> lines, DateTime timestamp)
public static void RenderLines(StringBuilder builder, IEnumerable<ConsoleLine>? lines, DateTime timestamp)
{
if (builder == null)
{
Expand Down Expand Up @@ -179,7 +179,7 @@ public static void RenderLineBuffer(StringBuilder builder, IConsoleStorage stora
/// On completion, <paramref name="start" /> is set to the end of the current batch,
/// and can be used for next requests (or set to -1, if the job has finished processing).
/// </remarks>
private static IEnumerable<ConsoleLine> ReadLines(IConsoleStorage storage, ConsoleId consoleId, ref int start)
private static IEnumerable<ConsoleLine>? ReadLines(IConsoleStorage storage, ConsoleId consoleId, ref int start)
{
if (start < 0)
{
Expand All @@ -193,7 +193,7 @@ private static IEnumerable<ConsoleLine> ReadLines(IConsoleStorage storage, Conso
{
// has some new items to fetch

Dictionary<string, ConsoleLine> progressBars = null;
Dictionary<string, ConsoleLine>? progressBars = null;

foreach (var entry in storage.GetLines(consoleId, start, count - 1))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Dashboard/DynamicCssDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class DynamicCssDispatcher : IDashboardDispatcher
{
private readonly ConsoleOptions _options;

public DynamicCssDispatcher(ConsoleOptions options)
public DynamicCssDispatcher(ConsoleOptions? options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Dashboard/DynamicJsDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class DynamicJsDispatcher : IDashboardDispatcher
{
private readonly ConsoleOptions _options;

public DynamicJsDispatcher(ConsoleOptions options)
public DynamicJsDispatcher(ConsoleOptions? options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Dashboard/JobProgressDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class JobProgressDispatcher : IDashboardDispatcher
// ReSharper disable once NotAccessedField.Local
private readonly ConsoleOptions _options;

public JobProgressDispatcher(ConsoleOptions options)
public JobProgressDispatcher(ConsoleOptions? options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Dashboard/ProcessingStateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class ProcessingStateRenderer
// ReSharper disable once NotAccessedField.Local
private readonly ConsoleOptions _options;

public ProcessingStateRenderer(ConsoleOptions options)
public ProcessingStateRenderer(ConsoleOptions? options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
8 changes: 4 additions & 4 deletions src/Hangfire.Console/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="context">Perform context</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, ConsoleTextColor color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(0, color), count);
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(0, color), count);

/// <summary>
/// Returns ab <see cref="IEnumerable" /> reporting enumeration progress.
Expand All @@ -75,7 +75,7 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="context">Perform context</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, ConsoleTextColor color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(0, color), count);
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(0, color), count);

/// <summary>
/// Returns an <see cref="IEnumerable{T}" /> reporting enumeration progress.
Expand All @@ -86,7 +86,7 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="name">Progress bar name</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, string name, ConsoleTextColor color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color), count);
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, PerformContext context, string name, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color), count);

/// <summary>
/// Returns ab <see cref="IEnumerable" /> reporting enumeration progress.
Expand All @@ -96,5 +96,5 @@ public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar
/// <param name="name">Progress bar name</param>
/// <param name="color">Progress bar color</param>
/// <param name="count">Item count</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, string name, ConsoleTextColor color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color), count);
public static IEnumerable WithProgress(this IEnumerable enumerable, PerformContext context, string name, ConsoleTextColor? color = null, int count = -1) => WithProgress(enumerable, context.WriteProgressBar(name, 0, color), count);
}
2 changes: 1 addition & 1 deletion src/Hangfire.Console/GlobalConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class GlobalConfigurationExtensions
/// </summary>
/// <param name="configuration">Global configuration</param>
/// <param name="options">Options for console</param>
public static IGlobalConfiguration UseConsole(this IGlobalConfiguration configuration, ConsoleOptions options = null)
public static IGlobalConfiguration UseConsole(this IGlobalConfiguration configuration, ConsoleOptions? options = null)
{
if (configuration == null)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Hangfire.Console/Hangfire.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,9 @@
<ItemGroup>
<PackageReference Include="Hangfire.Core" Version="1.8.5"/>
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" PrivateAssets="All" />
<PackageReference Include="PolySharp" Version="1.13.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Monitoring/ConsoleApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IList<LineDto> GetLines(string jobId, DateTime timestamp, LineType type =

if (count > 0)
{
Dictionary<string, ProgressBarDto> progressBars = null;
Dictionary<string, ProgressBarDto>? progressBars = null;

foreach (var entry in _storage.GetLines(consoleId, 0, count))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Monitoring/LineDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ internal LineDto(ConsoleLine line, DateTime referenceTimestamp)
/// <summary>
/// Returns HTML color for the console line
/// </summary>
public string Color { get; internal set; }
public string? Color { get; internal set; }
}
6 changes: 2 additions & 4 deletions src/Hangfire.Console/Monitoring/ProgressBarDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ internal ProgressBarDto(ConsoleLine line, DateTime referenceTimestamp) : base(li
{
Id = int.Parse(line.Message, CultureInfo.InvariantCulture);
Name = line.ProgressName;

// ReSharper disable once PossibleInvalidOperationException
Progress = line.ProgressValue.Value;
Progress = line.ProgressValue!.Value;
}

/// <inheritdoc />
Expand All @@ -31,7 +29,7 @@ internal ProgressBarDto(ConsoleLine line, DateTime referenceTimestamp) : base(li
/// <summary>
/// Returns optional name for a progress bar
/// </summary>
public string Name { get; }
public string? Name { get; }

/// <summary>
/// Returns progress value for a progress bar
Expand Down
6 changes: 3 additions & 3 deletions src/Hangfire.Console/Progress/DefaultProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ internal class DefaultProgressBar : IProgressBar

private readonly string _progressBarId;

private string _color;
private string? _color;

private string _name;
private string? _name;

private double _value;

internal DefaultProgressBar(ConsoleContext context, string progressBarId, string name, string color)
internal DefaultProgressBar(ConsoleContext context, string progressBarId, string? name, string? color)
{
if (string.IsNullOrEmpty(progressBarId))
{
Expand Down
6 changes: 3 additions & 3 deletions src/Hangfire.Console/Serialization/ConsoleId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class ConsoleId : IEquatable<ConsoleId>
{
private static readonly DateTime UnixEpoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private string _cachedString;
private string? _cachedString;

/// <summary>
/// Initializes an instance of <see cref="ConsoleId" />
Expand Down Expand Up @@ -59,7 +59,7 @@ private ConsoleId(string jobId, long timestamp)
public DateTime DateValue => UnixEpoch.AddMilliseconds(Timestamp);

/// <inheritdoc />
public bool Equals(ConsoleId other)
public bool Equals(ConsoleId? other)
{
if (ReferenceEquals(other, null))
{
Expand Down Expand Up @@ -133,7 +133,7 @@ public override string ToString()
}

/// <inheritdoc />
public override bool Equals(object obj) => Equals(obj as ConsoleId);
public override bool Equals(object? obj) => Equals(obj as ConsoleId);

/// <inheritdoc />
public override int GetHashCode() => (JobId.GetHashCode() * 17) ^ Timestamp.GetHashCode();
Expand Down
6 changes: 3 additions & 3 deletions src/Hangfire.Console/Serialization/ConsoleLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ internal class ConsoleLine
/// Message text, or message reference, or progress bar id
/// </summary>
[JsonProperty("s", Required = Required.Always)]
public string Message { get; set; }
public string Message { get; set; } = null!;

/// <summary>
/// Text color for this message
/// </summary>
[JsonProperty("c", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string TextColor { get; set; }
public string? TextColor { get; set; }

/// <summary>
/// Value update for a progress bar
Expand All @@ -38,5 +38,5 @@ internal class ConsoleLine
/// Optional name for a progress bar
/// </summary>
[JsonProperty("n", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ProgressName { get; set; }
public string? ProgressName { get; set; }
}
10 changes: 5 additions & 5 deletions src/Hangfire.Console/Server/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public ConsoleContext(ConsoleId consoleId, IConsoleStorage storage)
_storage.InitConsole(_consoleId);
}

public ConsoleTextColor TextColor { get; set; }
public ConsoleTextColor? TextColor { get; set; }

public static ConsoleContext FromPerformContext(PerformContext context)
public static ConsoleContext? FromPerformContext(PerformContext? context)
{
if (context == null)
if (context is null)
{
// PerformContext might be null because of refactoring, or during tests
return null;
Expand Down Expand Up @@ -71,12 +71,12 @@ public void AddLine(ConsoleLine line)
}
}

public void WriteLine(string value, ConsoleTextColor color)
public void WriteLine(string? value, ConsoleTextColor? color)
{
AddLine(new ConsoleLine { Message = value ?? "", TextColor = color ?? TextColor });
}

public IProgressBar WriteProgressBar(string name, double value, ConsoleTextColor color)
public IProgressBar WriteProgressBar(string? name, double value, ConsoleTextColor? color)
{
var progressBarId = Interlocked.Increment(ref _nextProgressBarId);

Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/Server/ConsoleServerFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class ConsoleServerFilter : IServerFilter
{
private readonly ConsoleOptions _options;

public ConsoleServerFilter(ConsoleOptions options)
public ConsoleServerFilter(ConsoleOptions? options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Console/States/ConsoleApplyStateFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class ConsoleApplyStateFilter : IApplyStateFilter
{
private readonly ConsoleOptions _options;

public ConsoleApplyStateFilter(ConsoleOptions options)
public ConsoleApplyStateFilter(ConsoleOptions? options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
Loading

0 comments on commit 3dbb512

Please sign in to comment.