/
parser.go
97 lines (66 loc) · 2.39 KB
/
parser.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sdk
import (
"bytes"
"encoding/xml"
"io"
toml "github.com/BurntSushi/toml"
json "github.com/json-iterator/go"
yaml "gopkg.in/yaml.v3"
)
type Decoder interface{ Decode(v any) error }
type Encoder interface{ Encode(v any) error }
type Parser interface {
Marshal(v any) (p []byte, err error)
Unmarshal(p []byte, v any) (err error)
NewEncoder(w io.Writer) Encoder
NewDecoder(r io.Reader) Decoder
}
//nolint:gochecknoglobals
var (
// JSON ...
JSON Parser = _json{json.ConfigFastest}
// XML ...
XML Parser = _xml{}
// YAML ...
YAML Parser = _yaml{}
// TOML ...
TOML Parser = _toml{}
)
// =============================================================================
// _json implementation.
type _json struct{ json.API }
func (json _json) NewDecoder(r io.Reader) Decoder { return json.API.NewDecoder(r) }
func (json _json) NewEncoder(w io.Writer) Encoder { return json.API.NewEncoder(w) }
// =============================================================================
type XMLName = xml.Name
// _xml implementation.
type _xml struct{}
func (_xml) Marshal(v any) ([]byte, error) { return xml.Marshal(v) }
func (_xml) Unmarshal(data []byte, v any) error { return xml.Unmarshal(data, v) }
func (_xml) NewEncoder(w io.Writer) Encoder { return xml.NewEncoder(w) }
func (_xml) NewDecoder(r io.Reader) Decoder { return xml.NewDecoder(r) }
// =============================================================================
// _yaml implementation.
type _yaml struct{}
func (_yaml) Marshal(v any) ([]byte, error) { return yaml.Marshal(v) }
func (_yaml) Unmarshal(data []byte, v any) error { return yaml.Unmarshal(data, v) }
func (_yaml) NewEncoder(w io.Writer) Encoder { return yaml.NewEncoder(w) }
func (_yaml) NewDecoder(r io.Reader) Decoder { return yaml.NewDecoder(r) }
// =============================================================================
// _toml implementation.
type _toml struct{ r io.Reader }
func (_toml) Marshal(v any) ([]byte, error) {
b := new(bytes.Buffer)
err := toml.NewEncoder(b).Encode(v)
return b.Bytes(), err
}
func (_toml) Unmarshal(data []byte, v any) error { return toml.Unmarshal(data, v) }
func (_toml) NewEncoder(w io.Writer) Encoder { return toml.NewEncoder(w) }
func (_toml) NewDecoder(r io.Reader) Decoder { return _toml{r} }
func (t _toml) Decode(v any) error {
if p, err := io.ReadAll(t.r); err != nil {
return err
} else {
return toml.Unmarshal(p, v)
}
}