Skip to content

Commit

Permalink
fix build error for built in json driver
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 22, 2022
1 parent 167f619 commit b71d046
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 6 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ func TestJSONAllowComments(t *testing.T) {
`), &m)
is.Error(err)

JSONAllowComments = true
err = JSONDecoder([]byte(`{
// comments
"n":"v"}
`), &m)
is.NoError(err)
JSONAllowComments = old
}

Expand Down
17 changes: 8 additions & 9 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ func (d *StdDriver) GetEncoder() Encoder {

var (
// JSONAllowComments support write comments on json file.
//
// Deprecated: please use JSONDriver.ClearComments = true
JSONAllowComments = true

// JSONMarshalIndent if not empty, will use json.MarshalIndent for encode data.
//
// Deprecated: please use JSONDriver.MarshalIndent
JSONMarshalIndent string
)

Expand All @@ -84,19 +88,14 @@ var JSONEncoder Encoder = func(v interface{}) (out []byte, err error) {

// JSONDriver instance fot json
var JSONDriver = &jsonDriver{
driverName: JSON,
ClearComments: JSONAllowComments,
MarshalIndent: JSONMarshalIndent,
// inject
StdDriver: StdDriver{
name: JSON,
decoder: JSONDecoder,
encoder: JSONEncoder,
},
}

// jsonDriver for json format content
type jsonDriver struct {
StdDriver
driverName string
// ClearComments before parse JSON string.
ClearComments bool
// MarshalIndent if not empty, will use json.MarshalIndent for encode data.
Expand All @@ -105,7 +104,7 @@ type jsonDriver struct {

// Name of the driver
func (d *jsonDriver) Name() string {
return d.name
return d.driverName
}

// Decode for the driver
Expand All @@ -125,7 +124,7 @@ func (d *jsonDriver) GetDecoder() Decoder {
// Encode for the driver
func (d *jsonDriver) Encode(v interface{}) (out []byte, err error) {
if len(d.MarshalIndent) > 0 {
return json.MarshalIndent(v, "", JSONMarshalIndent)
return json.MarshalIndent(v, "", d.MarshalIndent)
}
return json.Marshal(v)
}
Expand Down

0 comments on commit b71d046

Please sign in to comment.