@@ -6,7 +6,7 @@ description = "Explains the various presence-tracking disciplines for protobuf f
66type = " docs"
77+++
88
9- ## Background
9+ ## Background {#background}
1010
1111* Field presence* is the notion of whether a protobuf field has a value. There
1212are two different manifestations of presence for protobufs: * implicit presence* ,
@@ -18,15 +18,15 @@ recommend always adding the `optional` label for proto3 basic types. This
1818provides a smoother path to editions, which uses explicit presence by
1919default.{{% /alert %}}
2020
21- ### Presence Disciplines
21+ ### Presence Disciplines {#disciplines}
2222
2323* Presence disciplines* define the semantics for translating between the * API
2424representation* and the * serialized representation* . The * implicit presence*
2525discipline relies upon the field value itself to make decisions at
2626(de)serialization time, while the * explicit presence* discipline relies upon the
2727explicit tracking state instead.
2828
29- ### Presence in * Tag-value stream* (Wire Format) Serialization
29+ ### Presence in * Tag-value stream* (Wire Format) Serialization {#presence-tag-value}
3030
3131The wire format is a stream of tagged, * self-delimiting* values. By definition,
3232the wire format represents a sequence of * present* values. In other words, every
@@ -66,7 +66,7 @@ deserializing wire-formatted messages:
6666 APIs. However, out-of-range values may be stored as * unknown fields* in the
6767 API, even though the wire-format tag was recognized.
6868
69- ### Presence in * Named-field Mapping* Formats
69+ ### Presence in * Named-field Mapping* Formats {#presence-named-field}
7070
7171Protobufs can be represented in human-readable, textual forms. Two notable
7272formats are TextFormat (the output format produced by generated message
@@ -104,7 +104,7 @@ practice, however, presence correctness can vary depending upon implementation
104104choices, especially if JSON was chosen as a means to interoperate with clients
105105not using protobufs.
106106
107- ### Presence in Proto2 APIs
107+ ### Presence in Proto2 APIs {#presence-proto2}
108108
109109This table outlines whether presence is tracked for fields in proto2 APIs (both
110110for generated APIs and using dynamic reflection):
@@ -147,7 +147,7 @@ several methods:
147147Repeated fields and maps do not track presence: there is no distinction between
148148an * empty* and a * not-present* repeated field.
149149
150- ### Presence in Proto3 APIs
150+ ### Presence in Proto3 APIs {#presence-proto3}
151151
152152This table outlines whether presence is tracked for fields in proto3 APIs (both
153153for generated APIs and using dynamic reflection):
@@ -187,7 +187,7 @@ required to have an enumerator value which maps to 0. By convention, this is an
187187the domain of valid values for the application, this behavior can be thought of
188188as tantamount to * explicit presence* .
189189
190- ### Presence in Editions APIs
190+ ### Presence in Editions APIs {#presence-editions}
191191
192192This table outlines whether presence is tracked for fields in editions APIs
193193(both for generated APIs and using dynamic reflection):
@@ -239,7 +239,7 @@ For a singular field with numeric, enum, or string type:
239239exception to this behavior is Dart, which generates ` has_ ` methods with proto3
240240proto schema files.{{% /alert %}}
241241
242- ### Considerations for Merging
242+ ### Considerations for Merging {#merging}
243243
244244Under the * implicit presence* rules, it is effectively impossible for a target
245245field to merge-from its default value (using the protobuf's API merging
@@ -256,7 +256,7 @@ Updating to set a default value in this case requires some external mechanism,
256256such as ` FieldMask ` . However, if presence * is* tracked, then all explicitly-set
257257values -- even default values -- will be merged into the target.
258258
259- ### Considerations for change-compatibility
259+ ### Considerations for change-compatibility {#change-compatibility}
260260
261261Changing a field between * explicit presence* and * implicit presence* is a
262262binary-compatible change for serialized values in wire format. However, the
@@ -334,7 +334,7 @@ this is not a safe change: client A requires (by `assert`) that the field is
334334present; even without any modifications through the API, that requirement fails
335335in a value- and peer-dependent case.
336336
337- ## How to Enable * Explicit Presence* in Proto3
337+ ## How to Enable * Explicit Presence* in Proto3 {#enable-explicit-proto3}
338338
339339These are the general steps to use field tracking support for proto3:
340340
@@ -344,7 +344,7 @@ These are the general steps to use field tracking support for proto3:
3443441 . Use the generated "hazzer" methods and "clear" methods in application code,
345345 instead of comparing or setting default values.
346346
347- ### ` .proto ` File Changes
347+ ### ` .proto ` File Changes {#proto-file-changes}
348348
349349This is an example of a proto3 message with fields which follow both * no
350350presence* and * explicit presence* semantics:
@@ -362,7 +362,7 @@ message MyMessage {
362362}
363363```
364364
365- ### ` protoc ` Invocation
365+ ### ` protoc ` Invocation {#protoc-invocation}
366366
367367Presence tracking for proto3 messages is enabled by default
368368[ since v3.15.0] ( https://github.com/protocolbuffers/protobuf/releases/tag/v3.15.0 )
@@ -399,7 +399,7 @@ message Msg {
399399In the examples, a function ` GetProto ` constructs and returns a message of type
400400` Msg ` with unspecified contents.
401401
402- #### C++ Example
402+ #### C++ Example {#cpp-example}
403403
404404Implicit presence:
405405
@@ -427,7 +427,7 @@ if (m.has_foo()) {
427427}
428428```
429429
430- #### C# Example
430+ #### C# Example {#csharp-example}
431431
432432Implicit presence:
433433
@@ -455,7 +455,7 @@ if (m.HasFoo) {
455455}
456456```
457457
458- #### Go Example
458+ #### Go Example {#go-example}
459459
460460Implicit presence:
461461
@@ -483,7 +483,7 @@ if m.Foo != nil {
483483}
484484```
485485
486- #### Java Example
486+ #### Java Example {#java-example}
487487
488488These examples use a ` Builder ` to demonstrate clearing. Simply checking presence
489489and getting values from a ` Builder ` follows the same API as the message type.
@@ -514,7 +514,7 @@ if (m.hasFoo()) {
514514}
515515```
516516
517- #### Python Example
517+ #### Python Example {#python-example}
518518
519519Implicit presence:
520520
@@ -540,7 +540,7 @@ else:
540540 m.foo = 1
541541```
542542
543- #### Ruby Example
543+ #### Ruby Example {#ruby-example}
544544
545545Implicit presence:
546546
568568end
569569```
570570
571- #### Javascript Example
571+ #### Javascript Example {#js-example}
572572
573573Implicit presence:
574574
@@ -596,7 +596,7 @@ if (m.hasFoo()) {
596596}
597597```
598598
599- #### Objective-C Example
599+ #### Objective-C Example {#objc-example}
600600
601601Implicit presence:
602602
0 commit comments