Skip to content

Commit

Permalink
Nullable annotations for destructuring policies (serilog#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored and Twinki14 committed Dec 30, 2023
1 parent 3760984 commit bc7dd1f
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Serilog/Context/LogContextEnricher.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using Serilog.Core;
using Serilog.Events;

Expand Down
1 change: 1 addition & 0 deletions src/Serilog/Core/Filters/DelegateFilter.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using Serilog.Events;

Expand Down
4 changes: 3 additions & 1 deletion src/Serilog/Core/IDestructuringPolicy.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.Events;

namespace Serilog.Core
Expand All @@ -29,6 +31,6 @@ public interface IDestructuringPolicy
/// <param name="propertyValueFactory">Recursively apply policies to destructure additional values.</param>
/// <param name="result">The destructured value, or null.</param>
/// <returns>True if the value could be destructured under this policy.</returns>
bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result);
bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result);
}
}
3 changes: 2 additions & 1 deletion src/Serilog/Core/Pipeline/MessageTemplateCache.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using Serilog.Events;

Expand Down Expand Up @@ -52,7 +53,7 @@ public MessageTemplate Parse(string messageTemplate)
#if HASHTABLE
// ReSharper disable once InconsistentlySynchronizedField
// ignored warning because this is by design
var result = (MessageTemplate)_templates[messageTemplate];
var result = (MessageTemplate?)_templates[messageTemplate];
if (result != null)
return result;
#else
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog/Policies/DelegateDestructuringPolicy.cs
Expand Up @@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

namespace Serilog.Policies
{
class DelegateDestructuringPolicy : IDestructuringPolicy
{
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
if (value is Delegate del)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog/Policies/ProjectedDestructuringPolicy.cs
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

Expand All @@ -29,7 +31,7 @@ public ProjectedDestructuringPolicy(Func<Type, bool> canApply, Func<object, obje
_projection = projection ?? throw new ArgumentNullException(nameof(projection));
}

public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
if (value == null) throw new ArgumentNullException(nameof(value));

Expand Down
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Serilog.Core;
using Serilog.Events;
Expand All @@ -21,7 +23,7 @@ namespace Serilog.Policies
{
class ReflectionTypesScalarDestructuringPolicy : IDestructuringPolicy
{
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
// These types and their subclasses are property-laden and deep;
// most sinks will convert them to strings.
Expand Down
3 changes: 2 additions & 1 deletion test/TestDummies/DummyHardCodedStringDestructuringPolicy.cs
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Serilog.Core;
using Serilog.Events;

Expand All @@ -13,7 +14,7 @@ public DummyHardCodedStringDestructuringPolicy(string hardCodedString)
_hardCodedString = hardCodedString ?? throw new ArgumentNullException(nameof(hardCodedString));
}

public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
result = new ScalarValue(_hardCodedString);
return true;
Expand Down

0 comments on commit bc7dd1f

Please sign in to comment.