Skip to content

Commit 552d8d8

Browse files
committed
Use switch expressions
1 parent 8b186c6 commit 552d8d8

23 files changed

Lines changed: 363 additions & 723 deletions

File tree

src/Npgsql.GeoJSON/GeoJSONHandler.cs

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -322,34 +322,17 @@ static Position ReadPosition(NpgsqlReadBuffer buf, EwkbGeometryType type, bool l
322322
#region Write
323323

324324
public override int ValidateAndGetLength(GeoJSONObject value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
325-
{
326-
switch (value.Type)
325+
=> value.Type switch
327326
{
328-
case GeoJSONObjectType.Point:
329-
return ValidateAndGetLength((Point)value, ref lengthCache, parameter);
330-
331-
case GeoJSONObjectType.LineString:
332-
return ValidateAndGetLength((LineString)value, ref lengthCache, parameter);
333-
334-
case GeoJSONObjectType.Polygon:
335-
return ValidateAndGetLength((Polygon)value, ref lengthCache, parameter);
336-
337-
case GeoJSONObjectType.MultiPoint:
338-
return ValidateAndGetLength((MultiPoint)value, ref lengthCache, parameter);
339-
340-
case GeoJSONObjectType.MultiLineString:
341-
return ValidateAndGetLength((MultiLineString)value, ref lengthCache, parameter);
342-
343-
case GeoJSONObjectType.MultiPolygon:
344-
return ValidateAndGetLength((MultiPolygon)value, ref lengthCache, parameter);
345-
346-
case GeoJSONObjectType.GeometryCollection:
347-
return ValidateAndGetLength((GeometryCollection)value, ref lengthCache, parameter);
348-
349-
default:
350-
throw UnknownPostGisType();
351-
}
352-
}
327+
GeoJSONObjectType.Point => ValidateAndGetLength((Point)value, ref lengthCache, parameter),
328+
GeoJSONObjectType.LineString => ValidateAndGetLength((LineString)value, ref lengthCache, parameter),
329+
GeoJSONObjectType.Polygon => ValidateAndGetLength((Polygon)value, ref lengthCache, parameter),
330+
GeoJSONObjectType.MultiPoint => ValidateAndGetLength((MultiPoint)value, ref lengthCache, parameter),
331+
GeoJSONObjectType.MultiLineString => ValidateAndGetLength((MultiLineString)value, ref lengthCache, parameter),
332+
GeoJSONObjectType.MultiPolygon => ValidateAndGetLength((MultiPolygon)value, ref lengthCache, parameter),
333+
GeoJSONObjectType.GeometryCollection => ValidateAndGetLength((GeometryCollection)value, ref lengthCache, parameter),
334+
_ => throw UnknownPostGisType()
335+
};
353336

354337
public int ValidateAndGetLength(Point value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
355338
{
@@ -471,34 +454,17 @@ int INpgsqlTypeHandler<IGeometryObject>.ValidateAndGetLength(IGeometryObject val
471454
=> ValidateAndGetLength((GeoJSONObject)value, ref lengthCache, parameter);
472455

473456
public override Task Write(GeoJSONObject value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
474-
{
475-
switch (value.Type)
457+
=> value.Type switch
476458
{
477-
case GeoJSONObjectType.Point:
478-
return Write((Point)value, buf, lengthCache, parameter, async);
479-
480-
case GeoJSONObjectType.LineString:
481-
return Write((LineString)value, buf, lengthCache, parameter, async);
482-
483-
case GeoJSONObjectType.Polygon:
484-
return Write((Polygon)value, buf, lengthCache, parameter, async);
485-
486-
case GeoJSONObjectType.MultiPoint:
487-
return Write((MultiPoint)value, buf, lengthCache, parameter, async);
488-
489-
case GeoJSONObjectType.MultiLineString:
490-
return Write((MultiLineString)value, buf, lengthCache, parameter, async);
491-
492-
case GeoJSONObjectType.MultiPolygon:
493-
return Write((MultiPolygon)value, buf, lengthCache, parameter, async);
494-
495-
case GeoJSONObjectType.GeometryCollection:
496-
return Write((GeometryCollection)value, buf, lengthCache, parameter, async);
497-
498-
default:
499-
throw UnknownPostGisType();
500-
}
501-
}
459+
GeoJSONObjectType.Point => Write((Point)value, buf, lengthCache, parameter, async),
460+
GeoJSONObjectType.LineString => Write((LineString)value, buf, lengthCache, parameter, async),
461+
GeoJSONObjectType.Polygon => Write((Polygon)value, buf, lengthCache, parameter, async),
462+
GeoJSONObjectType.MultiPoint => Write((MultiPoint)value, buf, lengthCache, parameter, async),
463+
GeoJSONObjectType.MultiLineString => Write((MultiLineString)value, buf, lengthCache, parameter, async),
464+
GeoJSONObjectType.MultiPolygon => Write((MultiPolygon)value, buf, lengthCache, parameter, async),
465+
GeoJSONObjectType.GeometryCollection => Write((GeometryCollection)value, buf, lengthCache, parameter, async),
466+
_ => throw UnknownPostGisType()
467+
};
502468

503469
public async Task Write(Point value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
504470
{

src/Npgsql/BackendMessages/CopyMessages.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ internal void Load(NpgsqlReadBuffer buf)
2222
ColumnFormatCodes.Clear();
2323

2424
var binaryIndicator = buf.ReadByte();
25-
switch (binaryIndicator) {
26-
case 0:
27-
IsBinary = false;
28-
break;
29-
case 1:
30-
IsBinary = true;
31-
break;
32-
default:
33-
throw new Exception("Invalid binary indicator in CopyInResponse message: " + binaryIndicator);
34-
}
25+
IsBinary = binaryIndicator switch
26+
{
27+
0 => false,
28+
1 => true,
29+
_ => throw new Exception("Invalid binary indicator in CopyInResponse message: " + binaryIndicator)
30+
};
3531

3632
NumColumns = buf.ReadInt16();
3733
for (var i = 0; i < NumColumns; i++)

src/Npgsql/NpgsqlCommand.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -447,23 +447,15 @@ void DeriveParametersForFunction()
447447
param.Direction = ParameterDirection.Input;
448448
else
449449
{
450-
switch (modes[i])
450+
param.Direction = modes[i] switch
451451
{
452-
case 'i':
453-
param.Direction = ParameterDirection.Input;
454-
break;
455-
case 'o':
456-
case 't':
457-
param.Direction = ParameterDirection.Output;
458-
break;
459-
case 'b':
460-
param.Direction = ParameterDirection.InputOutput;
461-
break;
462-
case 'v':
463-
throw new NotImplementedException("Cannot derive function parameter of type VARIADIC");
464-
default:
465-
throw new ArgumentOutOfRangeException("Unknown code in proargmodes while deriving: " + modes[i]);
466-
}
452+
'i' => ParameterDirection.Input,
453+
'o' => ParameterDirection.Output,
454+
't' => ParameterDirection.Output,
455+
'b' => ParameterDirection.InputOutput,
456+
'v' => throw new NotSupportedException("Cannot derive function parameter of type VARIADIC"),
457+
_ => throw new ArgumentOutOfRangeException("Unknown code in proargmodes while deriving: " + modes[i])
458+
};
467459
}
468460

469461
Parameters.Add(param);

src/Npgsql/NpgsqlConnection.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -421,29 +421,20 @@ public ConnectionState FullState
421421
get
422422
{
423423
if (Connector == null || _disposed)
424-
{
425424
return _wasBroken ? ConnectionState.Broken : ConnectionState.Closed;
426-
}
427425

428-
switch (Connector.State)
426+
return Connector.State switch
429427
{
430-
case ConnectorState.Closed:
431-
return ConnectionState.Closed;
432-
case ConnectorState.Connecting:
433-
return ConnectionState.Connecting;
434-
case ConnectorState.Ready:
435-
return ConnectionState.Open;
436-
case ConnectorState.Executing:
437-
return ConnectionState.Open | ConnectionState.Executing;
438-
case ConnectorState.Copy:
439-
case ConnectorState.Fetching:
440-
case ConnectorState.Waiting:
441-
return ConnectionState.Open | ConnectionState.Fetching;
442-
case ConnectorState.Broken:
443-
return ConnectionState.Broken;
444-
default:
445-
throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {Connector.State} of enum {nameof(ConnectorState)}. Please file a bug.");
446-
}
428+
ConnectorState.Closed => ConnectionState.Closed,
429+
ConnectorState.Connecting => ConnectionState.Connecting,
430+
ConnectorState.Ready => ConnectionState.Open,
431+
ConnectorState.Executing => ConnectionState.Open | ConnectionState.Executing,
432+
ConnectorState.Copy => ConnectionState.Open | ConnectionState.Fetching,
433+
ConnectorState.Fetching => ConnectionState.Open | ConnectionState.Fetching,
434+
ConnectorState.Waiting => ConnectionState.Open | ConnectionState.Fetching,
435+
ConnectorState.Broken => ConnectionState.Broken,
436+
_ => throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {Connector.State} of enum {nameof(ConnectorState)}. Please file a bug.")
437+
};
447438
}
448439
}
449440

src/Npgsql/NpgsqlConnector.cs

Lines changed: 44 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -334,26 +334,18 @@ internal ConnectorState State
334334
/// Returns whether the connector is open, regardless of any task it is currently performing
335335
/// </summary>
336336
bool IsConnected
337-
{
338-
get
337+
=> State switch
339338
{
340-
switch (State)
341-
{
342-
case ConnectorState.Ready:
343-
case ConnectorState.Executing:
344-
case ConnectorState.Fetching:
345-
case ConnectorState.Waiting:
346-
case ConnectorState.Copy:
347-
return true;
348-
case ConnectorState.Closed:
349-
case ConnectorState.Connecting:
350-
case ConnectorState.Broken:
351-
return false;
352-
default:
353-
throw new ArgumentOutOfRangeException("Unknown state: " + State);
354-
}
355-
}
356-
}
339+
ConnectorState.Ready => true,
340+
ConnectorState.Executing => true,
341+
ConnectorState.Fetching => true,
342+
ConnectorState.Waiting => true,
343+
ConnectorState.Copy => true,
344+
ConnectorState.Closed => false,
345+
ConnectorState.Connecting => false,
346+
ConnectorState.Broken => false,
347+
_ => throw new ArgumentOutOfRangeException("Unknown state: " + State)
348+
};
357349

358350
internal bool IsReady => State == ConnectorState.Ready;
359351
internal bool IsClosed => State == ConnectorState.Closed;
@@ -1008,55 +1000,41 @@ internal ValueTask<IBackendMessage> ReadMessage(bool async, DataRowLoadingMode d
10081000

10091001
case BackendMessageCode.AuthenticationRequest:
10101002
var authType = (AuthenticationRequestType)buf.ReadInt32();
1011-
switch (authType)
1003+
return authType switch
10121004
{
1013-
case AuthenticationRequestType.AuthenticationOk:
1014-
return AuthenticationOkMessage.Instance;
1015-
case AuthenticationRequestType.AuthenticationCleartextPassword:
1016-
return AuthenticationCleartextPasswordMessage.Instance;
1017-
case AuthenticationRequestType.AuthenticationMD5Password:
1018-
return AuthenticationMD5PasswordMessage.Load(buf);
1019-
case AuthenticationRequestType.AuthenticationGSS:
1020-
return AuthenticationGSSMessage.Instance;
1021-
case AuthenticationRequestType.AuthenticationSSPI:
1022-
return AuthenticationSSPIMessage.Instance;
1023-
case AuthenticationRequestType.AuthenticationGSSContinue:
1024-
return AuthenticationGSSContinueMessage.Load(buf, len);
1025-
case AuthenticationRequestType.AuthenticationSASL:
1026-
return new AuthenticationSASLMessage(buf);
1027-
case AuthenticationRequestType.AuthenticationSASLContinue:
1028-
return new AuthenticationSASLContinueMessage(buf, len - 4);
1029-
case AuthenticationRequestType.AuthenticationSASLFinal:
1030-
return new AuthenticationSASLFinalMessage(buf, len - 4);
1031-
default:
1032-
throw new NotSupportedException($"Authentication method not supported (Received: {authType})");
1033-
}
1005+
AuthenticationRequestType.AuthenticationOk => (AuthenticationRequestMessage)AuthenticationOkMessage.Instance,
1006+
AuthenticationRequestType.AuthenticationCleartextPassword => AuthenticationCleartextPasswordMessage.Instance,
1007+
AuthenticationRequestType.AuthenticationMD5Password => AuthenticationMD5PasswordMessage.Load(buf),
1008+
AuthenticationRequestType.AuthenticationGSS => AuthenticationGSSMessage.Instance,
1009+
AuthenticationRequestType.AuthenticationSSPI => AuthenticationSSPIMessage.Instance,
1010+
AuthenticationRequestType.AuthenticationGSSContinue => AuthenticationGSSContinueMessage.Load(buf, len),
1011+
AuthenticationRequestType.AuthenticationSASL => new AuthenticationSASLMessage(buf),
1012+
AuthenticationRequestType.AuthenticationSASLContinue => new AuthenticationSASLContinueMessage(buf, len - 4),
1013+
AuthenticationRequestType.AuthenticationSASLFinal => new AuthenticationSASLFinalMessage(buf, len - 4),
1014+
_ => throw new NotSupportedException($"Authentication method not supported (Received: {authType})")
1015+
};
10341016

10351017
case BackendMessageCode.BackendKeyData:
10361018
return new BackendKeyDataMessage(buf);
10371019

10381020
case BackendMessageCode.CopyInResponse:
1039-
_copyInResponseMessage ??= new CopyInResponseMessage();
1040-
return _copyInResponseMessage.Load(ReadBuffer);
1041-
1021+
return (_copyInResponseMessage ??= new CopyInResponseMessage()).Load(ReadBuffer);
10421022
case BackendMessageCode.CopyOutResponse:
1043-
_copyOutResponseMessage ??= new CopyOutResponseMessage();
1044-
return _copyOutResponseMessage.Load(ReadBuffer);
1045-
1023+
return (_copyOutResponseMessage ??= new CopyOutResponseMessage()).Load(ReadBuffer);
10461024
case BackendMessageCode.CopyData:
1047-
_copyDataMessage ??= new CopyDataMessage();
1048-
return _copyDataMessage.Load(len);
1049-
1025+
return (_copyDataMessage ??= new CopyDataMessage()).Load(len);
10501026
case BackendMessageCode.CopyDone:
10511027
return CopyDoneMessage.Instance;
10521028

10531029
case BackendMessageCode.PortalSuspended:
10541030
throw new NpgsqlException("Unimplemented message: " + code);
10551031
case BackendMessageCode.ErrorResponse:
10561032
return null;
1033+
10571034
case BackendMessageCode.FunctionCallResponse:
10581035
// We don't use the obsolete function call protocol
10591036
throw new NpgsqlException("Unexpected backend message: " + code);
1037+
10601038
default:
10611039
throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {code} of enum {nameof(BackendMessageCode)}. Please file a bug.");
10621040
}
@@ -1093,22 +1071,15 @@ internal Task Rollback(bool async)
10931071
}
10941072

