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