Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Add a SetInParent equivalent so that is possible to set empty messages as fields of another message #5

Open
ecerulm opened this issue Jun 8, 2015 · 2 comments

Comments

@ecerulm
Copy link

ecerulm commented Jun 8, 2015

for a message like

message ListRequest {
}

message ServerRequest {
   optional ListRequest list = 1;
}

There is no way (that I know of) to conveniently set the list field to be present. In the original protobuf you could

req = mypackage.ServerRequest()
req.list.SetInParent()

to ensure that the list would be included although optional and empty. But there seems that there is no way to do that with protobuf3. Or ?

@Pr0Ger
Copy link
Owner

Pr0Ger commented Jun 10, 2015

As I know empty repeated (or optional) field doesn't have any signs in binary representation, so this call wouldn't affect serialization result

@ecerulm
Copy link
Author

ecerulm commented Jun 10, 2015

It does show up in the binary encoding:

message StartRequest {
}

message StopRequest {
}

message ControlRequest {
  optional StartRequest start = 1;
  optional StopRequest stop = 2;
}

with Python 2.7 protobuf:

import message_pb2 as pb
req = pb.ControlRequest()
req.start.SetInParent()
req.stop.SetInParent()
with open("out.bin", "wb") as f:
    f.write(req.SerializeToString())

the out.bin shows the fields 1 and 2

hexdump -C out.bin
00000000  0a 00 12 00                                       |....|
00000004

and cat out.bin | protoc --decode_raw show that both fields are present:

1: ""
2: ""

if I comment out the req.start.SetInParent() line then I get a different binary of course:

00000000  0a 00                                             |..|
00000002

and protoc --decode_raw show that field start with tag 1 is missing:

2: ""

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants