You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by fl...@apache.org on 2021/04/14 16:32:40 UTC

[tinkerpop] 02/02: WIP Enabled nullable checks in some files in the driver, GremlinClientExtensions still needs some work

This is an automated email from the ASF dual-hosted git repository.

florianhockmann pushed a commit to branch TINKERPOP-2348
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit ab8b1bd9baf1be5015c8c328f00fc56ffec476f3
Author: Florian Hockmann <fh...@florian-hockmann.de>
AuthorDate: Wed Apr 14 18:32:21 2021 +0200

    WIP Enabled nullable checks in some files in the driver, GremlinClientExtensions still needs some work
---
 .../src/Gremlin.Net/Driver/Connection.cs           | 23 +++++++++++-----------
 .../src/Gremlin.Net/Driver/ConnectionFactory.cs    | 10 ++++++----
 .../src/Gremlin.Net/Driver/ConnectionPool.cs       |  2 ++
 .../Gremlin.Net/Driver/ConnectionPoolSettings.cs   |  2 ++
 .../Gremlin.Net/Driver/CopyOnWriteCollection.cs    |  2 ++
 .../src/Gremlin.Net/Driver/GremlinClient.cs        | 23 +++++++++++-----------
 .../Gremlin.Net/Driver/GremlinClientExtensions.cs  |  8 +++++---
 .../src/Gremlin.Net/Driver/GremlinServer.cs        |  8 +++++---
 .../src/Gremlin.Net/Driver/ProxyConnection.cs      |  2 ++
 .../ResponseHandlerForSingleRequestMessage.cs      |  2 ++
 gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs |  2 ++
 gremlin-dotnet/src/Gremlin.Net/Driver/Tokens.cs    |  3 ++-
 .../src/Gremlin.Net/Driver/WebSocketConnection.cs  |  7 ++++---
 13 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
index 491a62a..0068910 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
@@ -45,20 +47,20 @@ namespace Gremlin.Net.Driver
         private readonly IMessageSerializer _messageSerializer;
         private readonly Uri _uri;
         private readonly WebSocketConnection _webSocketConnection;
-        private readonly string _username;
-        private readonly string _password;
-        private readonly string _sessionId;
+        private readonly string? _username;
+        private readonly string? _password;
+        private readonly string? _sessionId;
         private readonly bool _sessionEnabled;
-        private readonly ConcurrentQueue<RequestMessage> _writeQueue = new ConcurrentQueue<RequestMessage>();
+        private readonly ConcurrentQueue<RequestMessage> _writeQueue = new();
 
         private readonly ConcurrentDictionary<Guid, IResponseHandlerForSingleRequestMessage> _callbackByRequestId =
-            new ConcurrentDictionary<Guid, IResponseHandlerForSingleRequestMessage>();
+            new();
         private int _connectionState = 0;
         private int _writeInProgress = 0;
         private const int Closed = 1;
 
-        public Connection(Uri uri, string username, string password, IMessageSerializer messageSerializer,
-            Action<ClientWebSocketOptions> webSocketConfiguration, string sessionId)
+        public Connection(Uri uri, string? username, string? password, IMessageSerializer messageSerializer,
+            Action<ClientWebSocketOptions>? webSocketConfiguration, string? sessionId)
         {
             _uri = uri;
             _username = username;
@@ -120,7 +122,7 @@ namespace Gremlin.Net.Driver
             var receivedMsg = await _messageSerializer.DeserializeMessageAsync(received).ConfigureAwait(false);
             if (receivedMsg == null)
             {
-                ThrowMessageDeserializedNull();
+                throw new InvalidOperationException("Received data deserialized into null object message. Cannot operate on it.");
             }
 
             try
@@ -137,9 +139,6 @@ namespace Gremlin.Net.Driver
             }
         }
 
-        private static void ThrowMessageDeserializedNull() =>
-            throw new InvalidOperationException("Received data deserialized into null object message. Cannot operate on it.");
-
         private void HandleReceivedMessage(ResponseMessage<List<object>> receivedMsg)
         {
             var status = receivedMsg.Status;
@@ -260,7 +259,7 @@ namespace Gremlin.Net.Driver
             {
                 msgBuilder.AddArgument(kv.Key, kv.Value);
             }
-            msgBuilder.AddArgument(Tokens.ArgsSession, _sessionId);
+            msgBuilder.AddArgument(Tokens.ArgsSession, _sessionId!);
             return msgBuilder.Create();
         }
 
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
index 59807a1..b0c718e 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
@@ -1,4 +1,4 @@
-#region License
+#region License
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.Net.WebSockets;
 
