Fix primitive default-value round-trip and preserve -0.0 in the codec#112
Closed
ghostdogpr wants to merge 1 commit into
Closed
Fix primitive default-value round-trip and preserve -0.0 in the codec#112ghostdogpr wants to merge 1 commit into
ghostdogpr wants to merge 1 commit into
Conversation
Two encode/decode consistency bugs in the primitive codec: - Decode restored an absent field from the schema-captured default (which the deriver populates from the Scala case-class default parameter), while encode omits a field whenever it equals the type's zero. A primitive field with a non-zero declared default (e.g. Int = 5) therefore turned an encoded 0 into 5 on decode. For primitive fields, use the proto3 zero as the decode default so encode and decode agree; message/enum/option/collection defaults are unchanged. - Field presence for Double/Float used value == 0, and -0.0 == 0.0 in IEEE, so -0.0 was dropped and round-tripped to +0.0 (also diverging from protobuf-java, which gates on raw bits). Presence now checks raw bits, with a value == 0 short-circuit so non-zero values never call doubleToRawLongBits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two encode/decode consistency bugs in the primitive codec, both surfaced by a fresh bug-hunt pass.
1. Non-zero primitive defaults corrupt the round-trip
The deriver populates a
SimpleField's decode default from the schema default, which zio-blocks derives from the Scala case-class default parameter. So forcase class Foo(x: Int = 5), the field's decode default is5. But encode omits a primitive field whenever it equals the type's zero (value == 0,value.isEmpty, …) — it never consults the declared default. Result:Encode categorically ignores the declared default for every field category, so decode must too. For primitive fields the decode default is now the proto3 zero (
getDefaultValue), matching encode. Message / enum / option / collection / transform defaults are untouched.2.
-0.0is droppedPresence for
Double/Floatusedvalue == 0d/value == 0f. Since-0.0 == 0.0under IEEE equality,-0.0was treated as the omittable default and round-tripped to+0.0— also diverging from protobuf-java, whose generated code gates ondoubleToRawLongBits(value) != 0. Presence now checks raw bits, with avalue == 0short-circuit so ordinary non-zero values never calldoubleToRawLongBits(keeping the JS hot path unaffected).Tests
Two regression tests (red before the fix, green after): a primitive field with a non-zero default round-tripping its zero value, and
-0.0double/float fields preserving their sign bit (compared by raw bits). Full core suite: 139 passed.