Skip to content

Commit

Permalink
out ScalarValue can be null (#1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jul 29, 2022
1 parent 1123784 commit 1c926f0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/Serilog/Core/IScalarConversionPolicy.cs
@@ -1,4 +1,6 @@
using Serilog.Events;
#nullable enable
using System.Diagnostics.CodeAnalysis;
using Serilog.Events;

namespace Serilog.Core
{
Expand All @@ -14,6 +16,6 @@ interface IScalarConversionPolicy
/// <param name="value">The value to convert.</param>
/// <param name="result">The converted value, or null.</param>
/// <returns>True if the value could be converted under this policy.</returns>
bool TryConvertToScalar(object value, out ScalarValue result);
bool TryConvertToScalar(object value, [NotNullWhen(true)] out ScalarValue? result);
}
}
}
7 changes: 4 additions & 3 deletions src/Serilog/Policies/ByteArrayScalarConversionPolicy.cs
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Serilog.Core;
using Serilog.Events;
Expand All @@ -25,10 +27,9 @@ class ByteArrayScalarConversionPolicy : IScalarConversionPolicy
{
const int MaximumByteArrayLength = 1024;

public bool TryConvertToScalar(object value, out ScalarValue result)
public bool TryConvertToScalar(object value, [NotNullWhen(true)] out ScalarValue? result)
{
var bytes = value as byte[];
if (bytes == null)
if (value is not byte[] bytes)
{
result = null;
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/Serilog/Policies/ByteMemoryScalarConversionPolicy.cs
Expand Up @@ -14,8 +14,9 @@

#if FEATURE_SPAN

#nullable enable
using System;

using System.Diagnostics.CodeAnalysis;
using Serilog.Core;
using Serilog.Events;

Expand All @@ -29,7 +30,7 @@ sealed class ByteMemoryScalarConversionPolicy : IScalarConversionPolicy
const int MaximumByteArrayLength = 1024;
const int MaxTake = 16;

public bool TryConvertToScalar(object value, out ScalarValue result)
public bool TryConvertToScalar(object value, [NotNullWhen(true)] out ScalarValue? result)
{
if (value is ReadOnlyMemory<byte> x)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog/Policies/EnumScalarConversionPolicy.cs
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System.Diagnostics.CodeAnalysis;
using Serilog.Core;
using Serilog.Events;
using System.Reflection;
Expand All @@ -20,7 +22,7 @@ namespace Serilog.Policies
{
class EnumScalarConversionPolicy : IScalarConversionPolicy
{
public bool TryConvertToScalar(object value, out ScalarValue result)
public bool TryConvertToScalar(object value, [NotNullWhen(true)] out ScalarValue? result)
{
if (value.GetType().GetTypeInfo().IsEnum)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog/Policies/SimpleScalarConversionPolicy.cs
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Serilog.Core;
using Serilog.Events;

Expand All @@ -28,7 +30,7 @@ public SimpleScalarConversionPolicy(IEnumerable<Type> scalarTypes)
_scalarTypes = new(scalarTypes);
}

public bool TryConvertToScalar(object value, out ScalarValue result)
public bool TryConvertToScalar(object value, [NotNullWhen(true)] out ScalarValue? result)
{
if (_scalarTypes.Contains(value.GetType()))
{
Expand Down

0 comments on commit 1c926f0

Please sign in to comment.