versioningconfiguration.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "sort"
  11. "github.com/syncthing/syncthing/lib/structutil"
  12. )
  13. // VersioningConfiguration is used in the code and for JSON serialization
  14. type VersioningConfiguration struct {
  15. Type string `json:"type" xml:"type,attr"`
  16. Params map[string]string `json:"params" xml:"parameter" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
  17. CleanupIntervalS int `json:"cleanupIntervalS" xml:"cleanupIntervalS" default:"3600"`
  18. FSPath string `json:"fsPath" xml:"fsPath"`
  19. FSType FilesystemType `json:"fsType" xml:"fsType"`
  20. }
  21. func (c *VersioningConfiguration) Reset() {
  22. *c = VersioningConfiguration{}
  23. }
  24. // internalVersioningConfiguration is used in XML serialization
  25. type internalVersioningConfiguration struct {
  26. Type string `xml:"type,attr,omitempty"`
  27. Params []internalParam `xml:"param"`
  28. CleanupIntervalS int `xml:"cleanupIntervalS" default:"3600"`
  29. FSPath string `xml:"fsPath"`
  30. FSType FilesystemType `xml:"fsType"`
  31. }
  32. type internalParam struct {
  33. Key string `xml:"key,attr"`
  34. Val string `xml:"val,attr"`
  35. }
  36. func (c VersioningConfiguration) Copy() VersioningConfiguration {
  37. cp := c
  38. cp.Params = make(map[string]string, len(c.Params))
  39. for k, v := range c.Params {
  40. cp.Params[k] = v
  41. }
  42. return cp
  43. }
  44. func (c *VersioningConfiguration) UnmarshalJSON(data []byte) error {
  45. structutil.SetDefaults(c)
  46. type noCustomUnmarshal VersioningConfiguration
  47. ptr := (*noCustomUnmarshal)(c)
  48. return json.Unmarshal(data, ptr)
  49. }
  50. func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  51. var intCfg internalVersioningConfiguration
  52. structutil.SetDefaults(&intCfg)
  53. if err := d.DecodeElement(&intCfg, &start); err != nil {
  54. return err
  55. }
  56. c.fromInternal(intCfg)
  57. return nil
  58. }
  59. func (c VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  60. // Using EncodeElement instead of plain Encode ensures that we use the
  61. // outer tag name from the VersioningConfiguration (i.e.,
  62. // `<versioning>`) rather than whatever the internal representation
  63. // would otherwise be.
  64. return e.EncodeElement(c.toInternal(), start)
  65. }
  66. func (c *VersioningConfiguration) toInternal() internalVersioningConfiguration {
  67. var tmp internalVersioningConfiguration
  68. tmp.Type = c.Type
  69. tmp.CleanupIntervalS = c.CleanupIntervalS
  70. tmp.FSPath = c.FSPath
  71. tmp.FSType = c.FSType
  72. for k, v := range c.Params {
  73. tmp.Params = append(tmp.Params, internalParam{k, v})
  74. }
  75. sort.Slice(tmp.Params, func(a, b int) bool {
  76. return tmp.Params[a].Key < tmp.Params[b].Key
  77. })
  78. return tmp
  79. }
  80. func (c *VersioningConfiguration) fromInternal(intCfg internalVersioningConfiguration) {
  81. c.Type = intCfg.Type
  82. c.CleanupIntervalS = intCfg.CleanupIntervalS
  83. c.FSPath = intCfg.FSPath
  84. c.FSType = intCfg.FSType
  85. c.Params = make(map[string]string, len(intCfg.Params))
  86. for _, p := range intCfg.Params {
  87. c.Params[p.Key] = p.Val
  88. }
  89. }