Skip to content
Permalink
Browse files

Convert codebase to use C# 8 nullability

Thanks to @Brar for considerable work on the feature!

Closes #2304
  • Loading branch information...
roji committed Apr 22, 2019
1 parent d922b0a commit f2dd3f7d0db4594c9220eca728f94acded9fd76c
Showing with 1,942 additions and 1,810 deletions.
  1. +1 −0 .appveyor.yml
  2. +1 −0 Directory.Build.props
  3. +0 −2 Npgsql.sln.DotSettings
  4. +6 −6 src/Npgsql.GeoJSON/CrsMap.cs
  5. +38 −36 src/Npgsql.GeoJSON/GeoJSONHandler.cs
  6. +11 −10 src/Npgsql.Json.NET/JsonHandler.cs
  7. +12 −10 src/Npgsql.Json.NET/JsonbHandler.cs
  8. +3 −3 src/Npgsql.Json.NET/NpgsqlJsonNetExtensions.cs
  9. +1 −1 src/Npgsql.LegacyPostgis/CodeAnnotations.cs
  10. +25 −25 src/Npgsql.LegacyPostgis/LegacyPostgisHandler.cs
  11. +53 −38 src/Npgsql.LegacyPostgis/PostgisTypes.cs
  12. +51 −55 src/Npgsql.NetTopologySuite/NetTopologySuiteHandler.cs
  13. +3 −3 src/Npgsql.NetTopologySuite/NpgsqlNetTopologySuiteExtensions.cs
  14. +40 −34 src/Npgsql.NetTopologySuite/NpgsqlPostGisWriter.cs
  15. +3 −3 src/Npgsql.NodaTime/DateHandler.cs
  16. +3 −3 src/Npgsql.NodaTime/IntervalHandler.cs
  17. +3 −3 src/Npgsql.NodaTime/TimeHandler.cs
  18. +3 −3 src/Npgsql.NodaTime/TimeTzHandler.cs
  19. +6 −6 src/Npgsql.NodaTime/TimestampHandler.cs
  20. +9 −9 src/Npgsql.NodaTime/TimestampTzHandler.cs
  21. +1 −1 src/Npgsql/AwaitableSocket.cs
  22. +29 −11 src/Npgsql/BackendMessages/AuthenticationMessages.cs
  23. +87 −40 src/Npgsql/BackendMessages/ErrorOrNoticeMessage.cs
  24. +5 −5 src/Npgsql/BackendMessages/RowDescriptionMessage.cs
  25. +8 −12 src/Npgsql/ConnectorPool.cs
  26. +5 −7 src/Npgsql/Counters.cs
  27. +1 −3 src/Npgsql/INpgsqlDatabaseInfoFactory.cs
  28. +1 −3 src/Npgsql/KerberosUsernameProvider.cs
  29. +1 −1 src/Npgsql/Logging/ConsoleLoggingProvider.cs
  30. +1 −1 src/Npgsql/Logging/NoOpLoggingProvider.cs
  31. +1 −1 src/Npgsql/Logging/NpgsqlLogger.cs
  32. +3 −3 src/Npgsql/NpgsqlBinaryExporter.cs
  33. +12 −3 src/Npgsql/NpgsqlBinaryImporter.cs
  34. +65 −59 src/Npgsql/NpgsqlCommand.cs
  35. +3 −2 src/Npgsql/NpgsqlCommandBuilder.cs
  36. +29 −42 src/Npgsql/NpgsqlConnection.cs
  37. +35 −47 src/Npgsql/NpgsqlConnectionStringBuilder.cs
  38. +5 −7 src/Npgsql/NpgsqlConnector.Auth.cs
  39. +1 −1 src/Npgsql/NpgsqlConnector.FrontendMessages.cs
  40. +64 −66 src/Npgsql/NpgsqlConnector.cs
  41. +6 −6 src/Npgsql/NpgsqlDataAdapter.cs
  42. +101 −98 src/Npgsql/NpgsqlDataReader.cs
  43. +35 −31 src/Npgsql/NpgsqlDatabaseInfo.cs
  44. +10 −11 src/Npgsql/NpgsqlFactory.cs
  45. +1 −1 src/Npgsql/NpgsqlOperationInProgressException.cs
  46. +19 −25 src/Npgsql/NpgsqlParameter.cs
  47. +8 −7 src/Npgsql/NpgsqlParameterCollection.cs
  48. +2 −0 src/Npgsql/NpgsqlParameter`.cs
  49. +2 −0 src/Npgsql/NpgsqlRawCopyStream.cs
  50. +4 −7 src/Npgsql/NpgsqlReadBuffer.cs
  51. +16 −17 src/Npgsql/NpgsqlSchema.cs
  52. +4 −9 src/Npgsql/NpgsqlStatement.cs
  53. +2 −2 src/Npgsql/NpgsqlTransaction.cs
  54. +5 −6 src/Npgsql/NpgsqlTypes/NpgsqlDate.cs
  55. +6 −6 src/Npgsql/NpgsqlTypes/NpgsqlDateTime.cs
  56. +3 −0 src/Npgsql/NpgsqlTypes/NpgsqlRange.cs
  57. +10 −16 src/Npgsql/NpgsqlTypes/NpgsqlTimeSpan.cs
  58. +34 −32 src/Npgsql/NpgsqlTypes/NpgsqlTsQuery.cs
  59. +17 −29 src/Npgsql/NpgsqlTypes/NpgsqlTsVector.cs
  60. +40 −21 src/Npgsql/NpgsqlTypes/NpgsqlTypes.cs
  61. +4 −9 src/Npgsql/NpgsqlWriteBuffer.cs
  62. +10 −14 src/Npgsql/PgPassFile.cs
  63. +2 −1 src/Npgsql/PoolManager.cs
  64. +18 −25 src/Npgsql/PostgresDatabaseInfo.cs
  65. +23 −18 src/Npgsql/PostgresException.cs
  66. +3 −12 src/Npgsql/PostgresMinimalDatabaseInfo.cs
  67. +22 −15 src/Npgsql/PostgresNotice.cs
  68. +2 −1 src/Npgsql/PostgresTypes/PostgresBaseType.cs
  69. +4 −4 src/Npgsql/PostgresTypes/PostgresType.cs
  70. +4 −0 src/Npgsql/PregeneratedMessages.cs
  71. +12 −14 src/Npgsql/PreparedStatement.cs
  72. +6 −9 src/Npgsql/PreparedStatementManager.cs
  73. +1 −0 src/Npgsql/Schema/DbColumn.cs
  74. +18 −13 src/Npgsql/Schema/DbColumnSchemaGenerator.cs
  75. +6 −5 src/Npgsql/Schema/NpgsqlDbColumn.cs
  76. +1 −3 src/Npgsql/SingleThreadSynchronizationContext.cs
  77. +2 −2 src/Npgsql/SqlQueryParser.cs
  78. +36 −38 src/Npgsql/TypeHandlers/ArrayHandler.cs
  79. +20 −20 src/Npgsql/TypeHandlers/BitStringHandler.cs
  80. +3 −3 src/Npgsql/TypeHandlers/BoolHandler.cs
  81. +13 −14 src/Npgsql/TypeHandlers/ByteaHandler.cs
  82. +5 −1 src/Npgsql/TypeHandlers/CompositeHandlers/ByReference.cs
  83. +2 −2 src/Npgsql/TypeHandlers/CompositeHandlers/CompositeMemberHandler.cs
  84. +14 −4 src/Npgsql/TypeHandlers/CompositeHandlers/CompositeMemberHandlerOfClass.cs
  85. +18 −8 src/Npgsql/TypeHandlers/CompositeHandlers/CompositeMemberHandlerOfStruct.cs
  86. +5 −5 src/Npgsql/TypeHandlers/CompositeHandlers/MappedCompositeHandler.cs
  87. +1 −1 src/Npgsql/TypeHandlers/CompositeHandlers/MappedCompositeTypeHandlerFactory.cs
  88. +2 −0 src/Npgsql/TypeHandlers/CompositeHandlers/UnmappedCompositeHandler.cs
  89. +2 −0 src/Npgsql/TypeHandlers/CompositeHandlers/UnmappedCompositeTypeHandlerFactory.cs
  90. +7 −7 src/Npgsql/TypeHandlers/DateTimeHandlers/DateHandler.cs
  91. +6 −6 src/Npgsql/TypeHandlers/DateTimeHandlers/IntervalHandler.cs
  92. +3 −3 src/Npgsql/TypeHandlers/DateTimeHandlers/TimeHandler.cs
  93. +9 −10 src/Npgsql/TypeHandlers/DateTimeHandlers/TimeTzHandler.cs
  94. +8 −8 src/Npgsql/TypeHandlers/DateTimeHandlers/TimestampHandler.cs
  95. +8 −8 src/Npgsql/TypeHandlers/DateTimeHandlers/TimestampTzHandler.cs
  96. +5 −5 src/Npgsql/TypeHandlers/EnumHandler.cs
  97. +53 −51 src/Npgsql/TypeHandlers/FullTextSearchHandlers/TsQueryHandler.cs
  98. +4 −4 src/Npgsql/TypeHandlers/FullTextSearchHandlers/TsVectorHandler.cs
  99. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/BoxHandler.cs
  100. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/CircleHandler.cs
  101. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/LineHandler.cs
  102. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/LineSegmentHandler.cs
  103. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/PathHandler.cs
  104. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/PointHandler.cs
  105. +3 −3 src/Npgsql/TypeHandlers/GeometricHandlers/PolygonHandler.cs
  106. +13 −13 src/Npgsql/TypeHandlers/HstoreHandler.cs
  107. +15 −16 src/Npgsql/TypeHandlers/InternalCharHandler.cs
  108. +4 −3 src/Npgsql/TypeHandlers/InternalTypeHandlers/Int2VectorHandler.cs
  109. +4 −3 src/Npgsql/TypeHandlers/InternalTypeHandlers/OIDVectorHandler.cs
  110. +3 −3 src/Npgsql/TypeHandlers/InternalTypeHandlers/TidHandler.cs
  111. +9 −9 src/Npgsql/TypeHandlers/JsonbHandler.cs
  112. +4 −4 src/Npgsql/TypeHandlers/MappedEnumHandler.cs
  113. +6 −8 src/Npgsql/TypeHandlers/NetworkHandlers/CidrHandler.cs
  114. +14 −18 src/Npgsql/TypeHandlers/NetworkHandlers/InetHandler.cs
  115. +3 −3 src/Npgsql/TypeHandlers/NetworkHandlers/MacaddrHandler.cs
  116. +3 −3 src/Npgsql/TypeHandlers/NumericHandlers/DoubleHandler.cs
  117. +24 −25 src/Npgsql/TypeHandlers/NumericHandlers/Int16Handler.cs
  118. +21 −22 src/Npgsql/TypeHandlers/NumericHandlers/Int32Handler.cs
  119. +21 −22 src/Npgsql/TypeHandlers/NumericHandlers/Int64Handler.cs
  120. +3 −3 src/Npgsql/TypeHandlers/NumericHandlers/MoneyHandler.cs
  121. +21 −22 src/Npgsql/TypeHandlers/NumericHandlers/NumericHandler.cs
  122. +6 −7 src/Npgsql/TypeHandlers/NumericHandlers/SingleHandler.cs
  123. +3 −3 src/Npgsql/TypeHandlers/NumericHandlers/UInt32Handler.cs
  124. +17 −15 src/Npgsql/TypeHandlers/RangeHandler.cs
  125. +4 −4 src/Npgsql/TypeHandlers/RecordHandler.cs
  126. +21 −22 src/Npgsql/TypeHandlers/TextHandler.cs
  127. +11 −8 src/Npgsql/TypeHandlers/UnknownTypeHandler.cs
  128. +12 −14 src/Npgsql/TypeHandlers/UnmappedEnumHandler.cs
  129. +4 −4 src/Npgsql/TypeHandlers/UuidHandler.cs
  130. +3 −3 src/Npgsql/TypeHandlers/VoidHandler.cs
  131. +5 −6 src/Npgsql/TypeHandling/INpgsqlSimpleTypeHandler.cs
  132. +4 −5 src/Npgsql/TypeHandling/INpgsqlTypeHandler.cs
  133. +15 −15 src/Npgsql/TypeHandling/NpgsqlSimpleTypeHandler.cs
  134. +14 −12 src/Npgsql/TypeHandling/NpgsqlSimpleTypeHandlerWithPsv.cs
  135. +24 −20 src/Npgsql/TypeHandling/NpgsqlTypeHandler.cs
  136. +23 −22 src/Npgsql/TypeHandling/NpgsqlTypeHandler`.cs
  137. +15 −15 src/Npgsql/TypeHandling/NullableHandler.cs
  138. +19 −17 src/Npgsql/TypeMapping/ConnectorTypeMapper.cs
  139. +1 −2 src/Npgsql/TypeMapping/GlobalTypeMapper.cs
  140. +8 −8 src/Npgsql/TypeMapping/INpgsqlTypeMapper.cs
  141. +5 −5 src/Npgsql/TypeMapping/NpgsqlTypeMapping.cs
  142. +6 −7 src/Npgsql/TypeMapping/TypeMapperBase.cs
  143. +3 −3 src/Npgsql/TypeMapping/TypeMappingAttribute.cs
  144. +1 −1 src/Npgsql/Util/CodeAnnotations.cs
  145. +11 −0 src/Npgsql/Util/NullabilityAnnotations.cs
  146. +10 −9 src/Npgsql/VolatileResourceManager.cs
  147. +9 −5 src/Npgsql/WindowsUsernameProvider.cs
  148. +3 −1 src/VSIX/NpgsqlConnectionUIControl.Designer.cs
  149. +2 −0 src/VSIX/NpgsqlProviderObjectFactory.cs
  150. +1 −1 src/VSIX/NpgsqlProviderRegistration.cs
  151. +6 −1 src/VSIX/VSIX.csproj
  152. +2 −0 test/Npgsql.Benchmarks/ConnectionOpenCloseBenchmarks.cs
  153. +1 −1 test/Npgsql.Benchmarks/CopyExport.cs
  154. +2 −2 test/Npgsql.Benchmarks/CopyImport.cs
  155. +3 −7 test/Npgsql.Benchmarks/Insert.cs
  156. +3 −3 test/Npgsql.Benchmarks/Prepare.cs
  157. +3 −3 test/Npgsql.Benchmarks/ReadArray.cs
  158. +2 −2 test/Npgsql.Benchmarks/ReadColumns.cs
  159. +1 −1 test/Npgsql.Benchmarks/ReadRows.cs
  160. +3 −3 test/Npgsql.Benchmarks/WriteVaryingNumberOfParameters.cs
  161. +4 −4 test/Npgsql.PluginTests/GeoJSONTests.cs
  162. +1 −1 test/Npgsql.PluginTests/JsonNetTests.cs
  163. +4 −4 test/Npgsql.PluginTests/LegacyPostgisTests.cs
  164. +2 −2 test/Npgsql.PluginTests/NetTopologySuiteTests.cs
  165. +1 −1 test/Npgsql.PluginTests/NodaTimeTests.cs
  166. +1 −1 test/Npgsql.PluginTests/RawPostgisTests.cs
  167. +2 −2 test/Npgsql.Tests/AutoPrepareTests.cs
  168. +8 −7 test/Npgsql.Tests/CommandBuilderTests.cs
  169. +1 −1 test/Npgsql.Tests/ConnectionStringBuilderTests.cs
  170. +9 −11 test/Npgsql.Tests/ConnectionTests.cs
  171. +3 −3 test/Npgsql.Tests/CopyTests.cs
  172. +8 −8 test/Npgsql.Tests/DistributedTransactionTests.cs
  173. +4 −1 test/Npgsql.Tests/ExceptionTests.cs
  174. +1 −1 test/Npgsql.Tests/NpgsqlParameterTests.cs
  175. +8 −8 test/Npgsql.Tests/PerformanceCounterTests.cs
  176. +6 −8 test/Npgsql.Tests/PgPassFileTests.cs
  177. +3 −3 test/Npgsql.Tests/PoolManagerTests.cs
  178. +10 −5 test/Npgsql.Tests/PoolTests.cs
  179. +1 −1 test/Npgsql.Tests/PrepareTests.cs
  180. +4 −2 test/Npgsql.Tests/ReadBufferTests.cs
  181. +6 −6 test/Npgsql.Tests/ReaderTests.cs
  182. +2 −2 test/Npgsql.Tests/SchemaTests.cs
  183. +3 −3 test/Npgsql.Tests/SqlQueryParserTests.cs
  184. +1 −1 test/Npgsql.Tests/Support/NLogLoggingProvider.cs
  185. +5 −3 test/Npgsql.Tests/SystemTransactionTests.cs
  186. +1 −1 test/Npgsql.Tests/TestBase.cs
  187. +14 −12 test/Npgsql.Tests/TestUtil.cs
  188. +5 −3 test/Npgsql.Tests/TransactionTests.cs
  189. +2 −2 test/Npgsql.Tests/TypeMapperTests.cs
  190. +4 −2 test/Npgsql.Tests/Types/CompositeTests.cs
  191. +20 −11 test/Npgsql.Tests/Types/MiscTypeTests.cs
  192. +1 −1 test/Npgsql.Tests/Types/RangeTests.cs
  193. +2 −0 test/Npgsql.Tests/TypesTests.cs
  194. +4 −2 test/Npgsql.Tests/WriteBufferTests.cs
