Skip to content

Commit

Permalink
Ensure non-nullability of FormattableLogMessage.ArgNames and ArgValue…
Browse files Browse the repository at this point in the history
…s for all cases (including default instances)
  • Loading branch information
adams85 committed Nov 24, 2023
1 parent 262f33b commit 45f3c6a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/ConfigCatClient/Logging/FormattableLogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ internal static FormattableLogMessage FromInterpolated(FormattableString message
message.GetArguments() ?? ArrayUtils.EmptyArray<object?>());
}

private readonly string? format;
private readonly string[]? argNames;
private readonly object?[]? argValues;
private string? invariantFormattedMessage;

/// <summary>
/// Initializes a new instance of the <see cref="FormattableLogMessage"/> struct from a plain log message.
/// </summary>
public FormattableLogMessage(string message)
{
this.invariantFormattedMessage = message ?? throw new ArgumentNullException(nameof(message));
this.format = null;
ArgNames = ArrayUtils.EmptyArray<string>();
ArgValues = ArrayUtils.EmptyArray<object?>();
this.argNames = null;
this.argValues = null;
}

/// <summary>
Expand All @@ -38,16 +43,15 @@ public FormattableLogMessage(string message)
public FormattableLogMessage(string format, string[] argNames, object?[] argValues)
{
this.format = format ?? throw new ArgumentNullException(nameof(format));
ArgNames = argNames ?? throw new ArgumentNullException(nameof(argNames));
ArgValues = argValues ?? throw new ArgumentNullException(nameof(argValues));
this.argNames = argNames ?? throw new ArgumentNullException(nameof(argNames));
this.argValues = argValues ?? throw new ArgumentNullException(nameof(argValues));
if (argNames.Length != argValues.Length)
{
throw new ArgumentException($"Number of argument names ({argNames.Length}) and argument values ({argValues.Length}) mismatch.", nameof(argNames));
}
this.invariantFormattedMessage = null;
}

private readonly string? format;
/// <summary>
/// Log message format.
/// </summary>
Expand All @@ -56,14 +60,13 @@ public FormattableLogMessage(string format, string[] argNames, object?[] argValu
/// <summary>
/// Names of the named arguments.
/// </summary>
public readonly string[] ArgNames { get; }
public readonly string[] ArgNames => this.argNames ?? ArrayUtils.EmptyArray<string>();

/// <summary>
/// Values of the named arguments.
/// </summary>
public readonly object?[] ArgValues { get; }
public readonly object?[] ArgValues => this.argValues ?? ArrayUtils.EmptyArray<object?>();

private string? invariantFormattedMessage;
/// <summary>
/// The log message formatted using <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
Expand Down

0 comments on commit 45f3c6a

Please sign in to comment.