Skip to content
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

parse pb in python, member is empty if its' value is zero. #2796

Closed
ruinanchen opened this issue Mar 7, 2017 · 3 comments
Closed

parse pb in python, member is empty if its' value is zero. #2796

ruinanchen opened this issue Mar 7, 2017 · 3 comments

Comments

@ruinanchen
Copy link

ruinanchen commented Mar 7, 2017

In our system, we use golang implement tcp server and python implement a test client. We construct communication protocol base on protocol buffer. I define a pb as follow:

message MsgSendError {
    int64 from_id               = 1;
    int64 to_id                 = 2;
    MsgSendErrorCode error_code = 3;
}
enum MsgSendErrorCode {
    NOT_FRIEND               = 0x00;
    MSG_INVALID              = 0x01;
}

Simple our problem:
Our server construct MsgSendError with error_code's value equal NOT_FRIEND(0x00), then serialize the pb and send binary to client. Our python client parse the binary, but can't extract error_code. If error_code's value equal MSG_INVALID(0x01), error_code could be extracted. Why? Because of python is a dynamic language?

P.S. protoc's version is 3.0.0 in our machine.

@acozzette
Copy link
Member

This behavior is actually to be expected, because proto3 does not make a distinction between a primitive field being empty and being set to its default value (which in this case is the 0 value, NOT_FRIEND). In other words, there is no way to distinguish whether error_code was set to NOT_FRIEND or was not set at all. See here for more about proto3 enums: https://developers.google.com/protocol-buffers/docs/proto3#enum

I'll close this issue for now but feel free to reopen it if you have more questions.

@ruinanchen
Copy link
Author

ruinanchen commented Mar 8, 2017

Very very thank you. B.T.W, Is there any solution,make python get the default value in this case(even if it actually is empty)? or any suggestion?

@acozzette
Copy link
Member

When you access the field you should automatically get the default value (0 or the empty string depending on the type). Could you post some example code if this isn't working?

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

No branches or pull requests

2 participants