Skip to content

Commit 76def90

Browse files
Use property pattern matching on union for clearer code
Refactored the Read method to use switch expression with property pattern matching directly on the union object, instead of switching on a tuple of boolean flags. This makes the code more idiomatic and easier to read. Co-authored-by: Scooletz <scooletz@users.noreply.github.com> Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
1 parent 00d63a6 commit 76def90

1 file changed

Lines changed: 36 additions & 46 deletions

File tree

src/ModelContextProtocol.Core/Protocol/JsonRpcMessage.cs

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,57 +86,47 @@ public sealed class Converter : JsonConverter<JsonRpcMessage>
8686
}
8787

8888
// Determine message type based on presence of id and method properties
89-
switch ((union.HasId, union.HasMethod))
89+
return union switch
9090
{
91-
case (true, true):
92-
// Messages with both method and id are requests
93-
Debug.Assert(union.Method is not null, "HasMethod should only be true when Method is non-null");
94-
return new JsonRpcRequest
95-
{
96-
JsonRpc = union.JsonRpc,
97-
Id = union.Id,
98-
Method = union.Method!,
99-
Params = union.Params
100-
};
101-
102-
case (true, false):
103-
// Messages with an id but no method are responses
104-
if (union.HasError)
105-
{
106-
Debug.Assert(union.Error is not null, "HasError should only be true when Error is non-null");
107-
return new JsonRpcError
108-
{
109-
JsonRpc = union.JsonRpc,
110-
Id = union.Id,
111-
Error = union.Error!
112-
};
113-
}
91+
// Messages with both method and id are requests
92+
{ HasId: true, HasMethod: true } => new JsonRpcRequest
93+
{
94+
JsonRpc = union.JsonRpc,
95+
Id = union.Id,
96+
Method = union.Method!,
97+
Params = union.Params
98+
},
99+
100+
// Messages with an id and error are error responses
101+
{ HasId: true, HasError: true } => new JsonRpcError
102+
{
103+
JsonRpc = union.JsonRpc,
104+
Id = union.Id,
105+
Error = union.Error!
106+
},
114107

115-
if (union.HasResult)
116-
{
117-
return new JsonRpcResponse
118-
{
119-
JsonRpc = union.JsonRpc,
120-
Id = union.Id,
121-
Result = union.Result
122-
};
123-
}
108+
// Messages with an id and result are success responses
109+
{ HasId: true, HasResult: true } => new JsonRpcResponse
110+
{
111+
JsonRpc = union.JsonRpc,
112+
Id = union.Id,
113+
Result = union.Result
114+
},
124115

125-
throw new JsonException("Response must have either result or error");
116+
// Messages with an id but no method, error, or result are invalid
117+
{ HasId: true } => throw new JsonException("Response must have either result or error"),
126118

127-
case (false, true):
128-
// Messages with a method but no id are notifications
129-
Debug.Assert(union.Method is not null, "HasMethod should only be true when Method is non-null");
130-
return new JsonRpcNotification
131-
{
132-
JsonRpc = union.JsonRpc,
133-
Method = union.Method!,
134-
Params = union.Params
135-
};
119+
// Messages with a method but no id are notifications
120+
{ HasMethod: true } => new JsonRpcNotification
121+
{
122+
JsonRpc = union.JsonRpc,
123+
Method = union.Method!,
124+
Params = union.Params
125+
},
136126

137-
default:
138-
throw new JsonException("Invalid JSON-RPC message format");
139-
}
127+
// All other cases are invalid
128+
_ => throw new JsonException("Invalid JSON-RPC message format")
129+
};
140130
}
141131

142132
/// <inheritdoc/>

0 commit comments

Comments
 (0)