-
Notifications
You must be signed in to change notification settings - Fork 53
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
Store original element (attributes?) on the change objects #33
Comments
Storing the node wouldn't work, as the node is being modified while diffing. Storing a copy would increase memory requirements a lot. In the case of UpdateAttribute, storing the original value is possible, but it becomes a strange special case. I'll have to think about that. The NamedTuples at the moment act only as "storage", they don't have and do not need any additional functionality, so using |
I do understand that I am working on a special case and we wouldn't want to infect anything with a custom mess. Though I wonder if diff-with-exceptions should actually be uncommon. Yes, I ran into the modification issue. As to memory, this feature could be optional, otherwise just store But, perhaps the solution lies more in #10? If while walking the list of changes I could apply them to (a copy of) my original left then I could reliably use the |
What about a map from the being-modified tree elements back to the actual original element in the passed-in tree? That wouldn't cost much. Just a reference and a |
But maybe I misread and the entire thing passed in is mutated and the caller must have made a copy if they didn't want the diff to destroy their data... |
No, xmldiff will make a copy. We can store the original value on UpdateAttrib, UpdateTextIn and UpdateTextAfter. Original names for RenameNode and RenameAttrib also makes sense I guess. |
Thanks for the clarification. In my case I need to check a different attribute, not the one described by the |
Ah, yes, you are right, I get what you mean. I wonder if some sort of pre-stage where the data is massaged could be an option? I don't want to make copies of every element when diffing, and if there was such a copy, then the next case is that a change is allowed if a sibling-element is an element set, etc. :-) So the generic way to support this is indeed to make it easier to iterate over the changes with a current tree, ie issue #10 as you say. |
I don't think what I suggested requires any more copying than normal. Assuming I understood correctly that the two trees passed in to xmldiff are not mutated. If so, they are the original and you hold a reference to the related element(s?) in them. The only additional storage is the attribute on the change object holding the reference. The actual element already exists. Doing this gives full context on the original left side from the point of the change. But there may be something I'm missing about the diff process. It may be needed to make a copied-element-to-be-modified-by-diff -> original-element mapping to make setting the proper original element on the change easy. I'm willing to try a PR on this but I like to have a sense I'm going a plausible direction first. |
The I can plan to do a PR for this, but I do like to make sure I'm heading a sensible direction. |
I think a possible way to do this could be with events. You wouldn't get a reference to the original element, but to the current element in the left tree, that would be doable. |
Perhaps I just need to dig into the code. It seems I'm missing something important about the implementation. Thanks for all the discussion, I'll see what I can learn. |
Since you mentioned issue #10, I just wanted to note that I have added a tool to patch trees. Not sure it would help in this case, though. I'm leaning more towards this being a case for massaging the data ahead of the diff. |
It would be helpful to keep the original element, or at least its direct attributes, with the change objects. The diffing I just needed to do included ignoring various differences that are allowable. In this case just ignoring some of the generated diff items is sufficient for now (only
UpdateAttrib
so no issue even if interpreting moves etc in the modified diff). This saves me from considering how to embed this flexibility into the actual diff engine.The change objects do include the node xpath for the left side but I suspect that can't be directly used since it would presumably be wrong if a previous action in the diff required a move or delete etc that affected the index of the present change node.
Would a PR be considered for this? I do see that the elements are mutated while building the diff but I didn't dig past that to see how I might capture the original element, or at least its attributes.
In case I'm heading down the wrong path, below are couple example elements I would like to end up either matching or ignoring the resulting diff of. The logic is that if the
type
has the value"42"
then a modification ofaccess
from"r"
to"rw"
is allowed.What about attrs instead of
namedtuple
s? Aside from generally liking attrs it came to my mind as I thought about adding the extra field but probably not wanting to change therepr
. I'm used to attrs but I wondered what it would look like withnamedtuple
so I went ahead and made up a comparison. Obviously I've already got the code fornamedtuple
now so... whatever, but I figured I'd ask anyways.Cheers,
-kyle
`namedtuple` and attrs repr example
The text was updated successfully, but these errors were encountered: