-
Notifications
You must be signed in to change notification settings - Fork 55
/
driver.go
162 lines (133 loc) · 3.76 KB
/
driver.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package config
import (
"encoding/json"
"github.com/gookit/goutil/jsonutil"
)
// Driver interface.
// TODO refactor: rename GetDecoder() to Decode(), rename GetEncoder() to Encode()
type Driver interface {
Name() string
Aliases() []string // alias format names, use for resolve format name
GetDecoder() Decoder
GetEncoder() Encoder
}
// DriverV2 interface.
type DriverV2 interface {
Name() string // driver name, also is format name.
Aliases() []string // alias format names, use for resolve format name
Decode(blob []byte, v any) (err error)
Encode(v any) (out []byte, err error)
}
// Decoder for decode yml,json,toml format content
type Decoder func(blob []byte, v any) (err error)
// Encoder for decode yml,json,toml format content
type Encoder func(v any) (out []byte, err error)
// StdDriver struct
type StdDriver struct {
name string
aliases []string
decoder Decoder
encoder Encoder
}
// NewDriver new std driver instance.
func NewDriver(name string, dec Decoder, enc Encoder) *StdDriver {
return &StdDriver{name: name, decoder: dec, encoder: enc}
}
// WithAliases set aliases for driver
func (d *StdDriver) WithAliases(aliases ...string) *StdDriver {
d.aliases = aliases
return d
}
// WithAlias add alias for driver
func (d *StdDriver) WithAlias(alias string) *StdDriver {
d.aliases = append(d.aliases, alias)
return d
}
// Name of driver
func (d *StdDriver) Name() string { return d.name }
// Aliases format name of driver
func (d *StdDriver) Aliases() []string {
return d.aliases
}
// Decode of driver
func (d *StdDriver) Decode(blob []byte, v any) (err error) {
return d.decoder(blob, v)
}
// Encode of driver
func (d *StdDriver) Encode(v any) ([]byte, error) {
return d.encoder(v)
}
// GetDecoder of driver
func (d *StdDriver) GetDecoder() Decoder {
return d.decoder
}
// GetEncoder of driver
func (d *StdDriver) GetEncoder() Encoder {
return d.encoder
}
/*************************************************************
* JSON driver
*************************************************************/
var (
// JSONAllowComments support write comments on json file.
JSONAllowComments = true
// JSONMarshalIndent if not empty, will use json.MarshalIndent for encode data.
//
// Deprecated: please use JSONDriver.MarshalIndent
JSONMarshalIndent string
)
// JSONDecoder for json decode
var JSONDecoder Decoder = func(data []byte, v any) (err error) {
JSONDriver.ClearComments = JSONAllowComments
return JSONDriver.Decode(data, v)
}
// JSONEncoder for json encode
var JSONEncoder Encoder = func(v any) (out []byte, err error) {
JSONDriver.MarshalIndent = JSONMarshalIndent
return JSONDriver.Encode(v)
}
// JSONDriver instance fot json
var JSONDriver = &jsonDriver{
driverName: JSON,
ClearComments: JSONAllowComments,
MarshalIndent: JSONMarshalIndent,
}
// jsonDriver for json format content
type jsonDriver struct {
driverName string
// ClearComments before parse JSON string.
ClearComments bool
// MarshalIndent if not empty, will use json.MarshalIndent for encode data.
MarshalIndent string
}
// Name of the driver
func (d *jsonDriver) Name() string {
return d.driverName
}
// Aliases of the driver
func (d *jsonDriver) Aliases() []string {
return nil
}
// Decode for the driver
func (d *jsonDriver) Decode(data []byte, v any) error {
if d.ClearComments {
str := jsonutil.StripComments(string(data))
return json.Unmarshal([]byte(str), v)
}
return json.Unmarshal(data, v)
}
// GetDecoder for the driver
func (d *jsonDriver) GetDecoder() Decoder {
return d.Decode
}
// Encode for the driver
func (d *jsonDriver) Encode(v any) (out []byte, err error) {
if len(d.MarshalIndent) > 0 {
return json.MarshalIndent(v, "", d.MarshalIndent)
}
return json.Marshal(v)
}
// GetEncoder for the driver
func (d *jsonDriver) GetEncoder() Encoder {
return d.Encode
}