diff --git a/README.md b/README.md index 313dfe9..c8af456 100644 --- a/README.md +++ b/README.md @@ -720,11 +720,12 @@ func main() { callback2 := func(wn *gtree.WalkerNode) error { fmt.Println("WalkerNode's methods called...") - fmt.Printf("\tName : %s\n", wn.Name()) - fmt.Printf("\tBranch : %s\n", wn.Branch()) - fmt.Printf("\tRow : %s\n", wn.Row()) - fmt.Printf("\tLevel : %d\n", wn.Level()) - fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tName : %s\n", wn.Name()) + fmt.Printf("\tBranch : %s\n", wn.Branch()) + fmt.Printf("\tRow : %s\n", wn.Row()) + fmt.Printf("\tLevel : %d\n", wn.Level()) + fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tHasChild : %t\n", wn.HasChild()) return nil } @@ -734,59 +735,68 @@ func main() { } // Output: // WalkerNode's methods called... - // Name : a - // Branch : - // Row : a - // Level : 1 - // Path : a + // Name : a + // Branch : + // Row : a + // Level : 1 + // Path : a + // HasChild : true // WalkerNode's methods called... - // Name : i - // Branch : ├── - // Row : ├── i - // Level : 2 - // Path : a/i + // Name : i + // Branch : ├── + // Row : ├── i + // Level : 2 + // Path : a/i + // HasChild : true // WalkerNode's methods called... - // Name : u - // Branch : │ └── - // Row : │ └── u - // Level : 3 - // Path : a/i/u + // Name : u + // Branch : │ └── + // Row : │ └── u + // Level : 3 + // Path : a/i/u + // HasChild : true // WalkerNode's methods called... - // Name : k - // Branch : │ └── - // Row : │ └── k - // Level : 4 - // Path : a/i/u/k + // Name : k + // Branch : │ └── + // Row : │ └── k + // Level : 4 + // Path : a/i/u/k + // HasChild : false // WalkerNode's methods called... - // Name : kk - // Branch : └── - // Row : └── kk - // Level : 2 - // Path : a/kk + // Name : kk + // Branch : └── + // Row : └── kk + // Level : 2 + // Path : a/kk + // HasChild : true // WalkerNode's methods called... - // Name : t - // Branch : └── - // Row : └── t - // Level : 3 - // Path : a/kk/t + // Name : t + // Branch : └── + // Row : └── t + // Level : 3 + // Path : a/kk/t + // HasChild : false // WalkerNode's methods called... - // Name : e - // Branch : - // Row : e - // Level : 1 - // Path : e + // Name : e + // Branch : + // Row : e + // Level : 1 + // Path : e + // HasChild : true // WalkerNode's methods called... - // Name : o - // Branch : └── - // Row : └── o - // Level : 2 - // Path : e/o + // Name : o + // Branch : └── + // Row : └── o + // Level : 2 + // Path : e/o + // HasChild : true // WalkerNode's methods called... - // Name : g - // Branch : └── - // Row : └── g - // Level : 3 - // Path : e/o/g + // Name : g + // Branch : └── + // Row : └── g + // Level : 3 + // Path : e/o/g + // HasChild : false } ``` @@ -1146,11 +1156,12 @@ func main() { callback2 := func(wn *gtree.WalkerNode) error { fmt.Println("WalkerNode's methods called...") - fmt.Printf("\tName : %s\n", wn.Name()) - fmt.Printf("\tBranch : %s\n", wn.Branch()) - fmt.Printf("\tRow : %s\n", wn.Row()) - fmt.Printf("\tLevel : %d\n", wn.Level()) - fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tName : %s\n", wn.Name()) + fmt.Printf("\tBranch : %s\n", wn.Branch()) + fmt.Printf("\tRow : %s\n", wn.Row()) + fmt.Printf("\tLevel : %d\n", wn.Level()) + fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tHasChild : %t\n", wn.HasChild()) return nil } @@ -1160,41 +1171,47 @@ func main() { } // Output: // WalkerNode's methods called... - // Name : root - // Branch : - // Row : root - // Level : 1 - // Path : root + // Name : root + // Branch : + // Row : root + // Level : 1 + // Path : root + // HasChild : true // WalkerNode's methods called... - // Name : child 1 - // Branch : ├── - // Row : ├── child 1 - // Level : 2 - // Path : root/child 1 + // Name : child 1 + // Branch : ├── + // Row : ├── child 1 + // Level : 2 + // Path : root/child 1 + // HasChild : true // WalkerNode's methods called... - // Name : child 2 - // Branch : │ └── - // Row : │ └── child 2 - // Level : 3 - // Path : root/child 1/child 2 + // Name : child 2 + // Branch : │ └── + // Row : │ └── child 2 + // Level : 3 + // Path : root/child 1/child 2 + // HasChild : true // WalkerNode's methods called... - // Name : child 3 - // Branch : │ ├── - // Row : │ ├── child 3 - // Level : 4 - // Path : root/child 1/child 2/child 3 + // Name : child 3 + // Branch : │ ├── + // Row : │ ├── child 3 + // Level : 4 + // Path : root/child 1/child 2/child 3 + // HasChild : false // WalkerNode's methods called... - // Name : child 4 - // Branch : │ └── - // Row : │ └── child 4 - // Level : 4 - // Path : root/child 1/child 2/child 4 + // Name : child 4 + // Branch : │ └── + // Row : │ └── child 4 + // Level : 4 + // Path : root/child 1/child 2/child 4 + // HasChild : false // WalkerNode's methods called... - // Name : child 5 - // Branch : └── - // Row : └── child 5 - // Level : 2 - // Path : root/child 5 + // Name : child 5 + // Branch : └── + // Row : └── child 5 + // Level : 2 + // Path : root/child 5 + // HasChild : false } ``` diff --git a/example/like_cli/main.go b/example/like_cli/main.go index 0e9fdd8..e677580 100644 --- a/example/like_cli/main.go +++ b/example/like_cli/main.go @@ -114,11 +114,12 @@ func sampleWalker() error { callback2 := func(wn *gtree.WalkerNode) error { fmt.Println("WalkerNode's methods called...") - fmt.Printf("\tName : %s\n", wn.Name()) - fmt.Printf("\tBranch : %s\n", wn.Branch()) - fmt.Printf("\tRow : %s\n", wn.Row()) - fmt.Printf("\tLevel : %d\n", wn.Level()) - fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tName : %s\n", wn.Name()) + fmt.Printf("\tBranch : %s\n", wn.Branch()) + fmt.Printf("\tRow : %s\n", wn.Row()) + fmt.Printf("\tLevel : %d\n", wn.Level()) + fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tHasChild : %t\n", wn.HasChild()) return nil } @@ -127,59 +128,68 @@ func sampleWalker() error { } // Output: // WalkerNode's methods called... - // Name : a - // Branch : - // Row : a - // Level : 1 - // Path : a + // Name : a + // Branch : + // Row : a + // Level : 1 + // Path : a + // HasChild : true // WalkerNode's methods called... - // Name : i - // Branch : ├── - // Row : ├── i - // Level : 2 - // Path : a/i + // Name : i + // Branch : ├── + // Row : ├── i + // Level : 2 + // Path : a/i + // HasChild : true // WalkerNode's methods called... - // Name : u - // Branch : │ └── - // Row : │ └── u - // Level : 3 - // Path : a/i/u + // Name : u + // Branch : │ └── + // Row : │ └── u + // Level : 3 + // Path : a/i/u + // HasChild : true // WalkerNode's methods called... - // Name : k - // Branch : │ └── - // Row : │ └── k - // Level : 4 - // Path : a/i/u/k + // Name : k + // Branch : │ └── + // Row : │ └── k + // Level : 4 + // Path : a/i/u/k + // HasChild : false // WalkerNode's methods called... - // Name : kk - // Branch : └── - // Row : └── kk - // Level : 2 - // Path : a/kk + // Name : kk + // Branch : └── + // Row : └── kk + // Level : 2 + // Path : a/kk + // HasChild : true // WalkerNode's methods called... - // Name : t - // Branch : └── - // Row : └── t - // Level : 3 - // Path : a/kk/t + // Name : t + // Branch : └── + // Row : └── t + // Level : 3 + // Path : a/kk/t + // HasChild : false // WalkerNode's methods called... - // Name : e - // Branch : - // Row : e - // Level : 1 - // Path : e + // Name : e + // Branch : + // Row : e + // Level : 1 + // Path : e + // HasChild : true // WalkerNode's methods called... - // Name : o - // Branch : └── - // Row : └── o - // Level : 2 - // Path : e/o + // Name : o + // Branch : └── + // Row : └── o + // Level : 2 + // Path : e/o + // HasChild : true // WalkerNode's methods called... - // Name : g - // Branch : └── - // Row : └── g - // Level : 3 - // Path : e/o/g + // Name : g + // Branch : └── + // Row : └── g + // Level : 3 + // Path : e/o/g + // HasChild : false fmt.Println("\nWalker Sample...end") fmt.Println() diff --git a/example/programmable/main.go b/example/programmable/main.go index 25c8c54..366f444 100644 --- a/example/programmable/main.go +++ b/example/programmable/main.go @@ -495,11 +495,12 @@ func sampleWalker() { callback2 := func(wn *gtree.WalkerNode) error { fmt.Println("WalkerNode's methods called...") - fmt.Printf("\tName : %s\n", wn.Name()) - fmt.Printf("\tBranch : %s\n", wn.Branch()) - fmt.Printf("\tRow : %s\n", wn.Row()) - fmt.Printf("\tLevel : %d\n", wn.Level()) - fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tName : %s\n", wn.Name()) + fmt.Printf("\tBranch : %s\n", wn.Branch()) + fmt.Printf("\tRow : %s\n", wn.Row()) + fmt.Printf("\tLevel : %d\n", wn.Level()) + fmt.Printf("\tPath : %s\n", wn.Path()) + fmt.Printf("\tHasChild : %t\n", wn.HasChild()) return nil } @@ -509,41 +510,47 @@ func sampleWalker() { } // Output: // WalkerNode's methods called... - // Name : root - // Branch : - // Row : root - // Level : 1 - // Path : root + // Name : root + // Branch : + // Row : root + // Level : 1 + // Path : root + // HasChild : true // WalkerNode's methods called... - // Name : child 1 - // Branch : ├── - // Row : ├── child 1 - // Level : 2 - // Path : root/child 1 + // Name : child 1 + // Branch : ├── + // Row : ├── child 1 + // Level : 2 + // Path : root/child 1 + // HasChild : true // WalkerNode's methods called... - // Name : child 2 - // Branch : │ └── - // Row : │ └── child 2 - // Level : 3 - // Path : root/child 1/child 2 + // Name : child 2 + // Branch : │ └── + // Row : │ └── child 2 + // Level : 3 + // Path : root/child 1/child 2 + // HasChild : true // WalkerNode's methods called... - // Name : child 3 - // Branch : │ ├── - // Row : │ ├── child 3 - // Level : 4 - // Path : root/child 1/child 2/child 3 + // Name : child 3 + // Branch : │ ├── + // Row : │ ├── child 3 + // Level : 4 + // Path : root/child 1/child 2/child 3 + // HasChild : false // WalkerNode's methods called... - // Name : child 4 - // Branch : │ └── - // Row : │ └── child 4 - // Level : 4 - // Path : root/child 1/child 2/child 4 + // Name : child 4 + // Branch : │ └── + // Row : │ └── child 4 + // Level : 4 + // Path : root/child 1/child 2/child 4 + // HasChild : false // WalkerNode's methods called... - // Name : child 5 - // Branch : └── - // Row : └── child 5 - // Level : 2 - // Path : root/child 5 + // Name : child 5 + // Branch : └── + // Row : └── child 5 + // Level : 2 + // Path : root/child 5 + // HasChild : false fmt.Println("\nWalker Sample...end") fmt.Println() diff --git a/simple_tree_walker.go b/simple_tree_walker.go index 3860ac1..2b8e8a3 100644 --- a/simple_tree_walker.go +++ b/simple_tree_walker.go @@ -4,11 +4,12 @@ package gtree // WalkerNode is used in user-defined function that can be executed with Walk/WalkProgrammably function. type WalkerNode struct { - name string - branch string - row string - level uint - path string + name string + branch string + row string + level uint + path string + hasChild bool } // Name returns name of node in completed tree structure. @@ -32,10 +33,16 @@ func (wn *WalkerNode) Level() uint { } // Path returns path of node in completed tree structure. +// Path is the path from the root node to this node. func (wn *WalkerNode) Path() string { return wn.path } +// HasChild returns whether the node in completed tree structure has child nodes. +func (wn *WalkerNode) HasChild() bool { + return wn.hasChild +} + func newWalkerSimple() walkerSimple { return &defaultWalkerSimple{} } @@ -57,11 +64,12 @@ func (dw *defaultWalkerSimple) walkNode(current *Node, cb func(*WalkerNode) erro row = current.branch() + " " + current.name } wn := &WalkerNode{ - name: current.name, - branch: current.branch(), - row: row, - level: current.hierarchy, - path: current.path(), + name: current.name, + branch: current.branch(), + row: row, + level: current.hierarchy, + path: current.path(), + hasChild: current.hasChild(), } if err := cb(wn); err != nil { return err diff --git a/tree_handler.go b/tree_handler.go index fe7540a..68a0517 100644 --- a/tree_handler.go +++ b/tree_handler.go @@ -24,7 +24,7 @@ func Verify(r io.Reader, options ...Option) error { return initializeTree(cfg).verify(r, cfg) } -// Walk executes user-defined function while traversing tree structure. +// Walk executes user-defined function while traversing tree structure recursively. func Walk(r io.Reader, cb func(*WalkerNode) error, options ...Option) error { cfg := newConfig(options) return initializeTree(cfg).walk(r, cb, cfg) diff --git a/tree_handler_programmably.go b/tree_handler_programmably.go index 057d479..3e33d5c 100644 --- a/tree_handler_programmably.go +++ b/tree_handler_programmably.go @@ -50,7 +50,7 @@ func VerifyProgrammably(root *Node, options ...Option) error { return initializeTree(cfg).verifyProgrammably(root, cfg) } -// WalkProgrammably executes user-defined function while traversing tree structure. +// WalkProgrammably executes user-defined function while traversing tree structure recursively. // This function requires node generated by NewRoot function. func WalkProgrammably(root *Node, cb func(*WalkerNode) error, options ...Option) error { if err := validateTreeRoot(root); err != nil {