-
Notifications
You must be signed in to change notification settings - Fork 547
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
Introduce equality comparers for OpenXmlElement #1476
Conversation
Problem: The current way to determine equality for two OpenXmlElements seems to be x.OuterXml.Equals(y.OuterXml) (based on StackOverflow top answer). This performs horribly due to allocation of strings and XmlWriter. Solution: Make OpenXmlElement support quality via the IEquatable interface. Notes: For unparsed OpenXmlElement the order of the given XML element matters, this insn't the case for parsed ones. This both happens for child elements order and attributes. The order of ExtendedAttributes order always matters.
@twsouthwick Your eyes on this PR would be much appreciated. There is a few things that I would like some pointer with:
|
@PhDuck Great questions. I'll answer them first, then a few thoughts at the end:
With the points you're making, I think we may not want to implement equality directly on the element at this point, but rather provide helpers to create equality comparers that compare things people care about (you've pointed out questions that could be things people opt in/out of) I'm thinking something like this: public static class OpenXmlElementComparers
{
public static IEqualityComparer<OpenXmlElement> Default { get; }
public static IEqualityComparer<OpenXmlElement> Create(OpenXmlElementEqualityOptions options);
}
public sealed class OpenXmlElementEqualityOptions
{
// Include options
} |
According to the XML specification order of attributes is not significant and therefore it shouldn't be for us as well. If we see a problem with this, definitely we want to investigate. |
@PhDuck we've added GH action tests this week so if you merge main you'll be able to run tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Some of the changes won't work well with the multitargeting (I commented on a few things) - I'll kick the build off so we can see the results
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityComparer.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityOptions.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityComparer.cs
Outdated
Show resolved
Hide resolved
Can you merge from main so the tests will run? Edit: nm they're running |
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityOptions.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityOptions.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityComparerFactory.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityOptions.cs
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementComparers.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementEqualityComparer.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/MarkupCompatibilityAttributes.cs
Outdated
Show resolved
Hide resolved
test/DocumentFormat.OpenXml.Tests/OpenXmlElementEqualityTest.cs
Outdated
Show resolved
Hide resolved
@PhDuck I pushed a change that allows using the built in hashcode shim - it's in a different namespace |
src/DocumentFormat.OpenXml.Framework/MarkupCompatibilityAttributes.cs
Outdated
Show resolved
Hide resolved
src/DocumentFormat.OpenXml.Framework/Equality/OpenXmlElementComparers.cs
Outdated
Show resolved
Hide resolved
Thanks @PhDuck! This is cool feature that will be available in v3.0 when it's released |
Problem:
The current way to determine equality for two OpenXmlElements seems to
be x.OuterXml.Equals(y.OuterXml) (based on StackOverflow top answer).
This performs horribly due to allocation of strings and XmlWriter.
Solution:
This PR introduces OpenXmlElementEqualityComparer which supports equality determination.
Furthermore, the definition of equality is defined by OpenXmlElementEqualityOptions which allows for controlling how equality should be defined.
Benchmark results comparing to x.OuterXml.Equals(y.OuterXml):