Skip to content

Commit 50235ca

Browse files
committed
Use stream json api in protobuf 31 and later versions
1 parent 74ca596 commit 50235ca

9 files changed

Lines changed: 31 additions & 253 deletions

File tree

.github/workflows/ci-linux.yml

Lines changed: 0 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -15,192 +15,14 @@ env:
1515

1616
# https://github.com/actions/runner-images
1717
jobs:
18-
compile-with-make:
19-
runs-on: ubuntu-22.04
20-
steps:
21-
- uses: actions/checkout@v2
22-
- uses: ./.github/actions/install-all-dependencies
23-
24-
- name: gcc with default options
25-
uses: ./.github/actions/compile-with-make
26-
with:
27-
options: --headers=/usr/include --libs=/usr/lib /usr/lib64 --cc=gcc --cxx=g++ --werror
28-
29-
- name: gcc with all options
30-
uses: ./.github/actions/compile-with-make
31-
with:
32-
options: --headers=/usr/include --libs=/usr/lib /usr/lib64 --cc=gcc --cxx=g++ --werror --with-thrift --with-glog --with-rdma --with-debug-bthread-sche-safety --with-debug-lock --with-bthread-tracer --with-asan
33-
34-
- name: clang with default options
35-
uses: ./.github/actions/compile-with-make
36-
with:
37-
options: --headers=/usr/include --libs=/usr/lib /usr/lib64 --cc=clang --cxx=clang++ --werror
38-
39-
- name: clang with all options
40-
uses: ./.github/actions/compile-with-make
41-
with:
42-
options: --headers=/usr/include --libs=/usr/lib /usr/lib64 --cc=clang --cxx=clang++ --werror --with-thrift --with-glog --with-rdma --with-debug-bthread-sche-safety --with-debug-lock --with-bthread-tracer --with-asan
43-
44-
compile-with-cmake:
45-
runs-on: ubuntu-22.04
46-
steps:
47-
- uses: actions/checkout@v2
48-
- uses: ./.github/actions/install-all-dependencies
49-
50-
- name: gcc with default options
51-
run: |
52-
export CC=gcc && export CXX=g++
53-
mkdir gcc_build && cd gcc_build && cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
54-
make -j ${{env.proc_num}} && make clean
55-
56-
- name: gcc with all options
57-
run: |
58-
export CC=gcc && export CXX=g++
59-
mkdir gcc_build_all && cd gcc_build_all
60-
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
61-
make -j ${{env.proc_num}} && make clean
62-
63-
- name: clang with default options
64-
run: |
65-
export CC=clang && export CXX=clang++
66-
mkdir clang_build && cd clang_build && cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
67-
make -j ${{env.proc_num}} && make clean
68-
69-
- name: clang with all options
70-
run: |
71-
export CC=clang && export CXX=clang++
72-
mkdir clang_build_all && cd clang_build_all
73-
cmake -DWITH_MESALINK=OFF -DWITH_GLOG=ON -DWITH_THRIFT=ON -DWITH_RDMA=ON -DWITH_DEBUG_BTHREAD_SCHE_SAFETY=ON -DWITH_DEBUG_LOCK=ON -DWITH_BTHREAD_TRACER=ON -DWITH_ASAN=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
74-
make -j ${{env.proc_num}} && make clean
75-
76-
gcc-compile-with-make-protobuf:
77-
runs-on: ubuntu-22.04
78-
steps:
79-
- uses: actions/checkout@v2
80-
- uses: ./.github/actions/install-essential-dependencies
81-
82-
- name: protobuf 3.5.1
83-
uses: ./.github/actions/compile-with-make-protobuf
84-
with:
85-
protobuf-version: 3.5.1
86-
protobuf-cpp-version: 3.5.1
87-
protobuf-install-dir: /protobuf-3.5.1
88-
config-brpc-options: --cc=gcc --cxx=g++ --werror
89-
90-
- name: protobuf 3.12.4
91-
uses: ./.github/actions/compile-with-make-protobuf
92-
with:
93-
protobuf-version: 3.12.4
94-
protobuf-cpp-version: 3.12.4
95-
protobuf-install-dir: /protobuf-3.12.4
96-
config-brpc-options: --cc=gcc --cxx=g++ --werror
97-
98-
- name: protobuf 21.12
99-
uses: ./.github/actions/compile-with-make-protobuf
100-
with:
101-
protobuf-version: 21.12
102-
protobuf-cpp-version: 3.21.12
103-
protobuf-install-dir: /protobuf-3.21.12
104-
config-brpc-options: --cc=gcc --cxx=g++ --werror
105-
10618
gcc-compile-with-bazel:
10719
runs-on: ubuntu-22.04
10820
steps:
10921
- uses: actions/checkout@v2
11022
- run: bazel build --verbose_failures -- //... -//example/...
11123

