Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out ScalarValue can be null #1716

Merged
merged 1 commit into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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