mirror of
https://github.com/vbatts/go-mtree.git
synced 2025-10-04 12:31:00 +00:00
compare: export official way to modify InodeDelta.Diff
At the moment, filtering out keyword changes from an InodeDelta after doing Compare is a little complicated and error-prone. The simplest solution is to allow for callers to access a pointer to the underlying slice so it can be modified properly. The filtering logic in the gomtree command-line implicitly depends on the behaviour of InodeDelta.Diff -- DiffPtr would be a far more appropriate replacement. Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
parent
d02f298ad4
commit
02df712987
2 changed files with 20 additions and 13 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
cli "github.com/urfave/cli/v2"
|
cli "github.com/urfave/cli/v2"
|
||||||
|
@ -463,19 +464,14 @@ loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only filter out the size keyword.
|
// Only filter out the size keyword.
|
||||||
// NOTE: This currently takes advantage of the fact the
|
keys := diff.DiffPtr()
|
||||||
// diff.Diff() returns the actual slice to diff.keys.
|
*keys = slices.DeleteFunc(*keys, func(kd mtree.KeyDelta) bool {
|
||||||
keys := diff.Diff()
|
return kd.Name() == "size"
|
||||||
for idx, k := range keys {
|
})
|
||||||
// Delete the key if it's "size". Unfortunately in Go you
|
// If there are no key deltas left after filtering, the entry
|
||||||
// can't delete from a slice without reassigning it. So we
|
// should be filtered out entirely.
|
||||||
// just overwrite it with the last value.
|
if len(*keys) == 0 {
|
||||||
if k.Name() == "size" {
|
continue loop
|
||||||
if len(keys) < 2 {
|
|
||||||
continue loop
|
|
||||||
}
|
|
||||||
keys[idx] = keys[len(keys)-1]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
compare.go
11
compare.go
|
@ -77,6 +77,17 @@ func (i InodeDelta) Diff() []KeyDelta {
|
||||||
return i.keys
|
return i.keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DiffPtr returns a pointer to the internal slice that would be returned by
|
||||||
|
// [InodeDelta.Diff]. This is intended to be used by tools which need to filter
|
||||||
|
// aspects of [InodeDelta] entries. If the [DifferenceType] of the inode is not
|
||||||
|
// [Modified], then DiffPtr returns nil.
|
||||||
|
func (i *InodeDelta) DiffPtr() *[]KeyDelta {
|
||||||
|
if i.diff == Modified {
|
||||||
|
return &i.keys
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Old returns the value of the inode Entry in the "old" DirectoryHierarchy (as
|
// Old returns the value of the inode Entry in the "old" DirectoryHierarchy (as
|
||||||
// determined by the ordering of parameters to Compare).
|
// determined by the ordering of parameters to Compare).
|
||||||
func (i InodeDelta) Old() *Entry {
|
func (i InodeDelta) Old() *Entry {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue