/
traverse-item.go
96 lines (85 loc) 路 3.05 KB
/
traverse-item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package nav
import (
"io/fs"
"github.com/snivilised/extendio/xfs/utils"
)
// ExtendedItem provides extended information if the client requests
// it by setting the DoExtend boolean in the traverse options.
type ExtendedItem struct {
Depth int // traversal depth relative to the root
IsLeaf bool // defines whether this node a leaf node
Name string // derived as the leaf segment from filepath.Split
Parent string // derived as the directory from filepath.Split
SubPath string // represents the path between the root and the current item
NodeScope FilterScopeBiEnum // type of folder corresponding to the Filter Scope
Custom any // to be set and used by the client
}
// TraverseItem info provided for each file system entity encountered
// during traversal. The root item does not have a DirEntry because it is
// not created as a result of a readDir invoke. Therefore, the client has
// to know that when its function is called back, they will be no DirEntry
// for the root entity.
type TraverseItem struct {
Path string
Entry fs.DirEntry // contains a FileInfo via Info() function
Info fs.FileInfo // optional file info instance
Extension ExtendedItem // extended information about the file system node, if requested
Error error
Children []fs.DirEntry
filteredOut bool
Parent *TraverseItem
admit bool
dir bool
}
func isDir(item *TraverseItem) bool {
if !utils.IsNil(item.Entry) {
return item.Entry.IsDir()
} else if !utils.IsNil(item.Info) {
return item.Info.IsDir()
}
// only get here in error scenario, because neither Entry or Info is set
//
return false
}
func newTraverseItem(
path string, entry fs.DirEntry, info fs.FileInfo, parent *TraverseItem, err error,
) *TraverseItem {
item := &TraverseItem{
Path: path,
Entry: entry,
Info: info,
Parent: parent,
Children: []fs.DirEntry{},
Error: err,
}
item.dir = isDir(item)
return item
}
// clone makes shallow copy of TraverseItem (except the error).
func (ti *TraverseItem) clone() *TraverseItem {
return &TraverseItem{
Path: ti.Path,
Entry: ti.Entry,
Info: ti.Info,
Extension: ti.Extension,
Children: ti.Children,
}
}
func (ti *TraverseItem) IsDirectory() bool {
return ti.dir
}
func (ti *TraverseItem) filtered() {
// 馃摎 filtered is used by sampling functions to mark an item as already having
// been filtered. Sampling functions require the ability to 'Preview' an item
// so that it can be filtered, but doing so means there is potential for a
// child item to be double filtered. By marking an item is being pre-filtered,
// when the navigation process reaches the child entry in its own right (as
// opposed to being previewed), the filter can be bypassed and the client
// callback for this item can be invoked; ie if an item passes filtering at
// the preview stage, it does not needed to be filtered again.
//
ti.admit = true
}
func (ti *TraverseItem) key() string {
return ti.Extension.SubPath
}