Skip to content

Commit

Permalink
feat: replace go-multierror with Go 1.20 native joins (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
  • Loading branch information
greut committed Aug 9, 2023
1 parent 83a3feb commit 73c37c4
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 44 deletions.
8 changes: 4 additions & 4 deletions cached_parser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package editorconfig

import (
"errors"
"fmt"
"os"
"regexp"

"github.com/hashicorp/go-multierror"
"gopkg.in/ini.v1"
)

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

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

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

if warn != nil {
warning = multierror.Append(warning, warn)
warning = errors.Join(warning, warn)
}

parser.editorconfigs[filename] = ec
}

return ec, warning.ErrorOrNil(), nil //nolint:wrapcheck
return ec, warning, nil
}

// FnmatchCase calls the module's FnmatchCase and caches the parsed selector.
Expand Down
11 changes: 5 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"strings"

"github.com/hashicorp/go-multierror"
"golang.org/x/mod/semver"
)

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

return definition, err //nolint:wrapcheck
return definition, err
}

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

var warning *multierror.Error
var warning error

dir := absFilename
for dir != filepath.Dir(dir) {
dir = filepath.Dir(dir)

ec, warn, err := config.Parser.ParseIniGraceful(filepath.Join(dir, ecFile))
if warn != nil {
warning = multierror.Append(warning, warn)
warning = errors.Join(warning, warn)
}

if err != nil {
Expand Down Expand Up @@ -105,5 +104,5 @@ func (config *Config) LoadGraceful(filename string) (*Definition, error, error)
}
}

return definition, warning.ErrorOrNil(), nil //nolint:wrapcheck
return definition, warning, nil
}
12 changes: 6 additions & 6 deletions definition.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package editorconfig

import (
"errors"
"fmt"
"strconv"
"strings"

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

// normalize fixes some values to their lowercase value.
func (d *Definition) normalize() error {
var result *multierror.Error
var result error

d.Charset = strings.ToLower(d.Charset)
d.EndOfLine = strings.ToLower(d.Raw["end_of_line"])
Expand All @@ -45,7 +45,7 @@ func (d *Definition) normalize() error {
if ok && trimTrailingWhitespace != UnsetValue {
trim, err := strconv.ParseBool(trimTrailingWhitespace)
if err != nil {
result = multierror.Append(
result = errors.Join(
result,
fmt.Errorf("trim_trailing_whitespace=%s is not an acceptable value. %w", trimTrailingWhitespace, err),
)
Expand All @@ -58,7 +58,7 @@ func (d *Definition) normalize() error {
if ok && insertFinalNewline != UnsetValue {
insert, err := strconv.ParseBool(insertFinalNewline)
if err != nil {
result = multierror.Append(
result = errors.Join(
result,
fmt.Errorf("insert_final_newline=%s is not an acceptable value. %w", insertFinalNewline, err),
)
Expand All @@ -72,7 +72,7 @@ func (d *Definition) normalize() error {
if ok && tabWidth != UnsetValue {
num, err := strconv.Atoi(tabWidth)
if err != nil {
result = multierror.Append(result, fmt.Errorf("tab_width=%s is not an acceptable value. %w", tabWidth, err))
result = errors.Join(result, fmt.Errorf("tab_width=%s is not an acceptable value. %w", tabWidth, err))
} else {
d.TabWidth = num
}
Expand All @@ -85,7 +85,7 @@ func (d *Definition) normalize() error {
d.TabWidth = num
}

return result.ErrorOrNil() //nolint:wrapcheck
return result
}

// merge the parent definition into the child definition.
Expand Down
22 changes: 11 additions & 11 deletions editorconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package editorconfig

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"

"github.com/hashicorp/go-multierror"
"gopkg.in/ini.v1"
)

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

if err := definition.normalize(); err != nil {
// Append those error(s) into the warning
warning = multierror.Append(warning, err)
warning = errors.Join(warning, err)
}

editorConfig.Definitions = append(editorConfig.Definitions, definition)
}

return editorConfig, warning, nil //nolint:wrapcheck
return editorConfig, warning, nil
}

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

ec, warning, err := newEditorconfig(iniFile)
if warning != nil {
err = multierror.Append(warning)
err = errors.Join(warning, err)
}

return ec, err //nolint:wrapcheck
return ec, err
}

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

ec, warning, err := newEditorconfig(iniFile)
if warning != nil {
err = multierror.Append(warning)
err = errors.Join(warning, err)
}

return ec, err //nolint:wrapcheck
return ec, err
}

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

ec, warning, err := newEditorconfig(iniFile)
if warning != nil {
err = multierror.Append(warning)
err = errors.Join(warning, err)
}

return ec, err //nolint:wrapcheck
return ec, err
}

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

Expand All @@ -305,7 +305,7 @@ func GetDefinitionForFilenameWithConfigname(filename string, configname string)
// walking through the previous folders, until it reaches a folder with `root =
// true`, and returns the right editorconfig definition for the given file.
//
// In case of non-fatal errors, a multierror warning is return as well.
// In case of non-fatal errors, a joined errors warning is return as well.
func GetDefinitionForFilenameWithConfignameGraceful(filename string, configname string) (*Definition, error, error) {
config := &Config{
Name: configname,
Expand Down
11 changes: 5 additions & 6 deletions editorconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"testing/iotest"

"github.com/hashicorp/go-multierror"

"github.com/editorconfig/editorconfig-core-go/v2/internal/assert"
)

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

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

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

assert.Nil(t, err)
assert.Equal(t, "5", def.IndentSize)
Expand Down
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
module github.com/editorconfig/editorconfig-core-go/v2

go 1.18
go 1.20

require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-multierror v1.1.1
golang.org/x/mod v0.12.0
gopkg.in/ini.v1 v1.67.0
)

require (
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
)
require github.com/stretchr/testify v1.7.0 // indirect
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Parser interface {

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

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

0 comments on commit 73c37c4

Please sign in to comment.