Protocol Buffers preserve compatibility through stable field numbers and wire types. Removing a field without reserving its number can let a future field accidentally reinterpret old data, while changing a field's wire type can make existing serialized messages unsafe to read.
Design a ProtobufCompatibilityChecker class:
ProtobufCompatibilityChecker() creates a stateless checker.boolean isBackwardCompatible(int[][] oldFields, int[][] newFields, int[] reservedNumbers) validates a schema change.
Each field is [fieldNumber, wireType]. Return true only when all these rules hold:
- Field numbers are unique within
oldFields and within newFields. - Reserved numbers are unique.
- A field number present in both schemas keeps the same wire type.
- Every old field number absent from the new schema appears in
reservedNumbers. - No new field uses a reserved number.
Row order does not matter. Supported wire types are 0, 1, 2, and 5; inputs contain only supported values.
Example 1:
Input:
Output:
Explanation: Existing field numbers keep their wire types, and adding field 3 is safe.
Example 2:
Input:
Output:
Explanation: Field 2 was removed, but its number is reserved so it cannot be reused accidentally.
Constraints
0 <= oldFields.length, newFields.length, reservedNumbers.length <= 1000oldFields[i].length == newFields[i].length == 21 <= fieldNumber <= 2^29 - 1wireType is one of 0, 1, 2, or 5.- Inputs may contain duplicate numbers; duplicates make the result
false. - At most
100 calls are made to isBackwardCompatible.