Skip to content

Commit 94155e5

Browse files
committed
Document message naming, including swift_prefix
1 parent 3a467cf commit 94155e5

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

Documentation/API.md

Lines changed: 44 additions & 5 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 VeryImportant {
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,7 +161,10 @@ 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.
164+
165+
The name of the Swift enum is copied directly from the name in the proto file,
166+
prefixed with the package name or the name from `option swift_prefix`
167+
as documented above for messages.
129168
If that name would cause problems for the generated code, the word `Enum` will
130169
be appended to the name.
131170

0 commit comments

Comments
 (0)