112-
gcc-compile-with-boringssl:
113-
runs-on: ubuntu-22.04
114-
steps:
115-
- uses: actions/checkout@v2
116-
- run: bazel build --verbose_failures --define with_mesalink=false --define with_glog=true --define with_thrift=true --define BRPC_WITH_BORINGSSL=true -- //... -//example/...
117-
118-
gcc-compile-with-bazel-all-options:
119-
runs-on: ubuntu-22.04
120-
steps:
121-
- uses: actions/checkout@v2
122-
- run: bazel build --verbose_failures --define with_mesalink=false --define with_glog=true --define with_thrift=true --define with_debug_bthread_sche_safety=true --define with_debug_lock=true --define with_asan=true -- //... -//example/...
123-
124-
clang-compile-with-make-protobuf:
125-
runs-on: ubuntu-22.04
126-
steps:
127-
- uses: actions/checkout@v2
128-
- uses: ./.github/actions/install-essential-dependencies
129-
130-
- name: protobuf 3.5.1
131-
uses: ./.github/actions/compile-with-make-protobuf
132-
with:
133-
protobuf-version: 3.5.1
134-
protobuf-cpp-version: 3.5.1
135-
protobuf-install-dir: /protobuf-3.5.1
136-
config-brpc-options: --cc=clang --cxx=clang++ --werror
137-
138-
- name: protobuf 3.12.4
139-
uses: ./.github/actions/compile-with-make-protobuf
140-
with:
141-
protobuf-version: 3.12.4
142-
protobuf-cpp-version: 3.12.4
143-
protobuf-install-dir: /protobuf-3.12.4
144-
config-brpc-options: --cc=clang --cxx=clang++ --werror
145-
146-
- name: protobuf 21.12
147-
uses: ./.github/actions/compile-with-make-protobuf
148-
with:
149-
protobuf-version: 21.12
150-
protobuf-cpp-version: 3.21.12
151-
protobuf-install-dir: /protobuf-3.21.12
152-
config-brpc-options: --cc=clang --cxx=clang++ --werror
153-
15424
clang-compile-with-bazel:
15525
runs-on: ubuntu-22.04
15626
steps:
15727
- uses: actions/checkout@v2
15828
- run: bazel build --verbose_failures --action_env=CC=clang -- //... -//example/...
159-
160-
clang-compile-with-boringssl:
161-
runs-on: ubuntu-22.04
162-
steps:
163-
- uses: actions/checkout@v2
164-
- run: bazel build --verbose_failures --action_env=CC=clang --define with_mesalink=false --define with_glog=true --define with_thrift=true --define BRPC_WITH_BORINGSSL=true -- //... -//example/...
165-
166-
clang-compile-with-bazel-all-options:
167-
runs-on: ubuntu-22.04
168-
steps:
169-
- uses: actions/checkout@v2
170-
- run: bazel build --verbose_failures --action_env=CC=clang --define with_mesalink=false --define with_glog=true --define with_thrift=true --define with_debug_bthread_sche_safety=true --define with_debug_lock=true --define with_asan=true -- //... -//example/...
171-
172-
clang-unittest:
173-
runs-on: ubuntu-22.04
174-
steps:
175-
- uses: actions/checkout@v2
176-
- uses: ./.github/actions/install-essential-dependencies
177-
- uses: ./.github/actions/init-ut-make-config
178-
with:
179-
options: --cc=clang-12 --cxx=clang++-12 --with-bthread-tracer
180-
- name: compile tests
181-
run: |
182-
cat config.mk
183-
cd test
184-
make -j ${{env.proc_num}}
185-
- name: run tests
186-
run: |
187-
cd test
188-
sh ./run_tests.sh
189-
190-
clang-unittest-asan:
191-
runs-on: ubuntu-22.04
192-
steps:
193-
- uses: actions/checkout@v2
194-
- uses: ./.github/actions/install-essential-dependencies
195-
- uses: ./.github/actions/init-ut-make-config
196-
with:
197-
options: --cc=clang-12 --cxx=clang++-12 --with-bthread-tracer --with-asan
198-
- name: compile tests
199-
run: |
200-
cat config.mk
201-
cd test
202-
make NEED_GPERFTOOLS=0 -j ${{env.proc_num}}
203-
- name: run tests
204-
run: |
205-
cd test
206-
sh ./run_tests.sh

