Skip to content

Commit

Permalink
Convert codebase to use C# 8 nullability
Browse files Browse the repository at this point in the history
Thanks to @Brar for considerable work on the feature!

Closes #2304
  • Loading branch information
roji committed May 5, 2019
1 parent d922b0a commit f2dd3f7
Show file tree
Hide file tree
Showing 194 changed files with 1,942 additions and 1,810 deletions.
1 change: 1 addition & 0 deletions .appveyor.yml
Expand Up @@ -29,6 +29,7 @@ before_build:
build_script: build_script:
- dotnet build "test\Npgsql.Tests" -c Debug - dotnet build "test\Npgsql.Tests" -c Debug
- dotnet build "test\Npgsql.PluginTests" -c Debug - dotnet build "test\Npgsql.PluginTests" -c Debug
- dotnet build "src\Npgsql" -c Release
- msbuild src\VSIX\VSIX.csproj /p:Configuration=Release /v:Minimal - msbuild src\VSIX\VSIX.csproj /p:Configuration=Release /v:Minimal
# WIX hasn't been released yet for VS 2019 # WIX hasn't been released yet for VS 2019
# - msbuild src\MSI\MSI.wixproj /p:Configuration=Release /v:Minimal # - msbuild src\MSI\MSI.wixproj /p:Configuration=Release /v:Minimal
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Expand Up @@ -18,6 +18,7 @@
<PropertyGroup> <PropertyGroup>
<LangVersion>8.0</LangVersion> <LangVersion>8.0</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NullableContextOptions>enable</NullableContextOptions>
</PropertyGroup> </PropertyGroup>


<!-- Siging configuration --> <!-- Siging configuration -->
Expand Down
2 changes: 0 additions & 2 deletions Npgsql.sln.DotSettings
Expand Up @@ -6,8 +6,6 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleStringLiteral/@EntryIndexedValue">DO_NOT_SHOW</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleStringLiteral/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBeProtected_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBeProtected_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/ValueAnalysisMode/@EntryValue">IMPLICIT_NOTNULL</s:String>
<s:Boolean x:Key="/Default/CodeInspection/ImplicitNullability/Enabled/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ARGUMENTS_LITERAL/@EntryValue">Positional</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ARGUMENTS_LITERAL/@EntryValue">Positional</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ARGUMENTS_NAMED/@EntryValue">Positional</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ARGUMENTS_NAMED/@EntryValue">Positional</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/DEFAULT_INTERNAL_MODIFIER/@EntryValue">Implicit</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/DEFAULT_INTERNAL_MODIFIER/@EntryValue">Implicit</s:String>
Expand Down
12 changes: 6 additions & 6 deletions src/Npgsql.GeoJSON/CrsMap.cs
Expand Up @@ -9,9 +9,9 @@ namespace Npgsql.GeoJSON
{ {
internal readonly int MinSrid; internal readonly int MinSrid;
internal readonly int MaxSrid; internal readonly int MaxSrid;
internal readonly string Authority; internal readonly string? Authority;


internal CrsMapEntry(int minSrid, int maxSrid, string authority) internal CrsMapEntry(int minSrid, int maxSrid, string? authority)
{ {
MinSrid = minSrid; MinSrid = minSrid;
MaxSrid = maxSrid; MaxSrid = maxSrid;
Expand Down Expand Up @@ -74,15 +74,15 @@ internal CrsMap Build()


readonly partial struct CrsMap readonly partial struct CrsMap
{ {
readonly CrsMapEntry[] _overriden; readonly CrsMapEntry[]? _overriden;


internal CrsMap(CrsMapEntry[] overriden) internal CrsMap(CrsMapEntry[]? overriden)
=> _overriden = overriden; => _overriden = overriden;


internal string GetAuthority(int srid) internal string? GetAuthority(int srid)
=> GetAuthority(_overriden, srid) ?? GetAuthority(WellKnown, srid); => GetAuthority(_overriden, srid) ?? GetAuthority(WellKnown, srid);


static string GetAuthority(CrsMapEntry[] entries, int srid) static string? GetAuthority(CrsMapEntry[]? entries, int srid)
{ {
if (entries == null) if (entries == null)
return null; return null;
Expand Down
74 changes: 38 additions & 36 deletions src/Npgsql.GeoJSON/GeoJSONHandler.cs

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions src/Npgsql.Json.NET/JsonHandler.cs
Expand Up @@ -12,7 +12,8 @@ public class JsonHandlerFactory : NpgsqlTypeHandlerFactory<string>
{ {
readonly JsonSerializerSettings _settings; readonly JsonSerializerSettings _settings;


public JsonHandlerFactory(JsonSerializerSettings settings) => _settings = settings; public JsonHandlerFactory(JsonSerializerSettings? settings = null)
=> _settings = settings ?? new JsonSerializerSettings();


protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn) protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn)
=> new JsonHandler(conn, _settings); => new JsonHandler(conn, _settings);
Expand All @@ -24,7 +25,7 @@ class JsonHandler : Npgsql.TypeHandlers.TextHandler


public JsonHandler(NpgsqlConnection connection, JsonSerializerSettings settings) : base(connection) => _settings = settings; public JsonHandler(NpgsqlConnection connection, JsonSerializerSettings settings) : base(connection) => _settings = settings;


protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null) protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
{ {
var s = await base.Read<string>(buf, len, async, fieldDescription); var s = await base.Read<string>(buf, len, async, fieldDescription);
if (typeof(T) == typeof(string)) if (typeof(T) == typeof(string))
Expand All @@ -39,17 +40,17 @@ protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, boo
} }
} }


protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter) protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
=> typeof(T2) == typeof(string) => typeof(T2) == typeof(string)
? base.ValidateAndGetLength(value, ref lengthCache, parameter) ? base.ValidateAndGetLength(value, ref lengthCache, parameter)
: ValidateObjectAndGetLength(value, ref lengthCache, parameter); : ValidateObjectAndGetLength(value!, ref lengthCache, parameter);

protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> typeof(T2) == typeof(string) => typeof(T2) == typeof(string)
? base.WriteWithLength(value, buf, lengthCache, parameter, async) ? base.WriteWithLength(value, buf, lengthCache, parameter, async)
: WriteObjectWithLength(value, buf, lengthCache, parameter, async); : WriteObjectWithLength(value!, buf, lengthCache, parameter, async);


protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter) protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{ {
var s = value as string; var s = value as string;
if (s == null) if (s == null)
Expand All @@ -61,10 +62,10 @@ protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLength
return base.ValidateAndGetLength(s, ref lengthCache, parameter); return base.ValidateAndGetLength(s, ref lengthCache, parameter);
} }


protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
{ {
if (value == null || value is DBNull) if (value == null || value is DBNull)
return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async); return base.WriteObjectWithLength(DBNull.Value, buf, lengthCache, parameter, async);


if (parameter?.ConvertedValue != null) if (parameter?.ConvertedValue != null)
value = parameter.ConvertedValue; value = parameter.ConvertedValue;
Expand Down
22 changes: 12 additions & 10 deletions src/Npgsql.Json.NET/JsonbHandler.cs
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Npgsql.BackendMessages; using Npgsql.BackendMessages;
Expand All @@ -12,7 +13,8 @@ public class JsonbHandlerFactory : NpgsqlTypeHandlerFactory<string>
{ {
readonly JsonSerializerSettings _settings; readonly JsonSerializerSettings _settings;


public JsonbHandlerFactory(JsonSerializerSettings settings) => _settings = settings; public JsonbHandlerFactory(JsonSerializerSettings? settings = null)
=> _settings = settings ?? new JsonSerializerSettings();


protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn) protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn)
=> new JsonbHandler(conn, _settings); => new JsonbHandler(conn, _settings);
Expand All @@ -24,7 +26,7 @@ class JsonbHandler : Npgsql.TypeHandlers.JsonbHandler


public JsonbHandler(NpgsqlConnection connection, JsonSerializerSettings settings) : base(connection) => _settings = settings; public JsonbHandler(NpgsqlConnection connection, JsonSerializerSettings settings) : base(connection) => _settings = settings;


protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null) protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
{ {
var s = await base.Read<string>(buf, len, async, fieldDescription); var s = await base.Read<string>(buf, len, async, fieldDescription);
if (typeof(T) == typeof(string)) if (typeof(T) == typeof(string))
Expand All @@ -39,17 +41,17 @@ protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, boo
} }
} }


protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter) protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
=> typeof(T2) == typeof(string) => typeof(T2) == typeof(string)
? base.ValidateAndGetLength(value, ref lengthCache, parameter) ? base.ValidateAndGetLength(value, ref lengthCache, parameter)
: ValidateObjectAndGetLength(value, ref lengthCache, parameter); : ValidateObjectAndGetLength(value!, ref lengthCache, parameter);

protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> typeof(T2) == typeof(string) => typeof(T2) == typeof(string)
? base.WriteWithLength(value, buf, lengthCache, parameter, async) ? base.WriteWithLength(value, buf, lengthCache, parameter, async)
: WriteObjectWithLength(value, buf, lengthCache, parameter, async); : WriteObjectWithLength(value!, buf, lengthCache, parameter, async);


protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter) protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
{ {
var s = value as string; var s = value as string;
if (s == null) if (s == null)
Expand All @@ -61,10 +63,10 @@ protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLength
return base.ValidateObjectAndGetLength(s, ref lengthCache, parameter); return base.ValidateObjectAndGetLength(s, ref lengthCache, parameter);
} }


protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
{ {
if (value == null || value is DBNull) if (value == null || value is DBNull)
return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async); return base.WriteObjectWithLength(DBNull.Value, buf, lengthCache, parameter, async);


if (parameter?.ConvertedValue != null) if (parameter?.ConvertedValue != null)
value = parameter.ConvertedValue; value = parameter.ConvertedValue;
Expand Down
6 changes: 3 additions & 3 deletions src/Npgsql.Json.NET/NpgsqlJsonNetExtensions.cs
Expand Up @@ -21,9 +21,9 @@ public static class NpgsqlJsonNetExtensions
/// <param name="settings">Optional settings to customize JSON serialization</param> /// <param name="settings">Optional settings to customize JSON serialization</param>
public static INpgsqlTypeMapper UseJsonNet( public static INpgsqlTypeMapper UseJsonNet(
this INpgsqlTypeMapper mapper, this INpgsqlTypeMapper mapper,
Type[] jsonbClrTypes = null, Type[]? jsonbClrTypes = null,
Type[] jsonClrTypes = null, Type[]? jsonClrTypes = null,
JsonSerializerSettings settings = null JsonSerializerSettings? settings = null
) )
{ {
mapper.AddMapping(new NpgsqlTypeMappingBuilder mapper.AddMapping(new NpgsqlTypeMappingBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.LegacyPostgis/CodeAnnotations.cs
Expand Up @@ -164,7 +164,7 @@ internal enum ImplicitUseTargetFlags
sealed class PublicAPIAttribute : Attribute sealed class PublicAPIAttribute : Attribute
#pragma warning restore CA1018 #pragma warning restore CA1018
{ {
public PublicAPIAttribute() { } public PublicAPIAttribute() : this("") { }
public PublicAPIAttribute([NotNull] string comment) public PublicAPIAttribute([NotNull] string comment)
{ {
Comment = comment; Comment = comment;
Expand Down

0 comments on commit f2dd3f7

Please sign in to comment.