@@ -1138,74 +1138,67 @@ There are two main reasons to use extensions:
11381138
11391139### Example Extension {#ext-example}
11401140
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.
11421144
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.
11501147
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.**
11571149
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.
11621155
11631156``` proto
1164- // file media/user_content.proto
1157+ // media/user_content.proto
1158+ edition = "2023";
11651159
11661160package media;
11671161
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.
11871163message UserContent {
11881164 extensions 100 to 199 [
11891165 declaration = {
11901166 number: 126,
11911167 full_name: ".kittens.kitten_videos",
11921168 type: ".kittens.Video",
11931169 repeated: true
1194- },
1195- // Ensures all field numbers in this extension range are declarations.
1196- verification = DECLARATION
1170+ }
11971171 ];
11981172}
11991173```
12001174
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+ ```
12061198
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.
12091202
12101203There is no difference in the wire-format encoding of extension fields as
12111204compared to a standard field with the same field number, type, and cardinality.
0 commit comments