@@ -28,13 +30,13 @@ namespace Gremlin.Net.Driver
 {
     internal class ConnectionFactory : IConnectionFactory
     {
-        private readonly Action<ClientWebSocketOptions> _webSocketConfiguration;
+        private readonly Action<ClientWebSocketOptions>? _webSocketConfiguration;
         private readonly GremlinServer _gremlinServer;
-        private readonly string _sessionId;
+        private readonly string? _sessionId;
         private IMessageSerializer _messageSerializer;
 
         public ConnectionFactory(GremlinServer gremlinServer, IMessageSerializer messageSerializer,
-            Action<ClientWebSocketOptions> webSocketConfiguration, string sessionId)
+            Action<ClientWebSocketOptions>? webSocketConfiguration, string? sessionId)
         {
             _gremlinServer = gremlinServer;
             _messageSerializer = messageSerializer;
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
index 261504a..0229760 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPoolSettings.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPoolSettings.cs
index 0196598..f296268 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPoolSettings.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPoolSettings.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using Gremlin.Net.Driver.Exceptions;
 
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/CopyOnWriteCollection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/CopyOnWriteCollection.cs
index d00927e..311a0c8 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/CopyOnWriteCollection.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/CopyOnWriteCollection.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 
 namespace Gremlin.Net.Driver
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
index 745a3cc..25816d5 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.Net.WebSockets;
 using System.Threading.Tasks;
@@ -51,8 +53,8 @@ namespace Gremlin.Net.Driver
         /// <param name="sessionId">The session Id if Gremlin Client in session mode, defaults to null as session-less Client.</param>
         [Obsolete("This constructor is obsolete. Use the constructor that takes a IMessageSerializer instead.")]
         public GremlinClient(GremlinServer gremlinServer, GraphSONReader graphSONReader, GraphSONWriter graphSONWriter,
-            ConnectionPoolSettings connectionPoolSettings = null,
-            Action<ClientWebSocketOptions> webSocketConfiguration = null, string sessionId = null)
+            ConnectionPoolSettings? connectionPoolSettings = null,
+            Action<ClientWebSocketOptions>? webSocketConfiguration = null, string? sessionId = null)
             : this(gremlinServer, graphSONReader, graphSONWriter, SerializationTokens.GraphSON3MimeType,
                 connectionPoolSettings, webSocketConfiguration, sessionId)
         {
@@ -73,8 +75,8 @@ namespace Gremlin.Net.Driver
         /// <param name="sessionId">The session Id if Gremlin Client in session mode, defaults to null as session-less Client.</param>
         [Obsolete("This constructor is obsolete. Use the constructor that takes a IMessageSerializer instead.")]
         public GremlinClient(GremlinServer gremlinServer, GraphSONReader graphSONReader, GraphSONWriter graphSONWriter,
-            string mimeType, ConnectionPoolSettings connectionPoolSettings = null,
-            Action<ClientWebSocketOptions> webSocketConfiguration = null, string sessionId = null)
+            string mimeType, ConnectionPoolSettings? connectionPoolSettings = null,
+            Action<ClientWebSocketOptions>? webSocketConfiguration = null, string? sessionId = null)
         {
             IMessageSerializer messageSerializer;
             switch (mimeType)
@@ -84,9 +86,8 @@ namespace Gremlin.Net.Driver
                         mimeType);
                     VerifyGraphSONArgumentTypeForMimeType<GraphSON3Writer>(graphSONWriter, nameof(graphSONWriter),
                         mimeType);
-                    messageSerializer = new GraphSON3MessageSerializer(
-                        (GraphSON3Reader) graphSONReader ?? new GraphSON3Reader(),
-                        (GraphSON3Writer) graphSONWriter ?? new GraphSON3Writer());
+                    messageSerializer = new GraphSON3MessageSerializer((GraphSON3Reader) graphSONReader,
+                        (GraphSON3Writer) graphSONWriter);
                     break;
                 case SerializationTokens.GraphSON2MimeType:
                     VerifyGraphSONArgumentTypeForMimeType<GraphSON2Reader>(graphSONReader, nameof(graphSONReader),
@@ -145,11 +146,11 @@ namespace Gremlin.Net.Driver
         ///     object used to configure WebSocket connections.
         /// </param>
         /// <param name="sessionId">The session Id if Gremlin Client in session mode, defaults to null as session-less Client.</param>
-        public GremlinClient(GremlinServer gremlinServer, IMessageSerializer messageSerializer = null,
-            ConnectionPoolSettings connectionPoolSettings = null,
-            Action<ClientWebSocketOptions> webSocketConfiguration = null, string sessionId = null)
+        public GremlinClient(GremlinServer gremlinServer, IMessageSerializer? messageSerializer = null,
+            ConnectionPoolSettings? connectionPoolSettings = null,
+            Action<ClientWebSocketOptions>? webSocketConfiguration = null, string? sessionId = null)
         {
-            messageSerializer = messageSerializer ?? new GraphSON3MessageSerializer();
+            messageSerializer ??= new GraphSON3MessageSerializer();
             var connectionFactory =
                 new ConnectionFactory(gremlinServer, messageSerializer, webSocketConfiguration, sessionId);
 
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClientExtensions.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClientExtensions.cs
index 1b18241..b6cd02f 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClientExtensions.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClientExtensions.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
@@ -52,7 +54,7 @@ namespace Gremlin.Net.Driver
         /// </exception>
         public static async Task<T> SubmitWithSingleResultAsync<T>(this IGremlinClient gremlinClient,
             string requestScript,
-            Dictionary<string, object> bindings = null)
+            Dictionary<string, object>? bindings = null)
         {
             var resultCollection = await gremlinClient.SubmitAsync<T>(requestScript, bindings).ConfigureAwait(false);
             return resultCollection.FirstOrDefault();
@@ -93,7 +95,7 @@ namespace Gremlin.Net.Driver
         ///     that an error occurred.
         /// </exception>
         public static async Task SubmitAsync(this IGremlinClient gremlinClient, string requestScript,
-            Dictionary<string, object> bindings = null)
+            Dictionary<string, object>? bindings = null)
         {
             await gremlinClient.SubmitAsync<object>(requestScript, bindings).ConfigureAwait(false);
         }
@@ -128,7 +130,7 @@ namespace Gremlin.Net.Driver
         /// </exception>
         public static async Task<ResultSet<T>> SubmitAsync<T>(this IGremlinClient gremlinClient,
             string requestScript,
-            Dictionary<string, object> bindings = null)
+            Dictionary<string, object>? bindings = null)
         {
             var msgBuilder = RequestMessage.Build(Tokens.OpsEval).AddArgument(Tokens.ArgsGremlin, requestScript);
             if (bindings != null)
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinServer.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinServer.cs
index 43ef159..5838f1c 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinServer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinServer.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 
 namespace Gremlin.Net.Driver
@@ -39,7 +41,7 @@ namespace Gremlin.Net.Driver
         /// <param name="username">The username to submit on requests that require authentication.</param>
         /// <param name="password">The password to submit on requests that require authentication.</param>
         public GremlinServer(string hostname = "localhost", int port = 8182, bool enableSsl = false,
-            string username = null, string password = null)
+            string? username = null, string? password = null)
         {
             Uri = CreateUri(hostname, port, enableSsl);
             Username = username;
@@ -55,12 +57,12 @@ namespace Gremlin.Net.Driver
         /// <summary>
         ///     Gets the username to submit on requests that require authentication.
         /// </summary>
-        public string Username { get; }
+        public string? Username { get; }
 
         /// <summary>
         ///     Gets the password to submit on requests that require authentication.
         /// </summary>
-        public string Password { get; }
+        public string? Password { get; }
 
         private Uri CreateUri(string hostname, int port, bool enableSsl)
         {
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ProxyConnection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ProxyConnection.cs
index 41c5c4a..6660ad3 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ProxyConnection.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ProxyConnection.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.Collections.Generic;
 using System.Threading.Tasks;
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ResponseHandlerForSingleRequestMessage.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ResponseHandlerForSingleRequestMessage.cs
index fafa7d9..61dc9e0 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ResponseHandlerForSingleRequestMessage.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ResponseHandlerForSingleRequestMessage.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.Collections.Generic;
 using System.Threading.Tasks;
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs
index 55a8661..d726287 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ResultSet.cs
@@ -21,6 +21,8 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System.Collections;
 using System.Collections.Generic;
 
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Tokens.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Tokens.cs
index f35b128..d1dcd8c 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/Tokens.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Tokens.cs
@@ -21,7 +21,8 @@
 
 #endregion
 
-using System;
+#nullable enable warnings
+
 using Gremlin.Net.Driver.Messages;
 
 namespace Gremlin.Net.Driver
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs
index e239230..0432ab9 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs
@@ -21,14 +21,14 @@
 
 #endregion
 
+#nullable enable warnings
+
 using System;
 using System.IO;
 using System.Net.WebSockets;
 using System.Threading;
 using System.Threading.Tasks;
 
-#nullable enable warnings
-
 namespace Gremlin.Net.Driver
 {
     internal class WebSocketConnection : IDisposable
@@ -37,7 +37,7 @@ namespace Gremlin.Net.Driver
         private const WebSocketMessageType MessageType = WebSocketMessageType.Binary;
         private readonly ClientWebSocket _client;
 
-        public WebSocketConnection(Action<ClientWebSocketOptions> webSocketConfiguration)
+        public WebSocketConnection(Action<ClientWebSocketOptions>? webSocketConfiguration)
         {
             _client = new ClientWebSocket();
             webSocketConfiguration?.Invoke(_client.Options);
@@ -86,6 +86,7 @@ namespace Gremlin.Net.Driver
                 {
                     var receiveBuffer = new ArraySegment<byte>(buffer);
                     received = await _client.ReceiveAsync(receiveBuffer, CancellationToken.None).ConfigureAwait(false);
+                    if (receiveBuffer.Array == null) break;
                     ms.Write(receiveBuffer.Array, receiveBuffer.Offset, received.Count);
                 } while (!received.EndOfMessage);