How do I check if a message is "complete"?

105 views
Skip to first unread message

Klaim J. Lamotte

unread,
Jan 11, 2013, 10:39:36 AM1/11/13
to prot...@googlegroups.com
Hi,

I'm using protobuf 2.5.

Looking at the generated code of my messages and the documentation, I don't see a function that 
would be generated and would know how to check all fields, both required and optional, see
if they are all set (check the has_ members).

If such function already exists, could you point me to it please? I might have missed something.

To explain my context:
I have messages being basically "state" message of different objects, which provide full or partial state of an object.
These messages are used in other messages that might send partial update or full update/creation of an object.
Now, in some cases, I expect full updates, which means optional fields MUST be set when I receive the message (typically when I receive a message saying an object have been created on the server).
I want to add an assertion that the message is complete, but as said I can't find a way to do this.
Using only required fields would be expensive and duplicate code (though I'm consider doing that - I don't know if networking will be a bottleneck yet).
The IsInitialized() function is obviously not enough in my case.

Thanks for your time.

Joel Lamotte


Feng Xiao

unread,
Jan 11, 2013, 12:01:00 PM1/11/13
to Klaim J. Lamotte, prot...@googlegroups.com




--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/_k9eV1N1GKgJ.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.

Klaim - Joël Lamotte

unread,
Jan 11, 2013, 12:35:51 PM1/11/13
to Feng Xiao, prot...@googlegroups.com
On Fri, Jan 11, 2013 at 6:01 PM, Feng Xiao <xiao...@google.com> wrote:


Hi,

I have already read these several time before. I fail to see the relation with the question I'm asking.
What are you suggesting exactly? 

Joel Lamotte

Feng Xiao

unread,
Jan 13, 2013, 3:54:28 AM1/13/13
to Klaim - Joël Lamotte, prot...@googlegroups.com
With protobuf reflection you can easily write a function to check if all fields of a message are set.
 

Joel Lamotte


Klaim - Joël Lamotte

unread,
Jan 13, 2013, 7:04:51 AM1/13/13
to Feng Xiao, prot...@googlegroups.com
On Sun, Jan 13, 2013 at 9:54 AM, Feng Xiao <xiao...@google.com> wrote:

With protobuf reflection you can easily write a function to check if all fields of a message are set.


I see, thanks for pointing that, it's a good alternative.
But wouldn't it be far more efficient if it was generated by protobuf in the generated types?
At the moment I avoid using reflection where I can because of performance concerns.

Maybe I should make a ticket to request a function.

Joel Lamotte

 

Oliver Jowett

unread,
Jan 13, 2013, 9:34:37 AM1/13/13
to Klaim - Joël Lamotte, Feng Xiao, prot...@googlegroups.com
This seems like a function that would be only useful for your specific
requirement. It would be no use, for example, in any situation where
backwards message compatibility is needed.

If reflection is too expensive (have you tried it?) then you might
want to consider writing a code generator plugin to generate the code
that you want (see
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.plugin.pb)

Oliver

Klaim - Joël Lamotte

unread,
Jan 13, 2013, 6:27:07 PM1/13/13
to Oliver Jowett, Feng Xiao, prot...@googlegroups.com
On Sun, Jan 13, 2013 at 3:34 PM, Oliver Jowett <oliver...@gmail.com> wrote:
This seems like a function that would be only useful for your specific
requirement. It would be no use, for example, in any situation where
backwards message compatibility is needed.

Even with backward message compatibility, you might still want to know if the message is full or not.
This is because some protocoles (like replicating objects) require that state message are full even
if the reste of the messages in the messages can be uncomplete.
It's specific to a use case, but it's not specific to my case. I would have the same problem with
different other applications in the same domain. If I want maximum performance I mean.
 

If reflection is too expensive (have you tried it?)

Not yet but I will use this as it's the simplest way to do it right now, yes.
I believe it could not be a problem in performance, I just want to avoid latency on network message processing so 
I have everything coded as statically as possible.
But if it's slow then I will have to find another solution.
 
then you might
want to consider writing a code generator plugin to generate the code
that you want (see
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.plugin.pb)

Thanks I didn't know it was possible. 
Still I think it might be useful to others, but I guess I'll have to provide a solution myself.

Thanks both for pointing these info.

Joel Lamotte
 
Reply all
Reply to author
Forward
0 new messages