Skip to content

Commit

Permalink
Replace DebugString with text proto printer
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Avlasov <yavlasov@google.com>
  • Loading branch information
yanavlasov committed Mar 28, 2024
1 parent 522b0b3 commit 6fd2297
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
10 changes: 10 additions & 0 deletions source/common/protobuf/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ void MessageUtil::wireCast(const Protobuf::Message& src, Protobuf::Message& dst)
}
}

std::string MessageUtil::toTextProto(const Protobuf::Message& message) {
std::string text_format;
Protobuf::TextFormat::Printer printer;
printer.SetExpandAny(true);
printer.SetHideUnknownFields(true);
bool result = printer.PrintToString(message, &text_format);
ASSERT(result);
return text_format;
}

bool ValueUtil::equal(const ProtobufWkt::Value& v1, const ProtobufWkt::Value& v2) {
ProtobufWkt::Value::KindCase kind = v1.kind_case();
if (kind != v2.kind_case()) {
Expand Down
7 changes: 7 additions & 0 deletions source/common/protobuf/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,13 @@ class MessageUtil {
* the input string is valid UTF-8, it will be returned unmodified.
*/
static std::string sanitizeUtf8String(absl::string_view str);

/**
* Return text proto representation of the `message`.
* @param message proto to print.
* @return text representation of the proto `message`.
*/
static std::string toTextProto(const Protobuf::Message& message);
};

class ValueUtil {
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/common/tap/tap_config_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void FilePerTapSink::FilePerTapSinkHandle::submitTrace(
break;
}
case envoy::config::tap::v3::OutputSink::PROTO_TEXT:
output_file_ << trace->DebugString();
output_file_ << MessageUtil::toTextProto(*trace);
break;
case envoy::config::tap::v3::OutputSink::JSON_BODY_AS_BYTES:
case envoy::config::tap::v3::OutputSink::JSON_BODY_AS_STRING:
Expand Down
8 changes: 4 additions & 4 deletions test/config/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1751,8 +1751,8 @@ void CdsHelper::setCds(const std::vector<envoy::config::cluster::v3::Cluster>& c
}
// Past the initial write, need move semantics to trigger inotify move event that the
// FilesystemSubscriptionImpl is subscribed to.
std::string path =
TestEnvironment::writeStringToFileForTest("cds.update.pb_text", cds_response.DebugString());
std::string path = TestEnvironment::writeStringToFileForTest(
"cds.update.pb_text", MessageUtil::toTextProto(cds_response));
TestEnvironment::renameFile(path, cds_path_);
}

Expand All @@ -1773,8 +1773,8 @@ void EdsHelper::setEds(const std::vector<envoy::config::endpoint::v3::ClusterLoa
}
// Past the initial write, need move semantics to trigger inotify move event that the
// FilesystemSubscriptionImpl is subscribed to.
std::string path =
TestEnvironment::writeStringToFileForTest("eds.update.pb_text", eds_response.DebugString());
std::string path = TestEnvironment::writeStringToFileForTest(
"eds.update.pb_text", MessageUtil::toTextProto(eds_response));
TestEnvironment::renameFile(path, eds_path_);
}

Expand Down

0 comments on commit 6fd2297

Please sign in to comment.