@@ -1138,74 +1138,67 @@ There are two main reasons to use extensions:
1138
1138
1139
1139
### Example Extension {#ext-example}
1140
1140
1141
- Let's look at an example extension:
1141
+ Using an extension is a two-step process. First, in the message you want to
1142
+ extend (the "container"), you must reserve a range of field numbers for
1143
+ extensions. Then, in a separate file, you define the extension field itself.
1142
1144
1143
- ``` proto
1144
- // file kittens/video_ext.proto
1145
-
1146
- import "kittens/video.proto";
1147
- import "media/user_content.proto";
1148
-
1149
- package kittens;
1145
+ Here is an example that shows how to add an extension for kitten videos to a
1146
+ generic ` UserContent ` message.
1150
1147
1151
- // This extension allows kitten videos in a media.UserContent message.
1152
- extend media.UserContent {
1153
- // Video is a message imported from kittens/video.proto
1154
- repeated Video kitten_videos = 126;
1155
- }
1156
- ```
1148
+ ** Step 1: Reserve an extension range in the container message.**
1157
1149
1158
- Note that the file defining the extension (` kittens/video_ext.proto ` ) imports
1159
- the container message's file (` media/user_content.proto ` ).
1160
-
1161
- The container message must reserve a subset of its field numbers for extensions.
1150
+ The container message must use the ` extensions ` keyword to reserve a range of
1151
+ field numbers for others to use. It is a best practice to also add a
1152
+ ` declaration ` for the specific extension you plan to add. This declaration acts
1153
+ as a forward-declaration, making it easier for developers to discover extensions
1154
+ and avoid reusing field numbers.
1162
1155
1163
1156
``` proto
1164
- // file media/user_content.proto
1157
+ // media/user_content.proto
1158
+ edition = "2023";
1165
1159
1166
1160
package media;
1167
1161
1168
- // A container message to hold stuff that a user has created.
1169
- message UserContent {
1170
- // Set verification to `DECLARATION` to enforce extension declarations for all
1171
- // extensions in this range.
1172
- extensions 100 to 199 [verification = DECLARATION];
1173
- }
1174
- ```
1175
-
1176
- The container message's file (` media/user_content.proto ` ) defines the message
1177
- ` UserContent ` , which reserves field numbers [ 100 to 199] for extensions. It is
1178
- recommended to set ` verification = DECLARATION ` for the range to require
1179
- declarations for all its extensions.
1180
-
1181
- When the new extension (` kittens/video_ext.proto ` ) is added, a corresponding
1182
- declaration should be added to ` UserContent ` and ` verification ` should be
1183
- removed.
1184
-
1185
- ```
1186
- // A container message to hold stuff that a user has created.
1162
+ // A container for user-created content.
1187
1163
message UserContent {
1188
1164
extensions 100 to 199 [
1189
1165
declaration = {
1190
1166
number: 126,
1191
1167
full_name: ".kittens.kitten_videos",
1192
1168
type: ".kittens.Video",
1193
1169
repeated: true
1194
- },
1195
- // Ensures all field numbers in this extension range are declarations.
1196
- verification = DECLARATION
1170
+ }
1197
1171
];
1198
1172
}
1199
1173
```
1200
1174
1201
- ` UserContent ` declares that field number ` 126 ` will be used by a ` repeated `
1202
- extension field with the fully-qualified name ` .kittens.kitten_videos ` and the
1203
- fully-qualified type ` .kittens.Video ` . To learn more about extension
1204
- declarations see
1205
- [ Extension Declarations] ( /programming-guides/extension_declarations ) .
1175
+ This declaration specifies the field number, full name, type, and cardinality of
1176
+ the extension that will be defined elsewhere.
1177
+
1178
+ ** Step 2: Define the extension in a separate file.**
1179
+
1180
+ The extension itself is defined in a different ` .proto ` file, which typically
1181
+ focuses on a specific feature (like kitten videos). This avoids adding a
1182
+ dependency from the generic container to the specific feature.
1183
+
1184
+ ``` proto
1185
+ // kittens/video_ext.proto
1186
+ edition = "2023";
1187
+
1188
+ import "media/user_content.proto"; // Imports the container message
1189
+ import "kittens/video.proto"; // Imports the extension's message type
1190
+
1191
+ package kittens;
1192
+
1193
+ // This defines the extension field.
1194
+ extend media.UserContent {
1195
+ repeated Video kitten_videos = 126;
1196
+ }
1197
+ ```
1206
1198
1207
- Note that the container message's file (` media/user_content.proto ` ) ** does not**
1208
- import the kitten_video extension definition (` kittens/video_ext.proto ` )
1199
+ The ` extend ` block ties the new ` kitten_videos ` field back to the
1200
+ ` media.UserContent ` message, using the field number ` 126 ` that was reserved in
1201
+ the container.
1209
1202
1210
1203
There is no difference in the wire-format encoding of extension fields as
1211
1204
compared to a standard field with the same field number, type, and cardinality.
0 commit comments