-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistency in protocol buffers specification? #28
Comments
I cannot tell from the documentation, but perhaps |
This is indeed confusing. I just tried it with the official Python implementation, and both cases result in I believe the key is interpreting the phrase
It seems that "appears" means "occurs explicitly in the serialized message". A field set to 0 never "appears" because it's set to a default value, and so it isn't present in the serialized message. This seems like a bad idea (it's surprising that Here's some code:
import test_pb2
x = test_pb2.Example()
x.foo = 1
xStr = x.SerializeToString()
y = test_pb2.Example()
y.foo = 0
yStr = y.SerializeToString()
fromConcat = test_pb2.Example()
fromConcat.ParseFromString(xStr + yStr)
fromMerge = test_pb2.Example()
fromMerge.ParseFromString(xStr)
yParsed = test_pb2.Example()
yParsed.ParseFromString(yStr)
fromMerge.MergeFrom(yParsed)
print('fromConcat: ' + fromConcat.__str__())
print('fromMerge: ' + fromMerge.__str__()) |
That seems to be consistent with @j6carey's interpretation, which is that merging two scalar fields returns the last field with a non-default value. In other words: newtype ProtobufInt32 = ProtobufInt { unProtobufInt :: Int32 }
instance Monoid ProtobufInt32 where
mempty = ProtobufInt32 0
mappend x (ProtobufInt32 0) = x
mappend _ x = x ... which is actually a law-abiding |
Agreed. Keep in mind that since the So the |
The protocol buffers encoding specification says:
Let's call this a monoid homomorphism law for
ParseFromString
However, this seems to conflict with another protocol buffers design decision to omit default fields when serializing message. Specifically, the protocol buffers 3 language guide says:
Defaulting values breaks the above monoid homomorphism law if you consider the following message type:
... and then you serialize the following two messages:
message1
: anExample
message with the fieldfoo
set to1
message2
: anExample
message with the fieldfoo
set to0
If you deserialize each
ByteString
independently and combine the resultingMessage
s you would get the fieldfoo
set to0
(since the latterMessage
would default tofoo = 0
and fields from the latterMessage
take precedence), but if you combined theByteString
s before deserializing, you would get the fieldfoo
set to1
(since the0
field was never explicitly serialized)Is there something that I'm missing or is the specification inconsistent?
The text was updated successfully, but these errors were encountered: