Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pandaproxy/sr: protobuf rendering: refactor string rendering #24992

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/v/pandaproxy/schema_registry/protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <absl/container/flat_hash_set.h>
#include <absl/strings/ascii.h>
#include <absl/strings/escaping.h>
#include <boost/algorithm/string/trim.hpp>
#include <boost/range/combine.hpp>
#include <confluent/meta.pb.h>
Expand Down Expand Up @@ -77,6 +78,16 @@
#include <string_view>
#include <unordered_set>

namespace {

// Protobuf string values need to be quoted and escaped
// as they may contain characters like '\'
auto pb_string_value(const std::string_view v) {
return fmt::format("\"{}\"", absl::CEscape(v));
}

} // namespace

struct indent_formatter : fmt::formatter<std::string_view> {
using Base = fmt::formatter<std::string_view>;
constexpr auto parse(fmt::format_parse_context& ctx) {
Expand Down Expand Up @@ -138,9 +149,9 @@ struct fmt::formatter<google::protobuf::UninterpretedOption>
if (option.has_string_value()) {
return fmt::format_to(
ctx.out(),
"{} = {:?}",
"{} = {}",
fmt::join(option.name(), "."),
val);
pb_string_value(val));
}
}
if (option.has_aggregate_value()) {
Expand Down Expand Up @@ -920,7 +931,8 @@ struct protobuf_schema_definition::impl {
}
if (field.has_json_name()) {
maybe_print_seperator();
fmt::print(os, "json_name = {:?}", field.json_name());
fmt::print(
os, "json_name = {}", pb_string_value(field.json_name()));
}
if (field.has_options()) {
const auto& options = field.options();
Expand Down Expand Up @@ -1076,11 +1088,12 @@ struct protobuf_schema_definition::impl {

if (decl.has_full_name()) {
maybe_print_seperator();
fmt::print(os, "{}: {:?}", "full_name", decl.full_name());
fmt::print(
os, "{}: {}", "full_name", pb_string_value(decl.full_name()));
}
if (decl.has_type()) {
maybe_print_seperator();
fmt::print(os, "{}: {:?}", "type", decl.type());
fmt::print(os, "{}: {}", "type", pb_string_value(decl.type()));
}
if (decl.has_number()) {
maybe_print_seperator();
Expand Down Expand Up @@ -1163,7 +1176,7 @@ struct protobuf_schema_definition::impl {
auto reserved_names = maybe_sorted(message.reserved_name());
if (!reserved_names.empty()) {
const auto to_debug_string = [](const std::string_view strv) {
return fmt::format("{:?}", strv);
return fmt::format("{}", pb_string_value(strv));
};
fmt::print(
os,
Expand Down Expand Up @@ -1292,7 +1305,12 @@ struct protobuf_schema_definition::impl {
fmt::print(os, ";\n");
}
for (const auto& value : maybe_sorted(enum_proto.reserved_name())) {
fmt::print(os, "{:{}}reserved {:?};\n", "", indent + 2, value);
fmt::print(
os,
"{:{}}reserved {};\n",
"",
indent + 2,
pb_string_value(value));
}
if (enum_proto.options().has_allow_alias()) {
fmt::print(
Expand Down Expand Up @@ -1434,7 +1452,7 @@ struct protobuf_schema_definition::impl {
first_option = false;
};
auto prints = [&](std::string_view name, const auto& val) {
fmt::print(os, "option {} = {:?};\n", name, val);
fmt::print(os, "option {} = {};\n", name, pb_string_value(val));
first_option = false;
};
if (options.has_cc_enable_arenas()) {
Expand Down Expand Up @@ -1551,7 +1569,7 @@ struct protobuf_schema_definition::impl {

auto print_deps = [&](const auto& view, std::string_view type) {
for (const auto& dep : view) {
fmt::print(os, "import {}{:?};\n", type, dep);
fmt::print(os, "import {}{};\n", type, pb_string_value(dep));
}
};

Expand All @@ -1569,9 +1587,12 @@ struct protobuf_schema_definition::impl {
auto syntax = fdp.has_syntax() ? fdp.syntax() : "proto2";
edition = syntax == "proto3" ? pb::Edition::EDITION_PROTO3
: pb::Edition::EDITION_PROTO2;
fmt::print(os, "syntax = {:?};\n", syntax);
fmt::print(os, "syntax = {};\n", pb_string_value(syntax));
} else {
fmt::print(os, "edition = {:?};\n", Edition_Name(fdp.edition()));
fmt::print(
os,
"edition = {};\n",
pb_string_value(Edition_Name(fdp.edition())));
}

if (fdp.has_package() && !fdp.package().empty()) {
Expand Down