versioningconfiguration.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "encoding/json"
  9. "encoding/xml"
  10. "slices"
  11. "strings"
  12. "github.com/syncthing/syncthing/lib/structutil"
  13. )
  14. // VersioningConfiguration is used in the code and for JSON serialization
  15. type VersioningConfiguration struct {
  16. Type string `json:"type" xml:"type,attr"`
  17. Params map[string]string `json:"params" xml:"parameter" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
  18. CleanupIntervalS int `json:"cleanupIntervalS" xml:"cleanupIntervalS" default:"3600"`
  19. FSPath string `json:"fsPath" xml:"fsPath"`
  20. FSType FilesystemType `json:"fsType" xml:"fsType" default:"basic"`
  21. }
  22. func (c *VersioningConfiguration) Reset() {
  23. *c = VersioningConfiguration{}
  24. }
  25. // internalVersioningConfiguration is used in XML serialization
  26. type internalVersioningConfiguration struct {
  27. Type string `xml:"type,attr,omitempty"`
  28. Params []internalParam `xml:"param"`
  29. CleanupIntervalS int `xml:"cleanupIntervalS" default:"3600"`
  30. FSPath string `xml:"fsPath"`
  31. FSType FilesystemType `xml:"fsType" default:"basic"`
  32. }
  33. type internalParam struct {
  34. Key string `xml:"key,attr"`
  35. Val string `xml:"val,attr"`
  36. }
  37. func (c VersioningConfiguration) Copy() VersioningConfiguration {
  38. cp := c
  39. cp.Params = make(map[string]string, len(c.Params))
  40. for k, v := range c.Params {
  41. cp.Params[k] = v
  42. }
  43. return cp
  44. }
  45. func (c *VersioningConfiguration) UnmarshalJSON(data []byte) error {
  46. structutil.SetDefaults(c)
  47. type noCustomUnmarshal VersioningConfiguration
  48. ptr := (*noCustomUnmarshal)(c)
  49. return json.Unmarshal(data, ptr)
  50. }
  51. func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  52. var intCfg internalVersioningConfiguration
  53. structutil.SetDefaults(&intCfg)
  54. if err := d.DecodeElement(&intCfg, &start); err != nil {
  55. return err
  56. }
  57. c.fromInternal(intCfg)
  58. return nil
  59. }
  60. func (c VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  61. // Using EncodeElement instead of plain Encode ensures that we use the
  62. // outer tag name from the VersioningConfiguration (i.e.,
  63. // `<versioning>`) rather than whatever the internal representation
  64. // would otherwise be.
  65. return e.EncodeElement(c.toInternal(), start)
  66. }
  67. func (c *VersioningConfiguration) toInternal() internalVersioningConfiguration {
  68. var tmp internalVersioningConfiguration
  69. tmp.Type = c.Type
  70. tmp.CleanupIntervalS = c.CleanupIntervalS
  71. tmp.FSPath = c.FSPath
  72. tmp.FSType = c.FSType
  73. for k, v := range c.Params {
  74. tmp.Params = append(tmp.Params, internalParam{k, v})
  75. }
  76. slices.SortFunc(tmp.Params, func(a, b internalParam) int {
  77. return strings.Compare(a.Key, b.Key)
  78. })
  79. return tmp
  80. }
  81. func (c *VersioningConfiguration) fromInternal(intCfg internalVersioningConfiguration) {
  82. c.Type = intCfg.Type
  83. c.CleanupIntervalS = intCfg.CleanupIntervalS
  84. c.FSPath = intCfg.FSPath
  85. c.FSType = intCfg.FSType
  86. c.Params = make(map[string]string, len(intCfg.Params))
  87. for _, p := range intCfg.Params {
  88. c.Params[p.Key] = p.Val
  89. }
  90. }