10951073
internal bool InTransaction
1096-
{
1097-
get
1074+
=> TransactionStatus switch
10981075
{
1099-
switch (TransactionStatus)
1100-
{
1101-
case TransactionStatus.Idle:
1102-
return false;
1103-
case TransactionStatus.Pending:
1104-
case TransactionStatus.InTransactionBlock:
1105-
case TransactionStatus.InFailedTransactionBlock:
1106-
return true;
1107-
default:
1108-
throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {TransactionStatus} of enum {nameof(TransactionStatus)}. Please file a bug.");
1109-
}
1110-
}
1111-
}
1076+
TransactionStatus.Idle => false,
1077+
TransactionStatus.Pending => true,
1078+
TransactionStatus.InTransactionBlock => true,
1079+
TransactionStatus.InFailedTransactionBlock => true,
1080+
_ => throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {TransactionStatus} of enum {nameof(TransactionStatus)}. Please file a bug.")
1081+
};
1082+
11121083
/// <summary>
11131084
/// Handles a new transaction indicator received on a ReadyForQuery message
11141085
/// </summary>
@@ -1117,17 +1088,14 @@ void ProcessNewTransactionStatus(TransactionStatus newStatus)
11171088
if (newStatus == TransactionStatus)
11181089
return;
11191090

1120-
switch (newStatus) {
1121-
case TransactionStatus.Idle:
1122-
case TransactionStatus.InTransactionBlock:
1123-
case TransactionStatus.InFailedTransactionBlock:
1124-
TransactionStatus = newStatus;
1125-
return;
1126-
case TransactionStatus.Pending:
1127-
throw new Exception("Invalid TransactionStatus (should be frontend-only)");
1128-
default:
1129-
throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {newStatus} of enum {nameof(TransactionStatus)}. Please file a bug.");
1130-
}
1091+
TransactionStatus = newStatus switch
1092+
{
1093+
TransactionStatus.Idle => newStatus,
1094+
TransactionStatus.InTransactionBlock => newStatus,
1095+
TransactionStatus.InFailedTransactionBlock => newStatus,
1096+
TransactionStatus.Pending => throw new Exception("Invalid TransactionStatus (should be frontend-only)"),
1097+
_ => throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {newStatus} of enum {nameof(TransactionStatus)}. Please file a bug.")
1098+
};
11311099
}
11321100

11331101
void ClearTransaction()

0 commit comments

Comments
 (0)