Skip to content

Commit 6fea3da

Browse files
authored
[lldb] Simplify handling of empty strings for MCP (NFC) (#148317)
Instead of storing a `std::optional<std::string>`, directly use a `std::string` and treat a missing value the same was as an empty string.
1 parent 5a95ec6 commit 6fea3da

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

lldb/source/Plugins/Protocol/MCP/Protocol.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool fromJSON(const llvm::json::Value &V, Request &R, llvm::json::Path P) {
4242

4343
llvm::json::Value toJSON(const ErrorInfo &EI) {
4444
llvm::json::Object Result{{"code", EI.code}, {"message", EI.message}};
45-
if (EI.data)
45+
if (!EI.data.empty())
4646
Result.insert({"data", EI.data});
4747
return Result;
4848
}
@@ -132,9 +132,9 @@ bool fromJSON(const llvm::json::Value &V, Resource &R, llvm::json::Path P) {
132132

133133
llvm::json::Value toJSON(const Resource &R) {
134134
llvm::json::Object Result{{"uri", R.uri}, {"name", R.name}};
135-
if (R.description)
135+
if (!R.description.empty())
136136
Result.insert({"description", R.description});
137-
if (R.mimeType)
137+
if (!R.mimeType.empty())
138138
Result.insert({"mimeType", R.mimeType});
139139
return Result;
140140
}
@@ -146,7 +146,7 @@ bool fromJSON(const llvm::json::Value &V, Capabilities &C, llvm::json::Path P) {
146146

147147
llvm::json::Value toJSON(const ResourceContents &RC) {
148148
llvm::json::Object Result{{"uri", RC.uri}, {"text", RC.text}};
149-
if (RC.mimeType)
149+
if (!RC.mimeType.empty())
150150
Result.insert({"mimeType", RC.mimeType});
151151
return Result;
152152
}
@@ -188,7 +188,7 @@ bool fromJSON(const llvm::json::Value &V, TextResult &TR, llvm::json::Path P) {
188188

189189
llvm::json::Value toJSON(const ToolDefinition &TD) {
190190
llvm::json::Object Result{{"name", TD.name}};
191-
if (TD.description)
191+
if (!TD.description.empty())
192192
Result.insert({"description", TD.description});
193193
if (TD.inputSchema)
194194
Result.insert({"inputSchema", TD.inputSchema});

lldb/source/Plugins/Protocol/MCP/Protocol.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path);
3636
struct ErrorInfo {
3737
int64_t code = 0;
3838
std::string message;
39-
std::optional<std::string> data;
39+
std::string data;
4040
};
4141

4242
llvm::json::Value toJSON(const ErrorInfo &);
@@ -112,10 +112,10 @@ struct Resource {
112112
std::string name;
113113

114114
/// A description of what this resource represents.
115-
std::optional<std::string> description;
115+
std::string description;
116116

117117
/// The MIME type of this resource, if known.
118-
std::optional<std::string> mimeType;
118+
std::string mimeType;
119119
};
120120

121121
llvm::json::Value toJSON(const Resource &);
@@ -131,7 +131,7 @@ struct ResourceContents {
131131
std::string text;
132132

133133
/// The MIME type of this resource, if known.
134-
std::optional<std::string> mimeType;
134+
std::string mimeType;
135135
};
136136

137137
llvm::json::Value toJSON(const ResourceContents &);
@@ -167,7 +167,7 @@ struct ToolDefinition {
167167
std::string name;
168168

169169
/// Human-readable description.
170-
std::optional<std::string> description;
170+
std::string description;
171171

172172
// JSON Schema for the tool's parameters.
173173
std::optional<llvm::json::Value> inputSchema;

lldb/source/Plugins/Protocol/MCP/Tool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Tool::Tool(std::string name, std::string description)
4545
protocol::ToolDefinition Tool::GetDefinition() const {
4646
protocol::ToolDefinition definition;
4747
definition.name = m_name;
48-
definition.description.emplace(m_description);
48+
definition.description = m_description;
4949

5050
if (std::optional<llvm::json::Value> input_schema = GetSchema())
5151
definition.inputSchema = *input_schema;

lldb/unittests/Protocol/ProtocolMCPTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ TEST(ProtocolMCPTest, ResourceWithoutOptionals) {
257257

258258
EXPECT_EQ(resource.uri, deserialized_resource->uri);
259259
EXPECT_EQ(resource.name, deserialized_resource->name);
260-
EXPECT_FALSE(deserialized_resource->description.has_value());
261-
EXPECT_FALSE(deserialized_resource->mimeType.has_value());
260+
EXPECT_TRUE(deserialized_resource->description.empty());
261+
EXPECT_TRUE(deserialized_resource->mimeType.empty());
262262
}
263263

264264
TEST(ProtocolMCPTest, ResourceContents) {
@@ -287,7 +287,7 @@ TEST(ProtocolMCPTest, ResourceContentsWithoutMimeType) {
287287

288288
EXPECT_EQ(contents.uri, deserialized_contents->uri);
289289
EXPECT_EQ(contents.text, deserialized_contents->text);
290-
EXPECT_FALSE(deserialized_contents->mimeType.has_value());
290+
EXPECT_TRUE(deserialized_contents->mimeType.empty());
291291
}
292292

293293
TEST(ProtocolMCPTest, ResourceResult) {

0 commit comments

Comments
 (0)