versioningconfiguration.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 "encoding/xml"
  8. type VersioningConfiguration struct {
  9. Type string `xml:"type,attr" json:"type"`
  10. Params map[string]string `json:"params"`
  11. }
  12. type InternalVersioningConfiguration struct {
  13. Type string `xml:"type,attr,omitempty"`
  14. Params []InternalParam `xml:"param"`
  15. }
  16. type InternalParam struct {
  17. Key string `xml:"key,attr"`
  18. Val string `xml:"val,attr"`
  19. }
  20. func (c VersioningConfiguration) Copy() VersioningConfiguration {
  21. cp := c
  22. cp.Params = make(map[string]string, len(c.Params))
  23. for k, v := range c.Params {
  24. cp.Params[k] = v
  25. }
  26. return cp
  27. }
  28. func (c *VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  29. var tmp InternalVersioningConfiguration
  30. tmp.Type = c.Type
  31. for k, v := range c.Params {
  32. tmp.Params = append(tmp.Params, InternalParam{k, v})
  33. }
  34. return e.EncodeElement(tmp, start)
  35. }
  36. func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  37. var tmp InternalVersioningConfiguration
  38. err := d.DecodeElement(&tmp, &start)
  39. if err != nil {
  40. return err
  41. }
  42. c.Type = tmp.Type
  43. c.Params = make(map[string]string, len(tmp.Params))
  44. for _, p := range tmp.Params {
  45. c.Params[p.Key] = p.Val
  46. }
  47. return nil
  48. }