Skip to content

Commit 126c8c9

Browse files
committed
Teach Descriptors about proto3 optional.
- Add methods to FieldDescritor to check attributes of the field (modeling the C++ changes). - Add a method to OneofDescriptor to check if it is synthetic (i.e. made for proto3 optional support). - Add methods to Descriptor to get the non synthetic oneofs. - Add a new test file to unittest the apis. - Add --experimental_allow_proto3_optional to protoc invoke in the Makefile.
1 parent f27d535 commit 126c8c9

File tree

8 files changed

+1275
-57
lines changed

8 files changed

+1275
-57
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ GOOGLE_PROTOBUF_CHECKOUT?=../protobuf
5353
# previously installed one), we use a custom output name (-tfiws_out).
5454
PROTOC_GEN_SWIFT=.build/debug/protoc-gen-swift
5555
GENERATE_SRCS_BASE=${PROTOC} --plugin=protoc-gen-tfiws=${PROTOC_GEN_SWIFT}
56-
GENERATE_SRCS=${GENERATE_SRCS_BASE} -I Protos
56+
# Until the flag isn't needed, add the flag to enable proto3 optional.
57+
GENERATE_SRCS=${GENERATE_SRCS_BASE} -I Protos --experimental_allow_proto3_optional
5758

5859
# Where to find the Swift conformance test runner executable.
5960
SWIFT_CONFORMANCE_PLUGIN=.build/debug/Conformance
@@ -167,6 +168,7 @@ CONFORMANCE_PROTOS= \
167168

168169
SWIFT_DESCRIPTOR_TEST_PROTOS= \
169170
Protos/pluginlib_descriptor_test.proto \
171+
Protos/pluginlib_descriptor_test2.proto \
170172
${PLUGIN_PROTOS}
171173

172174
XCODEBUILD_EXTRAS =
@@ -388,7 +390,9 @@ regenerate-test-protos: build ${PROTOC_GEN_SWIFT} Protos/generated_swift_names_e
388390
${TEST_PROTOS}
389391

390392
Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${SWIFT_DESCRIPTOR_TEST_PROTOS}
393+
# Until the flag isn't needed, add the flag to enable proto3 optional.
391394
@${PROTOC} \
395+
--experimental_allow_proto3_optional \
392396
--include_imports \
393397
--descriptor_set_out=DescriptorTestData.bin \
394398
-I Protos \

Protos/pluginlib_descriptor_test.proto

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,29 @@ service SomeService {
8080
rpc Foo(google.protobuf.DescriptorProto) returns (google.protobuf.compiler.Version);
8181
rpc Bar(TopLevelMessage) returns (TopLevelMessage2);
8282
}
83+
84+
message Proto2MessageForPresence {
85+
86+
required string req_str_field = 1;
87+
required int32 req_int32_field = 2;
88+
required TopLevelEnum req_enum_field = 3;
89+
required TopLevelMessage req_message_field = 4;
90+
91+
optional string opt_str_field = 11;
92+
optional int32 opt_int32_field = 12;
93+
optional TopLevelEnum opt_enum_field = 13;
94+
optional TopLevelMessage opt_message_field = 14;
95+
96+
repeated string repeat_str_field = 21;
97+
repeated int32 repeat_int32_field = 22;
98+
repeated TopLevelEnum repeat_enum_field = 23;
99+
repeated TopLevelMessage repeat_message_field = 24;
100+
101+
oneof o {
102+
string oneof_str_field = 31;
103+
int32 oneof_int32_field = 32;
104+
TopLevelEnum oneof_enum_field = 33;
105+
TopLevelMessage oneof_message_field = 34;
106+
}
107+
108+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Protos/pluginlib_descriptor_test.proto - test proto
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
// -----------------------------------------------------------------------------
12+
///
13+
/// Test proto for Tests/SwiftProtobufPluginLibraryTests/Test_Descriptor.swift
14+
///
15+
// -----------------------------------------------------------------------------
16+
17+
syntax = "proto3";
18+
19+
package swift_descriptor_test;
20+
21+
message Proto3MessageForPresence {
22+
23+
enum SubEnum {
24+
SUB_VALUE_0 = 0;
25+
SUB_VALUE_1 = 1;
26+
SUB_VALUE_2 = 2;
27+
}
28+
29+
string str_field = 1;
30+
int32 int32_field = 2;
31+
SubEnum enum_field = 3;
32+
OtherMessage message_field = 4;
33+
34+
optional string opt_str_field = 11;
35+
optional int32 opt_int32_field = 12;
36+
optional SubEnum opt_enum_field = 13;
37+
optional OtherMessage opt_message_field = 14;
38+
39+
repeated string repeat_str_field = 21;
40+
repeated int32 repeat_int32_field = 22;
41+
repeated SubEnum repeat_enum_field = 23;
42+
repeated OtherMessage repeat_message_field = 24;
43+
44+
oneof o {
45+
string oneof_str_field = 31;
46+
int32 oneof_int32_field = 32;
47+
SubEnum oneof_enum_field = 33;
48+
OtherMessage oneof_message_field = 34;
49+
}
50+
51+
}
52+
53+
message OtherMessage {
54+
string field = 1;
55+
}

0 commit comments

Comments
 (0)