How can I optimize complex nested protobuf validation in C ++?

I use Google protocol buffers in a messaging application. Protos are often nested at several levels in depth, and we accepted Google’s recommendations and made them all optional. Protos can describe many different types of overlapping messages, that is, a message of type == X must contain the element my_X, type == Y must contain my_Y. In addition, certain combinations of fields impose other restrictions on which fields should be present and what values ​​they should have. Changing the proto-structure is beyond what I can do.

Right now, this whole check is in the mess of if..else. If the cases do not match, this may be workable, but the verification cases may impose various restrictions on a particular field, so it can become quite ugly. Is there a better way? My goal is to make the code more convenient. I was looking a bit for functors / predicates, and it looks like this might simplify the problem, but it still seems like it will be a mess.

+4
source share
1 answer

If the code starts to contain too many ifs and elses, then sometimes a table-based approach is a solution. Chapter 18 Code Complete Edition 2 explains a good concept with a lot of examples. You can find some examples in this article .

Hoping to help.

0
source

Source: https://habr.com/ru/post/1314992/


All Articles