The current implementation of our API diffing does not handle every type of item that can be defined within a module.
It will compare items that we support, detecting addition, removal and modification of said items. If it fails to detect any such change, it will default back to comparing the whole module using Includemod.signatures.
Our ultimate goal is to support fine grained diffing of each and every signature items.
To get one step closer, we would like to add detection of the addition and removal of modtype_declaration items.
How to proceed
The diffing logic and representation is found in lib/diff.ml.
The content of a module is represented as a list even though the order does not matter there. To work around that we first build a map of its content, indexed by pairs of item type and name. We then proceed to compare the two maps we built from the two versions of the module we are diffing. If both map contain a mapping for a given type/name pair, we compare them to try and detect whether it was modified. If only one of the map contains a binding for a given pair, that means that it was either added or removed, based on which map has the binding.
To detect addition/removal of module type declarations, we will have to extend the item_type and sig_items types which are used to build the map mentioned above. These are then used in the extract_items function that builds the map from the module signature.
Finally, the actual diffing logic's entry point is the Diff.items function which handles the comparison of the two maps via the merge function (you can read the documentation here).
Testing
This should be tested at the library level. You can add a new tests/api-watch/test_diff_modtype_decl.ml file where you can write tests for type addition or removal. You can use the tests/api-watch/test_diff.ml or tests/api-watch/test_diff_module.ml files as examples on how to write such tests.
The current implementation of our API diffing does not handle every type of item that can be defined within a module.
It will compare items that we support, detecting addition, removal and modification of said items. If it fails to detect any such change, it will default back to comparing the whole module using
Includemod.signatures
.Our ultimate goal is to support fine grained diffing of each and every signature items.
To get one step closer, we would like to add detection of the addition and removal of modtype_declaration items.
How to proceed
The diffing logic and representation is found in
lib/diff.ml
.The content of a module is represented as a list even though the order does not matter there. To work around that we first build a map of its content, indexed by pairs of item type and name. We then proceed to compare the two maps we built from the two versions of the module we are diffing. If both map contain a mapping for a given type/name pair, we compare them to try and detect whether it was modified. If only one of the map contains a binding for a given pair, that means that it was either added or removed, based on which map has the binding.
To detect addition/removal of module type declarations, we will have to extend the
item_type
andsig_items
types which are used to build the map mentioned above. These are then used in theextract_items
function that builds the map from the module signature.Finally, the actual diffing logic's entry point is the
Diff.items
function which handles the comparison of the two maps via the merge function (you can read the documentation here).Testing
This should be tested at the library level. You can add a new
tests/api-watch/test_diff_modtype_decl.ml
file where you can write tests for type addition or removal. You can use thetests/api-watch/test_diff.ml
ortests/api-watch/test_diff_module.ml
files as examples on how to write such tests.