Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability to Diff Against NULL? #20

Open
HARVS1789UK-VAIIE opened this issue Mar 24, 2023 · 0 comments
Open

Add Ability to Diff Against NULL? #20

HARVS1789UK-VAIIE opened this issue Mar 24, 2023 · 0 comments

Comments

@HARVS1789UK-VAIIE
Copy link

Firstly, thanks for the great library, I've just found it yesterday and have found it very useful and configurable so far!

One use case I have that it doesn't seem to cater for however is the ability for one (or arguably both) of the objects to be diff'ed to be null. I am using AnyDiff to identify differences between a source and target object for both insert and update operations against my data store. In the case of inserts, my 'source' object will be null and it would be extremely useful for AnyDiff.Diff() to return an ICollection<Differences> which includes all fields marked for comparison in the target object and their values in RightValue and I suppose null (or the most suitable 'empty' value given the fields data type) in LeftValue.

I cannot see anyway of achieving this currently unless I am missing something? I've tried markiny my object for comparison as nullable via T? in the types method call below:

ICollection<Difference> differences = AnyDiff.AnyDiff.Diff<User?>(
    existingUser,
    currentUser,
    ComparisonOptions.All | ComparisonOptions.AllowCollectionsToBeOutOfOrder,
    new string[] { "Id", "CreatedAt", "UpdatedAt" }
);

but this results in:

System.NullReferenceException: Object reference not set to an instance of an object.

Further stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
at TypeSupport.Extensions.<fd5fea14-b6d2-4046-8d81-b8115045ec3c>ObjectExtensions.GetProperties(Object obj, <4d7a44c8-b5f5-44bf-8471-f0980838d61e>PropertyOptions options)
at AnyDiff.DiffProvider.RecurseProperties(Object left, Object right, Object parent, List`1 differences, Int32 currentDepth, Int32 maxDepth, <37c2b6f9-1728-45df-bb6e-473d4690fe19>ObjectHashcodeMap objectTree, String path, ComparisonOptions comparisonOptions, ICollection`1 propertiesToExcludeOrInclude, DiffOptions diffOptions)
at AnyDiff.DiffProvider.ComputeDiff(Object left, Object right, Int32 maxDepth, ComparisonOptions comparisonOptions, DiffOptions diffOptions, String[] propertiesToExcludeOrInclude)
at AnyDiff.AnyDiff.Diff[T](T left, T right, ComparisonOptions comparisonOptions, String[] propertiesToExcludeOrInclude)

My best idea for a workaround currently would be to create additional classes with nullable and/or sane default fields e.g. BlankUser, enabling the AllowCompareDifferentObjects option and passing this in as my source in these scenarios.

If however, AnyDiff could allow for a null left value however, that would be great?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant