Skip to content

Commit 73c37c4

Browse files
authored
feat: replace go-multierror with Go 1.20 native joins (#156)
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
1 parent 83a3feb commit 73c37c4

8 files changed

+34
-44
lines changed

cached_parser.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package editorconfig
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"regexp"
78

8-
"github.com/hashicorp/go-multierror"
99
"gopkg.in/ini.v1"
1010
)
1111

@@ -36,7 +36,7 @@ func (parser *CachedParser) ParseIni(filename string) (*Editorconfig, error) {
3636

3737
// ParseIniGraceful parses the given filename to a Definition and caches the result.
3838
func (parser *CachedParser) ParseIniGraceful(filename string) (*Editorconfig, error, error) {
39-
var warning *multierror.Error
39+
var warning error
4040

4141
ec, ok := parser.editorconfigs[filename]
4242
if !ok {
@@ -60,13 +60,13 @@ func (parser *CachedParser) ParseIniGraceful(filename string) (*Editorconfig, er
6060
}
6161

6262
if warn != nil {
63-
warning = multierror.Append(warning, warn)
63+
warning = errors.Join(warning, warn)
6464
}
6565

6666
parser.editorconfigs[filename] = ec
6767
}
6868

69-
return ec, warning.ErrorOrNil(), nil //nolint:wrapcheck
69+
return ec, warning, nil
7070
}
7171

7272
// FnmatchCase calls the module's FnmatchCase and caches the parsed selector.

config.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"path/filepath"
88
"strings"
99

10-
"github.com/hashicorp/go-multierror"
1110
"golang.org/x/mod/semver"
1211
)
1312

@@ -27,10 +26,10 @@ type Config struct {
2726
func (config *Config) Load(filename string) (*Definition, error) {
2827
definition, warning, err := config.LoadGraceful(filename)
2928
if warning != nil {
30-
err = multierror.Append(err, warning)
29+
err = errors.Join(err, warning)
3130
}
3231

33-
return definition, err //nolint:wrapcheck
32+
return definition, err
3433
}
3534

3635
// Load loads definition of a given file with warnings and error.
@@ -66,15 +65,15 @@ func (config *Config) LoadGraceful(filename string) (*Definition, error, error)
6665
definition.version = version
6766
}
6867

69-
var warning *multierror.Error
68+
var warning error
7069

7170
dir := absFilename
7271
for dir != filepath.Dir(dir) {
7372
dir = filepath.Dir(dir)
7473

7574
ec, warn, err := config.Parser.ParseIniGraceful(filepath.Join(dir, ecFile))
7675
if warn != nil {
77-
warning = multierror.Append(warning, warn)
76+
warning = errors.Join(warning, warn)
7877
}
7978

8079
if err != nil {
@@ -105,5 +104,5 @@ func (config *Config) LoadGraceful(filename string) (*Definition, error, error)
105104
}
106105
}
107106

108-
return definition, warning.ErrorOrNil(), nil //nolint:wrapcheck
107+
return definition, warning, nil
109108
}

definition.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package editorconfig
22

