Skip to content

How to apply diffs #213

Open
Open
@Kira-Cesonia

Description

@Kira-Cesonia

My use case is that I want to store diffs and the original object in a database, and then at a later date restore different versions of the object using the diffs.

For that, I am looking for a functionality that lets me apply the diffs to the base object, like this:

	@Test
	void applyDiffDeltaShouldCorrectlyApplyDeltaToCity() {
		GameCharacter sylvia = GameCharacters.sylvia();
		GameCharacter sylviaEdited = GameCharacters.sylviaEdited();
		
		DiffNode characterDelta
			= objectDiffer.compare(sylvia, sylviaEdited);
		GameCharacter sylviaWithAppliedDelta
			= DeltaApplier.applyDelta(sylvia, characterDelta);
		
		assertEquals(sylviaEdited.getName(), sylviaWithAppliedDelta.getName());
	}

	public static <T> T applyDelta(T baseObject, DiffNode diff) {
		//This won't work, but it would be cool if it did:
		T updatedObject = baseObject + diff;
		return updatedObject ;
	}

This feels like something that java-object-diff should be able to do. However, all the examples in the documentation appear to require both the start and the end version of a document, in addition to the diffs.

So, how can I make this work? How can I create the end document just from the start document and one (or more) diffs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions