Skip to content

Commit fc34437

Browse files
committed
Merge remote-tracking branch 'origin/oneof_support'
2 parents 5c621bb + 66888c7 commit fc34437

6 files changed

+129
-3
lines changed

.travis.before_install.sh

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,33 @@
66
# and/or modify it under the terms of the MIT license.
77
# See LICENSE for details.
88

9-
echo $TRAVIS_OS_NAME
9+
echo TRAVIS_OS_NAME=$TRAVIS_OS_NAME
1010

11+
PROTOBUF_VERSION=2.6.1
12+
echo PROTOBUF_VERSION=$PROTOBUF_VERSION
13+
14+
PROTOBUF_C_VERSION=1.2.1
15+
echo PROTOBUF_C_VERSION=$PROTOBUF_C_VERSION
16+
17+
echo "Installing protobuf..."
18+
wget https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-$PROTOBUF_VERSION.tar.gz
19+
tar xf protobuf-$PROTOBUF_VERSION.tar.gz
20+
cd protobuf-$PROTOBUF_VERSION && ./configure && make -j2 && sudo make install && sudo ldconfig
21+
22+
echo "Installing protobuf-c..."
23+
wget https://github.com/protobuf-c/protobuf-c/releases/download/v$PROTOBUF_C_VERSION/protobuf-c-$PROTOBUF_C_VERSION.tar.gz
24+
tar xf protobuf-c-$PROTOBUF_C_VERSION.tar.gz
25+
cd protobuf-c-$PROTOBUF_C_VERSION && ./configure && make -j2 && sudo make install && sudo ldconfig
26+
27+
echo "Installing jansson and some tools..."
1128
if [ "x$TRAVIS_OS_NAME" = "xosx" ]; then
1229
brew update
13-
brew install protobuf-c
1430
brew install jansson
1531
brew install cppcheck
1632
if [ "x$MY_VALGRIND" = "x1" ]; then brew install valgrind; fi
1733
else # linux
1834
sudo add-apt-repository --yes ppa:pi-rho/security
1935
sudo apt-get update
20-
sudo apt-get install libprotobuf-c0-dev libprotobuf-c0 protobuf-c-compiler
2136
sudo apt-get install libjansson4-dev libjansson4
2237
sudo apt-get install cppcheck
2338
if [ "x$MY_VALGRIND" = "x1" ]; then sudo apt-get install valgrind; fi

src/protobuf2json.c

