/
xml.go
60 lines (48 loc) · 1.11 KB
/
xml.go
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
// Copyright (c) Pedersen authors.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package xml
import (
"encoding/xml"
iofs "io/fs"
"path/filepath"
perrors "github.com/matteoarella/pedersen/internal/errors"
"github.com/matteoarella/pedersen/internal/io"
"github.com/spf13/afero"
)
type xmlIO struct {
io.BaseIO
}
func New(fs afero.Fs) io.IO {
return xmlIO{
BaseIO: io.BaseIO{
Fs: fs,
},
}
}
func Ext() string {
return ".xml"
}
func (x xmlIO) Ext() string {
return Ext()
}
func (x xmlIO) ReadFile(name string, v interface{}) error {
fileData, err := x.BaseIO.ReadFile(name, Ext())
if err != nil {
return perrors.WrapErrorf(err, "[xmlIO.ReadFile]")
}
return xml.Unmarshal(fileData, v)
}
func (x xmlIO) WriteFile(name string, v interface{}, perm iofs.FileMode) error {
derData, err := xml.Marshal(v)
if err != nil {
return perrors.WrapErrorf(err, "[xmlIO.WriteFile]")
}
ext := filepath.Ext(name)
if len(ext) < 1 {
name += x.Ext()
}
return x.BaseIO.WriteFile(name, Ext(), derData, perm)
}