Skip to content

Instantly share code, notes, and snippets.

@tomaslin
Last active March 20, 2019 23:41
Show Gist options
  • Save tomaslin/3a02fb1f201c7a6ecd55319e89761e41 to your computer and use it in GitHub Desktop.
Save tomaslin/3a02fb1f201c7a6ecd55319e89761e41 to your computer and use it in GitHub Desktop.
Simple diff for shaded proto messages in Java/Grpc
// 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