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.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package types
  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) Pop() string {
  29. if len(p.comps) == 0 {
  30. return ""
  31. }
  32. comp := p.comps[len(p.comps)-1]
  33. p.comps = p.comps[:len(p.comps)-1]
  34. return comp
  35. }
  36. func (p *JPath) SplitParent() JPath {
  37. if len(p.comps) <= 1 {
  38. return JPath{}
  39. }
  40. parent := JPath{
  41. comps: make([]string, len(p.comps)-1),
  42. }
  43. copy(parent.comps, p.comps[:len(p.comps)-1])
  44. p.comps = p.comps[len(p.comps)-1:]
  45. return parent
  46. }
  47. func (p *JPath) DropFrontN(cnt int) {
  48. if cnt >= len(p.comps) {
  49. p.comps = nil
  50. return
  51. }
  52. if cnt <= 0 {
  53. return
  54. }
  55. p.comps = p.comps[cnt:]
  56. }
  57. func (p *JPath) Concat(other JPath) {
  58. p.comps = append(p.comps, other.comps...)
  59. }
  60. func (p *JPath) ConcatNew(other JPath) JPath {
  61. clone := p.Clone()
  62. clone.Concat(other)
  63. return clone
  64. }
  65. func (p *JPath) ConcatComps(comps []string) {
  66. p.comps = append(p.comps, comps...)
  67. }
  68. func (p *JPath) ConcatCompsNew(comps ...string) JPath {
  69. clone := p.Clone()
  70. clone.ConcatComps(comps)
  71. return clone
  72. }
  73. func (p *JPath) Clone() JPath {
  74. clone := JPath{
  75. comps: make([]string, len(p.comps)),
  76. }
  77. copy(clone.comps, p.comps)
  78. return clone
  79. }
  80. func (p *JPath) JoinOSPath() string {
  81. return filepath.Join(p.comps...)
  82. }
  83. func (p JPath) String() string {
  84. return strings.Join(p.comps, ObjectPathSeparator)
  85. }
  86. func (p JPath) ToString() (string, error) {
  87. return p.String(), nil
  88. }
  89. func (p JPath) FromString(s string) (any, error) {
  90. p2 := PathFromJcsPathString(s)
  91. return p2, nil
  92. }
  93. func (p JPath) MarshalJSON() ([]byte, error) {
  94. return []byte(`"` + p.String() + `"`), nil
  95. }
  96. func (p *JPath) UnmarshalJSON(data []byte) error {
  97. s := string(data)
  98. s = strings.Trim(s, `"`)
  99. p2 := PathFromJcsPathString(s)
  100. *p = p2
  101. return nil
  102. }
  103. func PathFromComps(comps ...string) JPath {
  104. c2 := make([]string, len(comps))
  105. copy(c2, comps)
  106. return JPath{
  107. comps: c2,
  108. }
  109. }
  110. func PathFromOSPathString(s string) JPath {
  111. cleaned := filepath.Clean(s)
  112. comps := strings.Split(cleaned, string(filepath.Separator))
  113. return JPath{
  114. comps: lo.Reject(comps, func(s string, idx int) bool { return s == "" }),
  115. }
  116. }
  117. func PathFromJcsPathString(s string) JPath {
  118. comps := strings.Split(s, ObjectPathSeparator)
  119. i := 0
  120. for ; i < len(comps) && len(comps[i]) == 0; i++ {
  121. }
  122. comps = comps[i:]
  123. return JPath{
  124. comps: comps,
  125. }
  126. }

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