Last active
March 20, 2019 23:41
-
-
Save tomaslin/3a02fb1f201c7a6ecd55319e89761e41 to your computer and use it in GitHub Desktop.
Simple diff for shaded proto messages in Java/Grpc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: diff(Message, OtherMessage).empty | |
def diff(x, y) { | |
deepEquals(x.newBuilder().build().getDescriptorForType().fields, | |
y.newBuilder().build().getDescriptorForType().fields) | |
} | |
def deepEquals(List<FieldDescriptor> fields1, List<FieldDescriptor> fields2) { | |
if (fields1.size() != fields2.size()) { | |
return ["Missing field between ${fields1*.name} and ${fields2*.name}"] | |
} | |
def errors = [] | |
fields1.eachWithIndex { field, count -> | |
def otherField = fields2.get(count) | |
if (field.type != otherField.type) { | |
errors.add "Field Type mismatch between ${field.name} and ${otherField.name}" | |
} | |
if (field.jsonName != otherField.jsonName) { | |
errors.add "JsonName mismatch between ${field.name} and ${otherField.name}" | |
} | |
if (field.type.equals(FieldDescriptor.Type.MESSAGE)) { | |
def childError = deepEquals(field.messageType.fields, otherField.messageType.fields) | |
if (!childError.empty) { | |
errors.add "Errors found in child message for ${field.name}" | |
errors.addAll(childError) | |
} | |
} | |
} | |
return errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment