You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

path.go 2.8 kB

3 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package jcstypes
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "github.com/samber/lo"
  6. )
  7. type JPath struct {
  8. comps []string
  9. }
  10. func (p *JPath) Len() int {
  11. return len(p.comps)
  12. }
  13. func (p *JPath) Comp(idx int) string {
  14. return p.comps[idx]
  15. }
  16. func (p *JPath) Comps() []string {
  17. return p.comps
  18. }
  19. func (p *JPath) LastComp() string {
  20. if len(p.comps) == 0 {
  21. return ""
  22. }
  23. return p.comps[len(p.comps)-1]
  24. }
  25. func (p *JPath) Push(comp ...string) {
  26. p.comps = append(p.comps, comp...)
  27. }
  28. func (p *JPath) PushNew(comp ...string) JPath {
  29. clone := p.Clone()
  30. clone.Push(comp...)
  31. return clone
  32. }
  33. func (p *JPath) Pop() string {
  34. if len(p.comps) == 0 {
  35. return ""
  36. }
  37. comp := p.comps[len(p.comps)-1]
  38. p.comps = p.comps[:len(p.comps)-1]
  39. return comp
  40. }
  41. func (p *JPath) PopFrontN(cnt int) {
  42. if cnt >= len(p.comps) {
  43. p.comps = nil
  44. return
  45. }
  46. if cnt <= 0 {
  47. return
  48. }
  49. p.comps = p.comps[cnt:]
  50. }
  51. func (p *JPath) CutParent() JPath {
  52. if len(p.comps) <= 1 {
  53. return JPath{}
  54. }
  55. parent := JPath{
  56. comps: make([]string, len(p.comps)-1),
  57. }
  58. copy(parent.comps, p.comps[:len(p.comps)-1])
  59. p.comps = p.comps[len(p.comps)-1:]
  60. return parent
  61. }
  62. func (p *JPath) CopyParent() JPath {
  63. if len(p.comps) <= 1 {
  64. return JPath{}
  65. }
  66. parent := JPath{
  67. comps: make([]string, len(p.comps)-1),
  68. }
  69. copy(parent.comps, p.comps[:len(p.comps)-1])
  70. return parent
  71. }
  72. func (p *JPath) Concat(other JPath) {
  73. p.comps = append(p.comps, other.comps...)
  74. }
  75. func (p *JPath) ConcatNew(other JPath) JPath {
  76. clone := p.Clone()
  77. clone.Concat(other)
  78. return clone
  79. }
  80. func (p *JPath) Clone() JPath {
  81. clone := JPath{
  82. comps: make([]string, len(p.comps)),
  83. }
  84. copy(clone.comps, p.comps)
  85. return clone
  86. }
  87. func (p *JPath) OSString() string {
  88. return filepath.Join(p.comps...)
  89. }
  90. func (p JPath) String() string {
  91. return strings.Join(p.comps, ObjectPathSeparator)
  92. }
  93. func (p JPath) ToString() (string, error) {
  94. return p.String(), nil
  95. }
  96. func (p JPath) FromString(s string) (any, error) {
  97. p2 := PathFromJcsPathString(s)
  98. return p2, nil
  99. }
  100. func (p JPath) MarshalJSON() ([]byte, error) {
  101. return []byte(`"` + p.String() + `"`), nil
  102. }
  103. func (p *JPath) UnmarshalJSON(data []byte) error {
  104. s := string(data)
  105. s = strings.Trim(s, `"`)
  106. p2 := PathFromJcsPathString(s)
  107. *p = p2
  108. return nil
  109. }
  110. func PathFromComps(comps ...string) JPath {
  111. c2 := make([]string, len(comps))
  112. copy(c2, comps)
  113. return JPath{
  114. comps: c2,
  115. }
  116. }
  117. func PathFromOSPathString(s string) JPath {
  118. cleaned := filepath.Clean(s)
  119. comps := strings.Split(cleaned, string(filepath.Separator))
  120. return JPath{
  121. comps: lo.Reject(comps, func(s string, idx int) bool { return s == "" }),
  122. }
  123. }
  124. func PathFromJcsPathString(s string) JPath {
  125. comps := strings.Split(s, ObjectPathSeparator)
  126. i := 0
  127. for ; i < len(comps) && len(comps[i]) == 0; i++ {
  128. }
  129. comps = comps[i:]
  130. return JPath{
  131. comps: comps,
  132. }
  133. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。