-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix(object): add support for raw leaves in object diff #5472
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,26 +94,24 @@ func ApplyChange(ctx context.Context, ds ipld.DAGService, nd *dag.ProtoNode, cs | |
return e.Finalize(ctx, ds) | ||
} | ||
|
||
// Diff returns a set of changes that transform node 'a' into node 'b' | ||
// Diff returns a set of changes that transform node 'a' into node 'b'. | ||
// It only traverses links in the following cases: | ||
// 1. two node's links number are greater than 0. | ||
// 2. both of two nodes are ProtoNode. | ||
// Otherwise, it compares the cid and emits a Mod change object. | ||
func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, error) { | ||
// Base case where both nodes are leaves, just compare | ||
// their CIDs. | ||
if len(a.Links()) == 0 && len(b.Links()) == 0 { | ||
if a.Cid().Equals(b.Cid()) { | ||
return []*Change{}, nil | ||
} | ||
return []*Change{ | ||
&Change{ | ||
Type: Mod, | ||
Before: a.Cid(), | ||
After: b.Cid(), | ||
}, | ||
}, nil | ||
return getChange(a, b) | ||
} | ||
|
||
var out []*Change | ||
cleanA := a.Copy().(*dag.ProtoNode) | ||
cleanB := b.Copy().(*dag.ProtoNode) | ||
cleanA, okA := a.Copy().(*dag.ProtoNode) | ||
cleanB, okB := b.Copy().(*dag.ProtoNode) | ||
if !okA || !okB { | ||
return getChange(a, b) | ||
} | ||
|
||
// strip out unchanged stuff | ||
for _, lnk := range a.Links() { | ||
|
@@ -132,17 +130,7 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e | |
return nil, err | ||
} | ||
|
||
anodepb, ok := anode.(*dag.ProtoNode) | ||
if !ok { | ||
return nil, dag.ErrNotProtobuf | ||
} | ||
|
||
bnodepb, ok := bnode.(*dag.ProtoNode) | ||
if !ok { | ||
return nil, dag.ErrNotProtobuf | ||
} | ||
|
||
sub, err := Diff(ctx, ds, anodepb, bnodepb) | ||
sub, err := Diff(ctx, ds, anode, bnode) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -209,3 +197,16 @@ func MergeDiffs(a, b []*Change) ([]*Change, []Conflict) { | |
} | ||
return out, conflicts | ||
} | ||
|
||
func getChange(a, b ipld.Node) ([]*Change, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicely encapsulated! |
||
if a.Cid().Equals(b.Cid()) { | ||
return []*Change{}, nil | ||
} | ||
return []*Change{ | ||
{ | ||
Type: Mod, | ||
Before: a.Cid(), | ||
After: b.Cid(), | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not that much (I think), we could add a check here to verify that, if they have links, i.e., they are not raw nodes because they passed the first check (hence we would still be supporting them), they must be proto-nodes; if not, you could also return a
Mod
as before. Actually you could even merge this proto-node check in the previousif
, WDYT?