Skip to content

Commit 9501879

Browse files
authored
Merge pull request apple#634 from tbkka/swift-prefix-docs
Document message naming, including swift_prefix
2 parents e44477c + f47e4e8 commit 9501879

File tree

1 file changed

+93
-18
lines changed

1 file changed

+93
-18
lines changed

Documentation/API.md

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,46 @@ public struct Example: SwiftProtobuf.Message {
9393
func ==(lhs: Example, rhs: Example) -> Bool
9494
```
9595

96-
The name of the generated struct is usually copied from the name of
97-
the message in the proto file.
98-
If that name would cause a problem with the generated Swift code,
99-
the word `Message` will be appended to the name.
96+
### Generated struct name
97+
98+
The name of generated struct is based on the name of the
99+
message in the proto file.
100+
101+
For top-level messages, the name is prefixed with the proto package
102+
name as specified in any `package` statements. The name is converted
103+
to camel case with underscore separators to preserve the structure.
104+
105+
For example,
106+
```protobuf
107+
syntax = "proto3";
108+
package my_company.cool_project;
109+
message FooBar {
110+
...
111+
message Baz {
112+
...
113+
}
114+
}
115+
```
116+
will by default generate a struct named `MyCompany_CoolProject_FooBar`
117+
with another `Baz` struct nested inside it.
118+
Note that `Baz` is not prefixed.
119+
120+
You can change the prefix with the `option swift_prefix` statement
121+
in your proto file:
122+
```protobuf
123+
syntax = "proto3";
124+
package my_company.cool_project;
125+
option swift_prefix="My";
126+
message FooBar {
127+
...
128+
}
129+
```
130+
will generate a struct named `MyFooBar`.
131+
(Note: `swift_prefix` is only supported by protoc 3.2 or later.)
132+
133+
If the resulting name would collide with a Swift reserved word
134+
or would otherwise cause problems in the generated code,
135+
then the word `Message` is appended to the name.
100136
For example, a `message Int` in the proto file will cause the
101137
generator to emit a `struct IntMessage` to the generated Swift file.
102138

@@ -125,19 +161,7 @@ want to override the default generated behavior for any reason:
125161
Proto enums are translated to Swift enums in a fairly straightforward manner.
126162
The resulting Swift enums conform to the `SwiftProtobuf.Enum` protocol which extends
127163
`RawRepresentable` with a `RawValue` of `Int`.
128-
The name of the Swift enum is copied directly from the name in the proto file.
129-
If that name would cause problems for the generated code, the word `Enum` will
130-
be appended to the name.
131-
132-
Proto2 enums simply have a case for each enum value.
133-
Enum case names are converted from `UPPER_SNAKE_CASE` conventions
134-
in the proto file to `lowerCamelCase` in the Swift code.
135-
136-
Note: In most cases where the resulting name would cause a problem in
137-
the generated Swift, the code generator will protect the enum case name by
138-
surrounding it with backticks.
139-
Sometimes, this is insufficient and the code generator will append
140-
an underscore `_` to the converted name.
164+
The generated Swift enum will have a case for each enum value in the proto file.
141165

142166
If deserialization encounters an unknown value, that value is either
143167
ignored (JSON) or handled as an unknown field (protobuf binary),
@@ -175,6 +199,57 @@ public enum MyEnum: SwiftProtobuf.Enum {
175199
}
176200
```
177201

202+
### Enum and enum case naming
203+
204+
The name of the Swift enum is copied directly from the name in the proto file,
205+
prefixed with the package name or the name from `option swift_prefix`
206+
as documented above for messages.
207+
If that name would conflict with a Swift reserved word or otherwise
208+
cause problems for the generated code, the word `Enum` will
209+
be appended to the name.
210+
211+
Enum case names are converted from `UPPER_SNAKE_CASE` conventions
212+
in the proto file to `lowerCamelCase` in the Swift code.
213+
214+
If the enum case name includes the enum name as a prefix (ignoring case
215+
and underscore characters), that prefix is stripped.
216+
If the stripped name would conflict with another entry in the
217+
same enum, the conflicting cases will have their respective numeric values
218+
appended to ensure the results are unique.
219+
For example:
220+
```protobuf
221+
syntax = "proto3";
222+
enum TestEnum {
223+
TEST_ENUM_FOO = 0;
224+
TESTENUM_BAR = 1;
225+
BAZ = 2;
226+
BAR = -3;
227+
}
228+
```
229+
becomes
230+
```swift
231+
enum TestEnum {
232+
case foo = 0
233+
case bar_1 = 1
234+
case baz = 2
235+
case bar_n3 = -3 // 'n' for "negative"
236+
}
237+
```
238+
239+
Note #1: Enum aliases can potentially result in conflicting names
240+
even after appending the case numeric value.
241+
Since aliases are only supported to provide alternate names for
242+
the same underlying numeric value, SwiftProtobuf simply drops
243+
the alias in such cases.
244+
See the protobuf documentation for `allow_alias` for more information
245+
about enum case aliases.
246+
247+
Note #2: In most cases where an enum case name might conflict with a
248+
Swift reserved word, or otherwise cause problems, the code generator
249+
will protect the enum case name by surrounding it with backticks.
250+
In the few cases where this is insufficient, the code generator
251+
will append an additional underscore `_` to the converted name.
252+
178253
## Message Fields
179254

180255
Each message field in the `proto` file is compiled into a corresponding
@@ -486,7 +561,7 @@ with non-protobuf JSON implementations, but means that you
486561
cannot translate between binary and JSON encodings without
487562
having the type information available.
488563
If you need to translate objects between binary and JSON
489-
encodings, you should carefully read to documentation
564+
encodings, you should carefully read the documentation
490565
comments for `Google_Protobuf_Any.register()` which explains
491566
how to make your custom types available to the decode/encode machinery
492567
for this purpose.

0 commit comments

Comments
 (0)