java-object-diff icon indicating copy to clipboard operation
java-object-diff copied to clipboard

How to exclude map key

Open chagen19 opened this issue 7 years ago • 1 comments

I'm looking for the correct way to exclude a map key. Below is an example that shows that the lastUpdate field is not being excluded. Any help would be greatly appreciated.

@Test
public void shouldBeEqualWhenObjectsAreNotEqualButFieldIsConfiguredToBeIgnored() {
	Map<String, Object> before = new LinkedHashMap<>();
	before.put("field1", "value1");
	before.put("excludedField", "excludedField value");

	Map<String, Object> after = new HashMap<>();
	after.put("field1", "value1");

	//@formatter:off
	DiffNode root = ObjectDifferBuilder
			.startBuilding()
			.inclusion()
			.exclude()
			.propertyName("excludedField")
			.node(NodePath.with("excludedField"))
			.and()
			.build()
			.compare(after, before);
	//@formatter:on

	List<Map<String, Object>> results = new ArrayList<>();
	DiffNode.Visitor visitor = (node, visit) -> {
		if (node.hasChanges() && !node.hasChildren()) {
			final Object beforeValue = node.canonicalGet(before);
			final Object afterValue = node.canonicalGet(after);

			Map<String, Object> changeMap = new HashMap<>();
			changeMap.put("state", node.getState().name());
			changeMap.put("path", node.getPath().toString());
			changeMap.put("beforeValue", beforeValue);
			changeMap.put("afterValue", afterValue);
			results.add(changeMap);
		}
	};
	root.visit(visitor);
	Assert.assertThat(results, hasSize(0));
}

chagen19 avatar Jul 13 '18 20:07 chagen19

@chagen19 Have you resolved this issue?

yayaryna avatar May 13 '19 12:05 yayaryna