forked from sonic-net/sonic-pins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.cc
178 lines (145 loc) · 5.82 KB
/
proto.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gutil/proto.h"
#include <fcntl.h>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
#include "gutil/proto_string_error_collector.h"
#include "gutil/status.h"
namespace gutil {
bool IsEmptyProto(const google::protobuf::Message &message) {
return message.ByteSizeLong() == 0;
}
absl::Status ReadProtoFromFile(absl::string_view filename,
google::protobuf::Message *message) {
// Verifies that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
int fd = open(std::string(filename).c_str(), O_RDONLY);
if (fd < 0) {
return InvalidArgumentErrorBuilder()
<< "Error opening the file " << filename;
}
google::protobuf::io::FileInputStream file_stream(fd);
file_stream.SetCloseOnDelete(true);
if (!google::protobuf::TextFormat::Parse(&file_stream, message)) {
return InvalidArgumentErrorBuilder() << "Failed to parse file " << filename;
}
return absl::OkStatus();
}
absl::Status ReadProtoFromString(absl::string_view proto_string,
google::protobuf::Message *message) {
// Verifies that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
google::protobuf::TextFormat::Parser parser;
std::string all_errors;
StringErrorCollector collector(&all_errors);
parser.RecordErrorsTo(&collector);
if (!parser.ParseFromString(std::string(proto_string), message)) {
return InvalidArgumentErrorBuilder()
<< "string <" << proto_string << "> did not parse as a"
<< message->GetTypeName() << ":\n"
<< all_errors;
}
return absl::OkStatus();
}
absl::Status SaveProtoToFile(absl::string_view filename,
const google::protobuf::Message &message) {
// Verifies that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
int fd = open(std::string(filename).c_str(), O_WRONLY | O_CREAT, 0644);
if (fd < 0) {
return InvalidArgumentErrorBuilder()
<< "Error opening the file " << filename;
}
google::protobuf::io::FileOutputStream file_stream(fd);
file_stream.SetCloseOnDelete(true);
if (!google::protobuf::TextFormat::Print(message, &file_stream)) {
return InvalidArgumentErrorBuilder()
<< "Failed to print proto to file " << filename;
}
file_stream.Flush();
return absl::OkStatus();
}
absl::StatusOr<std::string> ProtoDiff(
const google::protobuf::Message &message1,
const google::protobuf::Message &message2,
google::protobuf::util::MessageDifferencer differ) {
if (message1.GetDescriptor() != message2.GetDescriptor()) {
return gutil::InvalidArgumentErrorBuilder()
<< "cannot compute diff for messages of incompatible descriptors `"
<< message1.GetDescriptor()->full_name() << "' vs '"
<< message2.GetDescriptor()->full_name() << "'";
}
std::string diff;
differ.ReportDifferencesToString(&diff);
differ.Compare(message1, message2);
return diff;
}
bool ProtoEqual(const google::protobuf::Message &message1,
const google::protobuf::Message &message2,
google::protobuf::util::MessageDifferencer &differ) {
if (message1.GetDescriptor() != message2.GetDescriptor()) {
return false;
}
return differ.Compare(message1, message2);
}
absl::StatusOr<std::string> GetOneOfFieldName(
const google::protobuf::Message &message, const std::string &oneof_name) {
const auto *oneof_descriptor =
message.GetDescriptor()->FindOneofByName(oneof_name);
const auto *field = message.GetReflection()->GetOneofFieldDescriptor(
message, oneof_descriptor);
if (!field) {
return gutil::NotFoundErrorBuilder()
<< "Oneof field \"" << oneof_name << "\" is not set";
}
return field->name();
}
std::string PrintTextProto(const google::protobuf::Message &message) {
std::string message_text;
google::protobuf::TextFormat::Printer printer;
printer.SetExpandAny(true);
printer.PrintToString(message, &message_text);
return message_text;
}
// Print proto in TextFormat with single line mode enabled.
std::string PrintShortTextProto(const google::protobuf::Message &message) {
std::string message_short_text;
google::protobuf::TextFormat::Printer printer;
printer.SetSingleLineMode(true);
printer.SetExpandAny(true);
printer.PrintToString(message, &message_short_text);
// Single line mode currently might have an extra space at the end.
if (!message_short_text.empty() && message_short_text.back() == ' ') {
message_short_text.pop_back();
}
return message_short_text;
}
absl::StatusOr<std::string> SerializeProtoAsJson(
const google::protobuf::Message &proto) {
std::string json;
RETURN_IF_ERROR(gutil::ToAbslStatus(
google::protobuf::util::MessageToJsonString(proto, &json)));
return json;
}
} // namespace gutil