Skip to content

Commit

Permalink
fix mac compile issue by switching to pointer
Browse files Browse the repository at this point in the history
Switch jsonPrintOptions to be a pointer in p4RuntimeSerializer.h to
avoid having to import the protobuf headers (causes failure on the Mac
CI build).
  • Loading branch information
grg committed Feb 12, 2024
1 parent 3246bd1 commit 88e0467
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion control-plane/p4RuntimeArchHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class P4RuntimeArchHandlerIface {
virtual bool filterAnnotations(cstring anno) = 0;

/// Control how JSON is output
virtual google::protobuf::util::JsonPrintOptions getJsonPrintOptions() = 0;
virtual google::protobuf::util::JsonPrintOptions *getJsonPrintOptions() = 0;
};

/// A functor interface that needs to be implemented for each
Expand Down
4 changes: 2 additions & 2 deletions control-plane/p4RuntimeArchStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {
const IR::ExternBlock *) override {}
bool filterAnnotations(cstring) override { return false; }

google::protobuf::util::JsonPrintOptions getJsonPrintOptions() override {
return jsonPrintOptions;
google::protobuf::util::JsonPrintOptions *getJsonPrintOptions() override {
return &jsonPrintOptions;
}

static std::optional<ActionProfile> getActionProfile(cstring name, const IR::Type_Extern *type,
Expand Down
11 changes: 9 additions & 2 deletions control-plane/p4RuntimeSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ void P4RuntimeAPI::serializeP4InfoTo(std::ostream *destination, P4RuntimeFormat
success = writers::writeTo(*p4Info, destination);
break;
case P4RuntimeFormat::JSON:
success = writers::writeJsonTo(*p4Info, destination, jsonPrintOptions);
success = writers::writeJsonTo(*p4Info, destination, *jsonPrintOptions);
break;
case P4RuntimeFormat::TEXT_PROTOBUF:
case P4RuntimeFormat::TEXT:
Expand All @@ -1482,7 +1482,7 @@ void P4RuntimeAPI::serializeEntriesTo(std::ostream *destination, P4RuntimeFormat
success = writers::writeTo(*entries, destination);
break;
case P4RuntimeFormat::JSON:
success = writers::writeJsonTo(*entries, destination, jsonPrintOptions);
success = writers::writeJsonTo(*entries, destination, *jsonPrintOptions);
break;
case P4RuntimeFormat::TEXT_PROTOBUF:
case P4RuntimeFormat::TEXT:
Expand All @@ -1494,6 +1494,13 @@ void P4RuntimeAPI::serializeEntriesTo(std::ostream *destination, P4RuntimeFormat
"Failed to serialize the P4Runtime static table entries to the output");
}

P4RuntimeAPI::P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info,
const ::p4::v1::WriteRequest *entries)
: p4Info(p4Info), entries(entries) {
jsonPrintOptions = new google::protobuf::util::JsonPrintOptions();
jsonPrintOptions->add_whitespace = true;
}

static bool parseFileNames(cstring fileNameVector, std::vector<cstring> &files,
std::vector<P4::P4RuntimeFormat> &formats) {
for (auto current = fileNameVector; current;) {
Expand Down
24 changes: 11 additions & 13 deletions control-plane/p4RuntimeSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ limitations under the License.
#ifndef CONTROL_PLANE_P4RUNTIMESERIALIZER_H_
#define CONTROL_PLANE_P4RUNTIMESERIALIZER_H_

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <google/protobuf/util/json_util.h>
#pragma GCC diagnostic pop

#include <iosfwd>
#include <unordered_map>

#include "lib/cstring.h"

namespace google {
namespace protobuf {
namespace util {
class JsonPrintOptions;
} // namespace util
} // namespace protobuf
} // namespace google
namespace p4 {
namespace config {
namespace v1 {
Expand Down Expand Up @@ -67,16 +68,13 @@ struct P4RuntimeAPI {
/// null.
const ::p4::v1::WriteRequest *entries;

// Print options to use while outputting JSON.
google::protobuf::util::JsonPrintOptions jsonPrintOptions;
/// Print options to use while outputting JSON.
google::protobuf::util::JsonPrintOptions *jsonPrintOptions;

P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries)
: p4Info(p4Info), entries(entries) {
jsonPrintOptions.add_whitespace = true;
}
P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries);

P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries,
google::protobuf::util::JsonPrintOptions jsonPrintOptions)
google::protobuf::util::JsonPrintOptions *jsonPrintOptions)
: p4Info(p4Info), entries(entries), jsonPrintOptions(jsonPrintOptions) {}
};

Expand Down
2 changes: 1 addition & 1 deletion test/gtest/p4runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ TEST_F(P4Runtime, JsonSerializationPrintOptions) {
{
// Disable adding whitespace: json should not contain whitespace
std::ostringstream json_output;
test->jsonPrintOptions.add_whitespace = false;
test->jsonPrintOptions->add_whitespace = false;
test->serializeP4InfoTo(&json_output, P4::P4RuntimeFormat::JSON);
EXPECT_EQ(json_output.str().find(' '), std::string::npos);
}
Expand Down

0 comments on commit 88e0467

Please sign in to comment.