Protobuf editions
Protobuf editions replace the syntax = "proto3" line at the top of a proto file with edition = "2023". Instead of one switch between proto2 and proto3, an edition has features which you set per file, per message or per field. As of version 4.0.0 Embedded Proto supports edition 2023 and edition 2024. The table below lists the features and their support:
| Feature | Support |
|---|---|
field_presence | Full. EXPLICIT, the default in 2023, tracks presence like an optional field and generates has_xxx(). IMPLICIT behaves like proto3. LEGACY_REQUIRED is always serialized. |
| Custom default values | Scalar and enum fields. The member starts at the default and clear_xxx() restores it. Defaults for string and bytes fields are not supported and ignored with a warning. |
repeated_field_encoding | Full. PACKED and EXPANDED are both written as asked, and both are accepted when reading. |
enum_type | Full. A CLOSED enum validates the value received and drops an unknown one. |
message_encoding | Full. See below for DELIMITED. |
utf8_validation | Treated as NONE. Embedded Proto performs no UTF-8 validation. |
json_format | Ignored. Embedded Proto has no JSON support. |
| Edition 2024 | Full. Its additions are naming concerns for the generator and have no effect at run time. |
Single pass serialization with delimited messages
A nested message is normally written with its length in front of it. Embedded Proto therefore calculates the size of the nested message first, and writes it afterwards. With delimited encoding the nested message is written between a start and an end marker instead, and no length is needed. The message is serialized in a single pass, and a callback field can be nested this way.
edition = "2023";
// Make every nested message in this file delimited.
option features.message_encoding = DELIMITED;Both sides must agree on the encoding, which they do by sharing the proto file. Reading a delimited message is slightly slower, as the reader scans for the end marker instead of jumping ahead by a known length.
Limitations
- Unknown fields are not kept when a message is deserialized and serialized again. As a result a CLOSED enum drops an unknown value rather than preserving it.
- Edition 2024 requires a protoc that can parse edition 2024. The one which comes with the package can.
Proto2 remains unsupported. Files with syntax = "proto3" work as before.