33
import (
4+
"errors"
45
"fmt"
56
"strconv"
67
"strings"
78

8-
"github.com/hashicorp/go-multierror"
99
"golang.org/x/mod/semver"
1010
"gopkg.in/ini.v1"
1111
)
@@ -35,7 +35,7 @@ func NewDefinition(config Config) (*Definition, error) {
3535

3636
// normalize fixes some values to their lowercase value.
3737
func (d *Definition) normalize() error {
38-
var result *multierror.Error
38+
var result error
3939

4040
d.Charset = strings.ToLower(d.Charset)
4141
d.EndOfLine = strings.ToLower(d.Raw["end_of_line"])
@@ -45,7 +45,7 @@ func (d *Definition) normalize() error {
4545
if ok && trimTrailingWhitespace != UnsetValue {
4646
trim, err := strconv.ParseBool(trimTrailingWhitespace)
4747
if err != nil {
48-
result = multierror.Append(
48+
result = errors.Join(
4949
result,
5050
fmt.Errorf("trim_trailing_whitespace=%s is not an acceptable value. %w", trimTrailingWhitespace, err),
5151
)
@@ -58,7 +58,7 @@ func (d *Definition) normalize() error {
5858
if ok && insertFinalNewline != UnsetValue {
5959
insert, err := strconv.ParseBool(insertFinalNewline)
6060
if err != nil {
61-
result = multierror.Append(
61+
result = errors.Join(
6262
result,
6363
fmt.Errorf("insert_final_newline=%s is not an acceptable value. %w", insertFinalNewline, err),
6464
)
@@ -72,7 +72,7 @@ func (d *Definition) normalize() error {
7272
if ok && tabWidth != UnsetValue {
7373
num, err := strconv.Atoi(tabWidth)
7474
if err != nil {
75-
result = multierror.Append(result, fmt.Errorf("tab_width=%s is not an acceptable value. %w", tabWidth, err))
75+
result = errors.Join(result, fmt.Errorf("tab_width=%s is not an acceptable value. %w", tabWidth, err))
7676
} else {
7777
d.TabWidth = num
7878
}
@@ -85,7 +85,7 @@ func (d *Definition) normalize() error {
8585
d.TabWidth = num
8686
}
8787

88-
return result.ErrorOrNil() //nolint:wrapcheck
88+
return result
8989
}
9090

9191
// merge the parent definition into the child definition.

editorconfig.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package editorconfig
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
89
"runtime"
910
"strings"
1011

11-
"github.com/hashicorp/go-multierror"
1212
"gopkg.in/ini.v1"
1313
)
1414

@@ -90,13 +90,13 @@ func newEditorconfig(iniFile *ini.File) (*Editorconfig, error, error) {
9090

9191
if err := definition.normalize(); err != nil {
9292
// Append those error(s) into the warning
93-
warning = multierror.Append(warning, err)
93+
warning = errors.Join(warning, err)
9494
}
9595

9696
editorConfig.Definitions = append(editorConfig.Definitions, definition)
9797
}
9898

99-
return editorConfig, warning, nil //nolint:wrapcheck
99+
return editorConfig, warning, nil
100100
}
101101

102102
// GetDefinitionForFilename returns a definition for the given filename.
@@ -216,10 +216,10 @@ func Parse(r io.Reader) (*Editorconfig, error) {
216216

217217
ec, warning, err := newEditorconfig(iniFile)
218218
if warning != nil {
219-
err = multierror.Append(warning)
219+
err = errors.Join(warning, err)
220220
}
221221

222-
return ec, err //nolint:wrapcheck
222+
return ec, err
223223
}
224224

225225
// ParseGraceful parses from a reader with warnings not treated as a fatal error.
@@ -243,10 +243,10 @@ func ParseBytes(data []byte) (*Editorconfig, error) {
243243

244244
ec, warning, err := newEditorconfig(iniFile)
245245
if warning != nil {
246-
err = multierror.Append(warning)
246+
err = errors.Join(warning, err)
247247
}
248248

249-
return ec, err //nolint:wrapcheck
249+
return ec, err
250250
}
251251

252252
// ParseFile parses from a file.
@@ -260,10 +260,10 @@ func ParseFile(path string) (*Editorconfig, error) {
260260

261261
ec, warning, err := newEditorconfig(iniFile)
262262
if warning != nil {
263-
err = multierror.Append(warning)
263+
err = errors.Join(warning, err)
264264
}
265265

266-
return ec, err //nolint:wrapcheck
266+
return ec, err
267267
}
268268

269269
// GetDefinitionForFilename given a filename, searches for .editorconfig files,
@@ -281,7 +281,7 @@ func GetDefinitionForFilename(filename string) (*Definition, error) {
281281
// previous folders, until it reaches a folder with `root = true`, and returns
282282
// the right editorconfig definition for the given file.
283283
//
284-
// In case of non-fatal errors, a multierror warning is return as well.
284+
// In case of non-fatal errors, a joined errors warning is return as well.
285285
func GetDefinitionForFilenameGraceful(filename string) (*Definition, error, error) {
286286
config := new(Config)
287287

@@ -305,7 +305,7 @@ func GetDefinitionForFilenameWithConfigname(filename string, configname string)
305305
// walking through the previous folders, until it reaches a folder with `root =
306306
// true`, and returns the right editorconfig definition for the given file.
307307
//
308-
// In case of non-fatal errors, a multierror warning is return as well.
308+
// In case of non-fatal errors, a joined errors warning is return as well.
309309
func GetDefinitionForFilenameWithConfignameGraceful(filename string, configname string) (*Definition, error, error) {
310310
config := &Config{
311311
Name: configname,

editorconfig_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"errors"
55
"os"
66
"path/filepath"
7+
"strings"
78
"testing"
89
"testing/iotest"
910

10-
"github.com/hashicorp/go-multierror"
11-
1211
"github.com/editorconfig/editorconfig-core-go/v2/internal/assert"
1312
)
1413

@@ -167,10 +166,10 @@ func TestPublicTestDefinitionForFilenameWithConfigname(t *testing.T) {
167166

168167
def, warning, err := GetDefinitionForFilenameWithConfignameGraceful("testdata/root/src/dummy.go", "a.ini")
169168

170-
if merr, ok := warning.(*multierror.Error); ok { //nolint:errorlint
171-
// 3 errors are expected
172-
assert.Equal(t, 3, len(merr.Errors))
173-
}
169+
// Checking that we've got three warnings by splitting the lines
170+
message := warning.Error()
171+
merr := strings.Split(message, "\n")
172+
assert.Equal(t, 3, len(merr))
174173

175174
assert.Nil(t, err)
176175
assert.Equal(t, "5", def.IndentSize)

go.mod

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
module github.com/editorconfig/editorconfig-core-go/v2
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/google/go-cmp v0.5.9
7-
github.com/hashicorp/go-multierror v1.1.1
87
golang.org/x/mod v0.12.0
98
gopkg.in/ini.v1 v1.67.0
109
)
1110

12-
require (
13-
github.com/hashicorp/errwrap v1.0.0 // indirect
14-
github.com/stretchr/testify v1.7.0 // indirect
15-
)
11+
require github.com/stretchr/testify v1.7.0 // indirect

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
44
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5-
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
6-
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
7-
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
8-
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
95
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
106
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
117
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Parser interface {
99

1010
// ParseIni takes one .editorconfig (ini format) filename and returns its
1111
// Editorconfig definition. In case of non fatal warnings, they are in
12-
// a multierror and might be ignored in some cases.
12+
// a joined errors and might be ignored in some cases.
1313
ParseIniGraceful(filename string) (*Editorconfig, error, error)
1414

1515
// FnmatchCase takes a pattern, a filename, and tells wether the given filename

0 commit comments

Comments
 (0)