Repository navigation
fix(options): encode option message fields like protoc - #4
Conversation
- drop zero values of fields without presence (proto3 implicit, editions field_presence = IMPLICIT) inside option message literals - pack repeated scalars that are packed (proto3 and editions defaults, proto2 [packed = true]), including repeated extensions - protoc serializes these values through a parsed message, so the descriptor bytes differ otherwise; plugins that embed the raw descriptor then generate different code than protoc
- an extension set inside an option message literal now carries its
IsPacked into the encoder, so [(box) = {[n1.ext_packed]: [1, 2, 0]}]
and (box).(n1.ext_packed) are written packed, as protoc 36.2 does
- fields and extensions whose type resolves to a message in another
file no longer keep the Implicit and IsPacked computed while the
type was unresolved; the field symbol also gets the resolved type
|
Pushed 887596a with fixes for both review points. Nested extensions: Cross-file message fields: when a field's type is in another file, Tests:
All packages pass with |
Fixes the root cause of easyp-tech/easyp#269.
protoc parses a message-typed option value into a message and serializes that, so field presence and packing rules apply to it. easyprotoc wrote every assigned value straight to the wire, which gave two differences from protoc:
{on: false}on a proto3bool on,features.field_presence = IMPLICITin editions). protoc drops it; oneof members, proto3optional, map entries and proto2 fields keep it, same as before.[packed = true]in proto2, unlessrepeated_field_encoding = EXPANDED. That includes a repeated scalar extension used directly, likeoption (ints) = 1; option (ints) = 0;.The meaning of the descriptor doesn't change, only its bytes, but protoc-gen-go and protoc-gen-es embed the raw descriptor, so the generated code differed from protoc's for the same source. In our repo that showed up as 505 changed files after an easyp upgrade.
The linker now records, per field, whether it is implicit and whether it is packed, resolving features from the file, the enclosing messages and the field.
encodeScalarFielduses that for both message fields and top-level extensions.Testing:
TestOptionValuesEncodeLikeProtoccovers proto3, proto2, editions 2023 and repeated extensions with nested messages and a map; the expected bytes come from protoc 36.2 on the same sources, and the test fails on main.go test -race ./...and golangci-lint pass, andbench/run.sh paritystill reports "same as protoc" for wkt, compute, api-common and all of googleapis.