123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package mountree
- import (
- libpath "path"
- "strings"
- )
- type SimpleUnixLikePathTree[T PayloadType] struct {
- baseTree *Tree[T]
- }
- func NewSimpleUnixLikePathTree[T PayloadType]() *SimpleUnixLikePathTree[T] {
- return &SimpleUnixLikePathTree[T]{
- baseTree: NewTree[T](),
- }
- }
- func (t *SimpleUnixLikePathTree[T]) splitPath(path string) []string {
- if path == "" {
- return []string{}
- }
- sp := strings.Split(libpath.Clean(path), "/")
- filtered := make([]string, 0, len(sp))
- for _, str := range sp {
- if str != "" {
- filtered = append(filtered, str)
- }
- }
- return filtered
- }
- func (t *SimpleUnixLikePathTree[T]) Mount(path string, payload T, forceRemount bool) error {
- pseq := t.splitPath(path)
- return t.baseTree.Mount(pseq, payload, forceRemount)
- }
- func (t *SimpleUnixLikePathTree[T]) Umount(path string) error {
- pseq := t.splitPath(path)
- return t.baseTree.Umount(pseq)
- }
- func (t *SimpleUnixLikePathTree[T]) GetPayload(path string) (*T, string, error) {
- pseq := t.splitPath(path)
- payload, rpseq, err := t.baseTree.GetPayload(pseq)
- if err != nil {
- return nil, path, err
- }
- rp := strings.Join(rpseq, "/")
- return payload, rp, nil
- }
- func (t *SimpleUnixLikePathTree[T]) ListAllMount(prt func(path string, payload T)) {
- t.baseTree.ListAllMount(prt, "/")
- }
|