+12
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ static int protobuf2json_process_message(
198198
);
199199
}
200200
} else if (field_descriptor->label == PROTOBUF_C_LABEL_OPTIONAL) {
201+
if (field_descriptor->flags & PROTOBUF_C_FIELD_FLAG_ONEOF) {
202+
if (*(uint32_t *)protobuf_value_quantifier == field_descriptor->id) {
203+
if (field_descriptor->type == PROTOBUF_C_TYPE_MESSAGE || field_descriptor->type == PROTOBUF_C_TYPE_STRING) {
204+
if (protobuf_value == NULL || protobuf_value == field_descriptor->default_value) {
205+
continue;
206+
}
207+
}
208+
} else {
209+
continue;
210+
}
211+
}
212+
201213
protobuf_c_boolean is_set = 0;
202214

203215
if (field_descriptor->type == PROTOBUF_C_TYPE_MESSAGE || field_descriptor->type == PROTOBUF_C_TYPE_STRING) {

test/run-tmp.c

+34
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@
1414

1515
char executable_path[MAXPATHLEN] = {0};
1616

17+
void oneof(void) {
18+
int result;
19+
20+
Foo__Something something = FOO__SOMETHING__INIT;
21+
22+
char *json_string;
23+
24+
// FOO__SOMETHING__SOMETHING__NOT_SET
25+
something.something_case = FOO__SOMETHING__SOMETHING__NOT_SET;
26+
result = protobuf2json_string(&something.base, TEST_JSON_FLAGS, &json_string, NULL, 0);
27+
28+
printf("Debug: %s\n", json_string);
29+
free(json_string);
30+
31+
// FOO__SOMETHING__SOMETHING_ONEOF_STRING
32+
something.oneof_string = "string";
33+
something.something_case = FOO__SOMETHING__SOMETHING_ONEOF_STRING;
34+
result = protobuf2json_string(&something.base, TEST_JSON_FLAGS, &json_string, NULL, 0);
35+
36+
printf("Debug: %s\n", json_string);
37+
free(json_string);
38+
39+
// FOO__SOMETHING__SOMETHING_ONEOF_BYTES
40+
something.oneof_bytes.len = 5;
41+
something.oneof_bytes.data = (uint8_t*)"bytes";
42+
something.something_case = FOO__SOMETHING__SOMETHING_ONEOF_BYTES;
43+
result = protobuf2json_string(&something.base, TEST_JSON_FLAGS, &json_string, NULL, 0);
44+
45+
printf("Debug: %s\n", json_string);
46+
free(json_string);
47+
}
48+
1749
void person__debug(void) {
1850
int result;
1951

@@ -328,6 +360,8 @@ void read_file_success(void) {
328360
int main(int argc, char **argv) {
329361
strncpy(executable_path, argv[0], sizeof(executable_path) - 1);
330362

363+
oneof();
364+
331365
person__debug();
332366

333367
person__error_unknown_enum_value();

test/test-list.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TEST_DECLARE(protobuf2json_file__error_cannot_dump_file)
1616
TEST_DECLARE(protobuf2json_string__required_field)
1717
TEST_DECLARE(protobuf2json_string__optional_field)
1818
TEST_DECLARE(protobuf2json_string__default_values)
19+
TEST_DECLARE(protobuf2json_string__oneof)
1920
TEST_DECLARE(protobuf2json_string__error_in_nested_message)
2021
TEST_DECLARE(protobuf2json_string__error_cannot_create_json_object)
2122
TEST_DECLARE(protobuf2json_string__error_cannot_create_json_value)
@@ -71,6 +72,7 @@ TASK_LIST_START
7172
TEST_ENTRY(protobuf2json_string__required_field)
7273
TEST_ENTRY(protobuf2json_string__optional_field)
7374
TEST_ENTRY(protobuf2json_string__default_values)
75+
TEST_ENTRY(protobuf2json_string__oneof)
7476
TEST_ENTRY(protobuf2json_string__error_in_nested_message)
7577
TEST_ENTRY(protobuf2json_string__error_cannot_create_json_object)
7678
TEST_ENTRY(protobuf2json_string__error_cannot_create_json_value)

test/test-protobuf2json-string.c

+56
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,62 @@ TEST_IMPL(protobuf2json_string__default_values) {
9595
RETURN_OK();
9696
}
9797

98+
TEST_IMPL(protobuf2json_string__oneof) {
99+
int result;
100+
101+
Foo__Something something = FOO__SOMETHING__INIT;
102+
103+
char *json_string;
104+
105+
// FOO__SOMETHING__SOMETHING__NOT_SET
106+
something.something_case = FOO__SOMETHING__SOMETHING__NOT_SET;
107+
result = protobuf2json_string(&something.base, TEST_JSON_FLAGS, &json_string, NULL, 0);
108+
ASSERT_ZERO(result);
109+
ASSERT(json_string);
110+
111+
ASSERT_STRCMP(
112+
json_string,
113+
"{}"
114+
);
115+
116+
free(json_string);
117+
118+
// FOO__SOMETHING__SOMETHING_ONEOF_STRING
119+
something.something_case = FOO__SOMETHING__SOMETHING_ONEOF_STRING;
120+
something.oneof_string = "string";
121+
result = protobuf2json_string(&something.base, TEST_JSON_FLAGS, &json_string, NULL, 0);
122+
ASSERT_ZERO(result);
123+
ASSERT(json_string);
124+
125+
ASSERT_STRCMP(
126+
json_string,
127+
"{\n"
128+
" \"oneof_string\": \"string\"\n"
129+
"}"
130+
);
131+
132+
free(json_string);
133+
134+
// FOO__SOMETHING__SOMETHING_ONEOF_BYTES
135+
something.oneof_bytes.len = 5;
136+
something.oneof_bytes.data = (uint8_t*)"bytes";
137+
something.something_case = FOO__SOMETHING__SOMETHING_ONEOF_BYTES;
138+
result = protobuf2json_string(&something.base, TEST_JSON_FLAGS, &json_string, NULL, 0);
139+
ASSERT_ZERO(result);
140+
ASSERT(json_string);
141+
142+
ASSERT_STRCMP(
143+
json_string,
144+
"{\n"
145+
" \"oneof_bytes\": \"bytes\"\n"
146+
"}"
147+
);
148+
149+
free(json_string);
150+
151+
RETURN_OK();
152+
}
153+
98154
TEST_IMPL(protobuf2json_string__error_in_nested_message) {
99155
int result;
100156
char error_string[256] = {0};

test/test.proto

+7
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ message RepeatedValues {
6060

6161
repeated Person value_message = 17;
6262
}
63+
64+
message Something {
65+
oneof something {
66+
string oneof_string = 11;
67+
bytes oneof_bytes = 22;
68+
}
69+
}

0 commit comments

Comments
 (0)