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.

string_lock_target.go 2.2 kB

6 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package lockprovider
  2. import (
  3. "fmt"
  4. "github.com/samber/lo"
  5. "gitlink.org.cn/cloudream/common/utils/serder"
  6. "gitlink.org.cn/cloudream/jcs-pub/client/internal/publock/types"
  7. )
  8. type StringLockTarget struct {
  9. Components []StringLockTargetComponet `json:"components"`
  10. }
  11. func NewStringLockTarget() *StringLockTarget {
  12. return &StringLockTarget{}
  13. }
  14. // Add 添加一个Component,并将其内容设置为compValues
  15. func (t *StringLockTarget) Add(compValues ...any) *StringLockTarget {
  16. t.Components = append(t.Components, StringLockTargetComponet{
  17. Values: lo.Map(compValues, func(val any, index int) string { return fmt.Sprintf("%v", val) }),
  18. })
  19. return t
  20. }
  21. // IsConflict 判断两个锁对象是否冲突。注:只有相同的结构的Target才有意义
  22. func (t *StringLockTarget) IsConflict(other *StringLockTarget) bool {
  23. if len(t.Components) != len(other.Components) {
  24. return false
  25. }
  26. if len(t.Components) == 0 {
  27. return true
  28. }
  29. for i := 0; i < len(t.Components); i++ {
  30. if t.Components[i].IsEquals(&other.Components[i]) {
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. func (t *StringLockTarget) Equals(other types.LockTarget) bool {
  37. st, ok := other.(*StringLockTarget)
  38. if !ok {
  39. return false
  40. }
  41. if len(t.Components) != len(st.Components) {
  42. return false
  43. }
  44. for i := 0; i < len(t.Components); i++ {
  45. if !t.Components[i].IsEquals(&st.Components[i]) {
  46. return false
  47. }
  48. }
  49. return true
  50. }
  51. type StringLockTargetComponet struct {
  52. Values []string `json:"values"`
  53. }
  54. // IsEquals 判断两个Component是否相同。注:只有相同的结构的Component才有意义
  55. func (t *StringLockTargetComponet) IsEquals(other *StringLockTargetComponet) bool {
  56. if len(t.Values) != len(other.Values) {
  57. return false
  58. }
  59. for i := 0; i < len(t.Values); i++ {
  60. if t.Values[i] != other.Values[i] {
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. func StringLockTargetToString(target *StringLockTarget) (string, error) {
  67. data, err := serder.ObjectToJSON(target)
  68. if err != nil {
  69. return "", err
  70. }
  71. return string(data), nil
  72. }
  73. func StringLockTargetFromString(str string) (StringLockTarget, error) {
  74. var ret StringLockTarget
  75. err := serder.JSONToObject([]byte(str), &ret)
  76. return ret, err
  77. }

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