Search code examples
c++protocol-buffers

Get difference of proto buffer files


Is there any options to get difference between two protobufs using C++ and protobuf API? For example:
I have two files
1.txt

Config {
    OtherField:   42
    GoodField:    13
    AnotherField: 4200
}

2.txt

Config {
    OtherField:  19
    GoodField:   13
}

I want to get output like:

Diff:
OtherField   - 1.txt: 42
             - 2.txt: 19
AnotherField - 1.txt: 4200
             - 2.txt: missing

Solution

  • See google::protobuf::util::MessageDifferencer. Because there's no single definition for the "difference" between two proto messages, MessageDifferencer comes with a number of ways to customize the comparison, including

    • Ignoring specific fields with IgnoreField (maybe your message contains a globally unique ID that you don't want to compare)
    • Ignoring repeated field ordering with TreatAsSet - but note that set-like comparison is quadratic in the number of elements!
    • EQUIVALENT comparison for treating unset fields as though they were set to their default value

    Once you've configured the MessageDifferencer, use ReportDifferencesTo or ReportDifferencesToString to specify how you want to handle the conflicts between the messages, and finally call Compare to do the actual work of differencing.