FindPath finds a node with the specified path starting with this node and searching in all its children recursively. A path is the sequence of the names from the first node to the desired node separated by the forward slash.
(path string)
| 246 | // A path is the sequence of the names from the first node to the desired node |
| 247 | // separated by the forward slash. |
| 248 | func (n *Node) FindPath(path string) INode { |
| 249 | |
| 250 | // Internal recursive function to find node |
| 251 | var finder func(inode INode, path string) INode |
| 252 | finder = func(inode INode, path string) INode { |
| 253 | // Get first component of the path |
| 254 | parts := strings.Split(path, "/") |
| 255 | if len(parts) == 0 { |
| 256 | return nil |
| 257 | } |
| 258 | first := parts[0] |
| 259 | // Checks current node |
| 260 | node := inode.GetNode() |
| 261 | if node.name != first { |
| 262 | return nil |
| 263 | } |
| 264 | // If the path has finished this is the desired node |
| 265 | rest := strings.Join(parts[1:], "/") |
| 266 | if rest == "" { |
| 267 | return inode |
| 268 | } |
| 269 | // Otherwise search in this node children |
| 270 | for _, ichild := range node.children { |
| 271 | found := finder(ichild, rest) |
| 272 | if found != nil { |
| 273 | return found |
| 274 | } |
| 275 | } |
| 276 | return nil |
| 277 | } |
| 278 | return finder(n, path) |
| 279 | } |
| 280 | |
| 281 | // FindLoaderID looks in the specified node and in all its children |
| 282 | // for a node with the specified loaderID and if found returns it. |