Skip to content

Commit 347fb04

Browse files
iahsfacebook-github-bot
authored andcommitted
Handle quotes in annotations codemod
Summary: We encounter this for cpp.methods, which sometimes injects constructors with default string arguments Reviewed By: yoney Differential Revision: D68931171 fbshipit-source-id: 928c9a104f0bce14d2f76cca89c668f86484f264
1 parent 3ba2e26 commit 347fb04

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

third-party/thrift/src/thrift/compiler/codemod/structure_annotations.cc

+17-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ bool is_container(const t_type& type) {
3333
return true_type && true_type->is_container();
3434
}
3535

36+
std::string quote(std::string_view str) {
37+
if (str.find('"') == std::string::npos) {
38+
return fmt::format("\"{}\"", str);
39+
} else if (str.find('\'') == std::string::npos) {
40+
return fmt::format("'{}'", str);
41+
} else {
42+
std::string out(str);
43+
size_t start_pos = 0;
44+
while ((start_pos = out.find('"', start_pos)) != std::string::npos) {
45+
out.replace(start_pos, 1, "\\\"");
46+
start_pos += 2;
47+
}
48+
return fmt::format("\"{}\"", out);
49+
}
50+
}
51+
3652
class structure_annotations {
3753
public:
3854
structure_annotations(source_manager& sm, t_program& program)
@@ -516,7 +532,7 @@ class structure_annotations {
516532
std::vector<std::string> annotations_for_catch_all_strs;
517533
for (const auto& [name, value] : annotations_for_catch_all) {
518534
annotations_for_catch_all_strs.push_back(
519-
fmt::format(R"("{}": "{}")", name, value));
535+
fmt::format(R"("{}": {})", name, quote(value)));
520536
}
521537
to_add.insert(fmt::format(
522538
"@thrift.DeprecatedUnvalidatedAnnotations{{items = {{{}}}}}",

third-party/thrift/src/thrift/compiler/codemod/structure_annotations_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def test_remaining(self):
489489
include "thrift.thrift"
490490
491491
struct S {
492-
1: i32 field1 (foo);
492+
1: i32 field1 (foo, quote = '"', both_quotes = "'\\"'");
493493
}(foo, bar = "baz")
494494
495495
typedef i32 (foo, hs.type = "hs") T (bar = "baz", hs.category = "value")
@@ -530,7 +530,7 @@ def test_remaining(self):
530530
531531
@thrift.DeprecatedUnvalidatedAnnotations{items = {"bar": "baz", "foo": "1"}}
532532
struct S {
533-
@thrift.DeprecatedUnvalidatedAnnotations{items = {"foo": "1"}}
533+
@thrift.DeprecatedUnvalidatedAnnotations{items = {"both_quotes": "'\\"'", "foo": "1", "quote": '"'}}
534534
1: i32 field1 ;
535535
}
536536

0 commit comments

Comments
 (0)