.github/workflows/ci-macos.yml

Lines changed: 0 additions & 65 deletions
This file was deleted.

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module(
88
bazel_dep(name = 'abseil-cpp', version = '20210324.2', repo_name = 'com_google_absl')
99
bazel_dep(name = 'bazel_skylib', version = '1.0.3')
1010
bazel_dep(name = 'boringssl', version = '0.0.0-20211025-d4f1ab9')
11-
bazel_dep(name = 'protobuf', version = '27.3', repo_name = 'com_google_protobuf')
11+
bazel_dep(name = 'protobuf', version = '31.0', repo_name = 'com_google_protobuf')
1212
bazel_dep(name = 'gflags', version = '2.2.2', repo_name = 'com_github_gflags_gflags')
1313
bazel_dep(name = 'glog', version = '0.5.0', repo_name = 'com_github_google_glog')
1414
bazel_dep(name = 'platforms', version = '0.0.4')

src/butil/iobuf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ void IOBufAsZeroCopyOutputStream::BackUp(int count) {
21452145
LOG_IF(FATAL, count != 0) << "BackUp an empty IOBuf";
21462146
}
21472147

2148-
google::protobuf::int64 IOBufAsZeroCopyOutputStream::ByteCount() const {
2148+
int64_t IOBufAsZeroCopyOutputStream::ByteCount() const {
21492149
return _byte_count;
21502150
}
21512151

src/butil/iobuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ class IOBufAsZeroCopyOutputStream
593593

594594
bool Next(void** data, int* size) override;
595595
void BackUp(int count) override; // `count' can be as long as ByteCount()
596-
google::protobuf::int64 ByteCount() const override;
596+
int64_t ByteCount() const override;
597597

598598
private:
599599
void _release_block();

src/json2pb/json_to_pb.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
perr->append(", ", 2); \
5353
} \
5454
butil::string_appendf(perr, fmt, ##__VA_ARGS__); \
55-
if ((pb) != nullptr) { \
56-
butil::string_appendf(perr, " [%s]", (pb)->GetDescriptor()->name().c_str()); \
55+
if ((pb) != nullptr) { \
56+
perr->append("["); \
57+
perr->append((pb)->GetDescriptor()->name()); \
58+
perr->append("]"); \
5759
} \
5860
} else { }
5961

@@ -572,7 +574,7 @@ bool JsonValueToProtoMessage(const BUTIL_RAPIDJSON_NAMESPACE::Value& json_value,
572574
for (size_t i = 0; i < fields.size(); ++i) {
573575
const google::protobuf::FieldDescriptor* field = fields[i];
574576

575-
const std::string& orig_name = field->name();
577+
const std::string orig_name = field->name();
576578
bool res = decode_name(orig_name, field_name_str_temp);
577579
const std::string& field_name_str = (res ? field_name_str_temp : orig_name);
578580

@@ -719,6 +721,14 @@ bool ProtoJsonToProtoMessage(google::protobuf::io::ZeroCopyInputStream* json,
719721
google::protobuf::Message* message,
720722
const ProtoJson2PbOptions& options,
721723
std::string* error) {
724+
#if GOOGLE_PROTOBUF_VERSION >= 6031000
725+
auto st = google::protobuf::json::JsonStreamToMessage(json, message, options);
726+
bool ok = st.ok();
727+
if (!ok && NULL != error) {
728+
*error = st.ToString();
729+
}
730+
return st.ok();
731+
#else
722732
TypeResolverUniqueptr type_resolver = GetTypeResolver(*message);
723733
std::string type_url = GetTypeUrl(*message);
724734
butil::IOBuf buf;
@@ -739,6 +749,7 @@ bool ProtoJsonToProtoMessage(google::protobuf::io::ZeroCopyInputStream* json,
739749
*error = "Fail to ParseFromCodedStream";
740750
}
741751
return ok;
752+
#endif // GOOGLE_PROTOBUF_VERSION >= 6031000
742753
}
743754

744755
bool ProtoJsonToProtoMessage(const std::string& json, google::protobuf::Message* message,

src/json2pb/pb_to_json.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ bool ProtoMessageToJson(const google::protobuf::Message& message,
352352
bool ProtoMessageToProtoJson(const google::protobuf::Message& message,
353353
google::protobuf::io::ZeroCopyOutputStream* json,
354354
const Pb2ProtoJsonOptions& options, std::string* error) {
355+
#if GOOGLE_PROTOBUF_VERSION >= 6031000
356+
auto st = google::protobuf::util::MessageToJson(message, json, options);
357+
bool ok = st.ok();
358+
if (!ok && NULL != error) {
359+
*error = st.ToString();
360+
}
361+
return st.ok();
362+
#else
355363
butil::IOBuf buf;
356364
butil::IOBufAsZeroCopyOutputStream output_stream(&buf);
357365
if (!message.SerializeToZeroCopyStream(&output_stream)) {
@@ -368,6 +376,7 @@ bool ProtoMessageToProtoJson(const google::protobuf::Message& message,
368376
*error = st.ToString();
369377
}
370378
return ok;
379+
#endif // GOOGLE_PROTOBUF_VERSION >= 6031000
371380
}
372381

373382
bool ProtoMessageToProtoJson(const google::protobuf::Message& message, std::string* json,

src/json2pb/protobuf_map.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ bool IsProtobufMap(const FieldDescriptor* field) {
3838
if (NULL == key_desc
3939
|| key_desc->is_repeated()
4040
|| key_desc->cpp_type() != FieldDescriptor::CPPTYPE_STRING
41-
|| strcmp(KEY_NAME, key_desc->name().c_str()) != 0) {
41+
|| key_desc->name() != KEY_NAME) {
4242
return false;
4343
}
4444
const FieldDescriptor* value_desc = entry_desc->field(VALUE_INDEX);
4545
if (NULL == value_desc
46-
|| strcmp(VALUE_NAME, value_desc->name().c_str()) != 0) {
46+
|| value_desc->name() != VALUE_NAME) {
4747
return false;
4848
}
4949
return true;

src/json2pb/protobuf_type_resolver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ namespace json2pb {
3131
#define PROTOBUF_TYPE_URL_PREFIX "type.googleapis.com"
3232

3333
inline std::string GetTypeUrl(const google::protobuf::Message& message) {
34-
return butil::string_printf(PROTOBUF_TYPE_URL_PREFIX"/%s",
35-
message.GetDescriptor()->full_name().c_str());
34+
std::string type_url = PROTOBUF_TYPE_URL_PREFIX;
35+
type_url.append(message.GetDescriptor()->full_name());
36+
return type_url;
3637
}
3738

3839
// unique_ptr deleter for TypeResolver only deletes the object

0 commit comments

Comments
 (0)