@@ -29,6 +29,7 @@ before_build:
build_script:
- dotnet build "test\Npgsql.Tests" -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
# WIX hasn't been released yet for VS 2019
# - msbuild src\MSI\MSI.wixproj /p:Configuration=Release /v:Minimal
@@ -18,6 +18,7 @@
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NullableContextOptions>enable</NullableContextOptions>
</PropertyGroup>

<!-- Siging configuration -->
@@ -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/=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/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_NAMED/@EntryValue">Positional</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/DEFAULT_INTERNAL_MODIFIER/@EntryValue">Implicit</s:String>
@@ -9,9 +9,9 @@ namespace Npgsql.GeoJSON
{
internal readonly int MinSrid;
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;
MaxSrid = maxSrid;
@@ -74,15 +74,15 @@ internal CrsMap Build()

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

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

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

static string GetAuthority(CrsMapEntry[] entries, int srid)
static string? GetAuthority(CrsMapEntry[]? entries, int srid)
{
if (entries == null)
return null;

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -12,7 +12,8 @@ public class JsonHandlerFactory : NpgsqlTypeHandlerFactory<string>
{
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)
=> new JsonHandler(conn, _settings);
@@ -24,7 +25,7 @@ class JsonHandler : Npgsql.TypeHandlers.TextHandler

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);
if (typeof(T) == typeof(string))
@@ -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)
? base.ValidateAndGetLength(value, ref lengthCache, parameter)
: ValidateObjectAndGetLength(value, ref lengthCache, parameter);
protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
: ValidateObjectAndGetLength(value!, ref lengthCache, parameter);

protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> typeof(T2) == typeof(string)
? 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;
if (s == null)
@@ -61,10 +62,10 @@ protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLength
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)
return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async);
return base.WriteObjectWithLength(DBNull.Value, buf, lengthCache, parameter, async);

if (parameter?.ConvertedValue != null)
value = parameter.ConvertedValue;
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Npgsql.BackendMessages;
@@ -12,7 +13,8 @@ public class JsonbHandlerFactory : NpgsqlTypeHandlerFactory<string>
{
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)
=> new JsonbHandler(conn, _settings);
@@ -24,7 +26,7 @@ class JsonbHandler : Npgsql.TypeHandlers.JsonbHandler

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);
if (typeof(T) == typeof(string))
@@ -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)
? base.ValidateAndGetLength(value, ref lengthCache, parameter)
: ValidateObjectAndGetLength(value, ref lengthCache, parameter);
protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
: ValidateObjectAndGetLength(value!, ref lengthCache, parameter);

protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
=> typeof(T2) == typeof(string)
? 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;
if (s == null)
@@ -61,10 +63,10 @@ protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLength
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)
return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async);
return base.WriteObjectWithLength(DBNull.Value, buf, lengthCache, parameter, async);

if (parameter?.ConvertedValue != null)
value = parameter.ConvertedValue;
@@ -21,9 +21,9 @@ public static class NpgsqlJsonNetExtensions
/// <param name="settings">Optional settings to customize JSON serialization</param>
public static INpgsqlTypeMapper UseJsonNet(
this INpgsqlTypeMapper mapper,
Type[] jsonbClrTypes = null,
Type[] jsonClrTypes = null,
JsonSerializerSettings settings = null
Type[]? jsonbClrTypes = null,
Type[]? jsonClrTypes = null,
JsonSerializerSettings? settings = null
)
{
mapper.AddMapping(new NpgsqlTypeMappingBuilder
@@ -164,7 +164,7 @@ internal enum ImplicitUseTargetFlags
sealed class PublicAPIAttribute : Attribute
#pragma warning restore CA1018
{
public PublicAPIAttribute() { }
public PublicAPIAttribute() : this("") { }
public PublicAPIAttribute([NotNull] string comment)
{
Comment = comment;
Oops, something went wrong.

0 comments on commit f2dd3f7

Please sign in to comment.
You can’t perform that action at this time.