@@ -29,35 +29,57 @@ act like proto2 or proto3 files. For more information on how Editions and
29
29
Features work together to set behavior, see
30
30
[ Protobuf Editions Overview] ( /editions/overview ) .
31
31
32
- Each of the following sections has an entry for what scope the feature applies
33
- to. This can include file, enum, message, or field. The following sample shows a
34
- mock feature applied to each scope:
32
+ <span id =" cascading " > Feature settings apply at different levels:</span >
33
+
34
+ ** File-level:** These settings apply to all elements (messages, fields, enums,
35
+ and so on) that don't have an overriding setting.
36
+
37
+ ** Non-nested:** Messages, enums, and services can override settings made at the
38
+ file level. They apply to everything within them (message fields, enum values)
39
+ that aren't overridden, but don't apply to other parallel messages and enums.
40
+
41
+ ** Nested:** Oneofs, messages, and enums can override settings from the message
42
+ they're nested in.
43
+
44
+ ** Lowest-level:** Fields, extensions, enum values, extension ranges, and methods
45
+ are the lowest level at which you can override settings.
46
+
47
+ Each of the following sections has a comment that states what scope the feature
48
+ can be applied to. The following sample shows a mock feature applied to each
49
+ scope:
35
50
36
51
``` proto
37
52
edition = "2023";
38
53
39
- // File-scope definition
54
+ // File-level scope definition
40
55
option features.bar = BAZ;
41
56
42
57
enum Foo {
43
- // Enum- scope definition
58
+ // Enum (non-nested scope) definition
44
59
option features.bar = QUX;
45
60
46
61
A = 1;
47
62
B = 2;
48
63
}
49
64
50
65
message Corge {
51
- // Message- scope definition
66
+ // Message (non-nested scope) definition
52
67
option features.bar = QUUX;
53
68
54
- // Field-scope definition
69
+ message Garply {
70
+ // Message (nested scope) definition
71
+ option features.bar = WALDO;
72
+ string id = 1;
73
+ }
74
+
75
+ // Field (lowest-level scope) definition
55
76
Foo A = 1 [features.bar = GRAULT];
56
77
}
57
78
```
58
79
59
- In this example, the setting ` GRAULT ` in the field-scope feature definition
60
- overrides the message-scope QUUX setting.
80
+ In this example, the setting "` GRAULT" ` in the lowest-level scope feature
81
+ definition overrides the non-nested-scope "` QUUX ` " setting. And within the
82
+ Garply message, "` WALDO ` " overrides "` QUUX ` ."
61
83
62
84
### ` features.enum_type ` {#enum_type}
63
85
@@ -83,6 +105,9 @@ and after of a proto3 file.
83
105
84
106
** Behavior in proto3:** ` OPEN `
85
107
108
+ ** Note:** Feature settings on different schema elements
109
+ [ have different scopes] ( #cascading ) .
110
+
86
111
The following code sample shows a proto2 file:
87
112
88
113
``` proto
@@ -102,6 +127,7 @@ this:
102
127
edition = "2023";
103
128
104
129
enum Foo {
130
+ // Setting the enum_type feature overrides the default OPEN enum
105
131
option features.enum_type = CLOSED;
106
132
A = 2;
107
133
B = 4;
@@ -129,7 +155,7 @@ whether a protobuf field has a value.
129
155
130
156
** Applicable to the following scopes:** File, Field
131
157
132
- ** Default value in the Edition 2023:** ` EXPLICIT `
158
+ ** Default behavior in the Edition 2023:** ` EXPLICIT `
133
159
134
160
** Behavior in proto2:** ` EXPLICIT `
135
161
@@ -138,6 +164,9 @@ which case it behaves like `EXPLICIT`. See
138
164
[ Presence in Proto3 APIs] ( /programming-guides/field_presence#presence-in-proto3-apis )
139
165
for more information.
140
166
167
+ ** Note:** Feature settings on different schema elements
168
+ [ have different scopes] ( #cascading ) .
169
+
141
170
The following code sample shows a proto2 file:
142
171
143
172
``` proto
@@ -156,6 +185,7 @@ After running Prototiller, the equivalent code might look like this:
156
185
edition = "2023";
157
186
158
187
message Foo {
188
+ // Setting the field_presence feature retains the proto2 required behavior
159
189
int32 x = 1 [features.field_presence = LEGACY_REQUIRED];
160
190
int32 y = 2;
161
191
repeated int32 z = 3;
@@ -178,10 +208,13 @@ After running Prototiller, the equivalent code might look like this:
178
208
179
209
``` proto
180
210
edition = "2023";
211
+ // Setting the file-level field_presence feature matches the proto3 implicit default
181
212
option features.field_presence = IMPLICIT;
182
213
183
214
message Bar {
184
215
int32 x = 1;
216
+ // Setting the field_presence here retains the explicit state that the proto3
217
+ // field has because of the optional syntax
185
218
int32 y = 2 [features.field_presence = EXPLICIT];
186
219
repeated int32 z = 3;
187
220
}
@@ -214,6 +247,9 @@ and after of a proto3 file. Editions behavior matches the behavior in proto3.
214
247
215
248
** Behavior in proto3:** ` ALLOW `
216
249
250
+ ** Note:** Feature settings on different schema elements
251
+ [ have different scopes] ( #cascading ) .
252
+
217
253
The following code sample shows a proto2 file:
218
254
219
255
``` proto
@@ -230,7 +266,7 @@ After running Prototiller, the equivalent code might look like this:
230
266
231
267
``` proto
232
268
edition = "2023";
233
- features.json_format = LEGACY_BEST_EFFORT;
269
+ option features.json_format = LEGACY_BEST_EFFORT;
234
270
235
271
message Foo {
236
272
string bar = 1;
@@ -270,6 +306,9 @@ the following conditions are met:
270
306
271
307
** Behavior in proto3:** ` LENGTH_PREFIXED ` . Proto3 doesn't support ` DELIMITED ` .
272
308
309
+ ** Note:** Feature settings on different schema elements
310
+ [ have different scopes] ( #cascading ) .
311
+
273
312
The following code sample shows a proto2 file:
274
313
275
314
``` proto
@@ -318,6 +357,9 @@ for `repeated` fields has been migrated to in Editions.
318
357
319
358
** Behavior in proto3:** ` PACKED `
320
359
360
+ ** Note:** Feature settings on different schema elements
361
+ [ have different scopes] ( #cascading ) .
362
+
321
363
The following code sample shows a proto2 file:
322
364
323
365
``` proto
@@ -389,6 +431,9 @@ and after of a proto3 file.
389
431
390
432
** Behavior in proto3:** ` VERIFY `
391
433
434
+ ** Note:** Feature settings on different schema elements
435
+ [ have different scopes] ( #cascading ) .
436
+
392
437
The following code sample shows a proto2 file:
393
438
394
439
``` proto
@@ -441,6 +486,9 @@ before and after of a proto3 file.
441
486
442
487
** Behavior in proto3:** ` false `
443
488
489
+ ** Note:** Feature settings on different schema elements
490
+ [ have different scopes] ( #cascading ) .
491
+
444
492
The following code sample shows a proto2 file:
445
493
446
494
``` proto
@@ -495,6 +543,9 @@ specified on a field, but not both.
495
543
496
544
** Behavior in proto3:** ` STRING `
497
545
546
+ ** Note:** Feature settings on different schema elements
547
+ [ have different scopes] ( #cascading ) .
548
+
498
549
The following code sample shows a proto2 file:
499
550
500
551
``` proto
@@ -568,6 +619,9 @@ before and after of a proto3 file.
568
619
569
620
** Behavior in proto3:** ` DEFAULT `
570
621
622
+ ** Note:** Feature settings on different schema elements
623
+ [ have different scopes] ( #cascading ) .
624
+
571
625
The following code sample shows a proto2 file:
572
626
573
627
``` proto
@@ -592,6 +646,7 @@ option features.utf8_validation = NONE;
592
646
option features.(pb.java).utf8_validation = VERIFY;
593
647
message MyMessage {
594
648
string foo = 1;
649
+ string bar = 2;
595
650
}
596
651
```
597
652
@@ -618,6 +673,9 @@ files.
618
673
619
674
** Behavior in proto3:** ` false `
620
675
676
+ ** Note:** Feature settings on different schema elements
677
+ [ have different scopes] ( #cascading ) .
678
+
621
679
## Preserving proto2 or proto3 Behavior {#preserving}
622
680
623
681
You may want to move to the editions format but not deal with updates to the way
0 commit comments