fullpath.go 627 B

123456789101112131415161718192021222324252627282930313233343536
  1. package filer2
  2. import (
  3. "path/filepath"
  4. "strings"
  5. )
  6. type FullPath string
  7. func NewFullPath(dir, name string) FullPath {
  8. return FullPath(dir).Child(name)
  9. }
  10. func (fp FullPath) DirAndName() (string, string) {
  11. dir, name := filepath.Split(string(fp))
  12. if dir == "/" {
  13. return dir, name
  14. }
  15. if len(dir) < 1 {
  16. return "/", ""
  17. }
  18. return dir[:len(dir)-1], name
  19. }
  20. func (fp FullPath) Name() string {
  21. _, name := filepath.Split(string(fp))
  22. return name
  23. }
  24. func (fp FullPath) Child(name string) FullPath {
  25. dir := string(fp)
  26. if strings.HasSuffix(dir, "/") {
  27. return FullPath(dir + name)
  28. }
  29. return FullPath(dir + "/" + name)
  30. }