Skip to content

Commit

Permalink
deps: Remove github.com/hashicorp/go-multierror direct dependency (#1247
Browse files Browse the repository at this point in the history
)

Reference: hashicorp/terraform-plugin-testing#99

Similar to terraform-plugin-testing, now that this Go module is Go 1.20+, we can use native `errors.Join()` functionality for joining multiple errors.

To fully remove the dependency, will need to update some other dependencies as well, e.g.

```
# github.com/hashicorp/go-multierror
github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest
github.com/hashicorp/hc-install
github.com/hashicorp/go-multierror
```
  • Loading branch information
bflad authored Sep 6, 2023
1 parent 6b58e7f commit af738e0
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-plugin v1.5.1
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand Down Expand Up @@ -35,6 +34,7 @@ require (
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
Expand Down
9 changes: 4 additions & 5 deletions helper/customdiff/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package customdiff

import (
"context"

"github.com/hashicorp/go-multierror"
"errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -48,14 +47,14 @@ import (
// }
func All(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc {
return func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
var err error
var errs []error
for _, f := range funcs {
thisErr := f(ctx, d, meta)
if thisErr != nil {
err = multierror.Append(err, thisErr)
errs = append(errs, thisErr)
}
}
return err
return errors.Join(errs...)
}
}

Expand Down
15 changes: 8 additions & 7 deletions helper/customdiff/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ package customdiff
import (
"context"
"errors"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func TestAll(t *testing.T) {
var aCalled, bCalled, cCalled bool
aErr := errors.New("A bad")
cErr := errors.New("C bad")

provider := testProvider(
map[string]*schema.Schema{},
All(
func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
aCalled = true
return errors.New("A bad")
return aErr
},
func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
bCalled = true
return nil
},
func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
cCalled = true
return errors.New("C bad")
return cErr
},
),
)
Expand All @@ -46,11 +47,11 @@ func TestAll(t *testing.T) {
if err == nil {
t.Fatal("Diff succeeded; want error")
}
if s, sub := err.Error(), "* A bad"; !strings.Contains(s, sub) {
t.Errorf("Missing substring %q in error message %q", sub, s)
if !errors.Is(err, aErr) {
t.Errorf("Missing substring %q in error message %q", aErr, err)
}
if s, sub := err.Error(), "* C bad"; !strings.Contains(s, sub) {
t.Errorf("Missing substring %q in error message %q", sub, s)
if !errors.Is(err, cErr) {
t.Errorf("Missing substring %q in error message %q", cErr, err)
}

if !aCalled {
Expand Down
7 changes: 3 additions & 4 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"strings"
"time"

"github.com/hashicorp/go-multierror"
"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
Expand Down Expand Up @@ -836,15 +835,15 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
// TestCheckFuncs and aggregates failures.
func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
return func(s *terraform.State) error {
var result *multierror.Error
var result []error

for i, f := range fs {
if err := f(s); err != nil {
result = multierror.Append(result, fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err))
result = append(result, fmt.Errorf("Check %d/%d error: %w", i+1, len(fs), err))
}
}

return result.ErrorOrNil()
return errors.Join(result...)
}
}

Expand Down
18 changes: 9 additions & 9 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"testing"

"github.com/hashicorp/go-multierror"
testinginterface "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -88,26 +87,27 @@ func TestParallelTest(t *testing.T) {
}

func TestComposeAggregateTestCheckFunc(t *testing.T) {
err1 := errors.New("Error 1")
check1 := func(s *terraform.State) error {
return errors.New("Error 1")
return err1
}

err2 := errors.New("Error 2")
check2 := func(s *terraform.State) error {
return errors.New("Error 2")
return err2
}

f := ComposeAggregateTestCheckFunc(check1, check2)
err := f(nil)
if err == nil {
t.Fatalf("Expected errors")
t.Fatal("expected error, got none")
}

multi := err.(*multierror.Error)
if !strings.Contains(multi.Errors[0].Error(), "Error 1") {
t.Fatalf("Expected Error 1, Got %s", multi.Errors[0])
if !errors.Is(err, err1) {
t.Errorf("expected %s, got: %s", err1, err)
}
if !strings.Contains(multi.Errors[1].Error(), "Error 2") {
t.Fatalf("Expected Error 2, Got %s", multi.Errors[1])
if !errors.Is(err, err2) {
t.Errorf("expected %s, got: %s", err2, err)
}
}

Expand Down
12 changes: 5 additions & 7 deletions helper/schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"sort"
"strings"

"github.com/hashicorp/go-multierror"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
Expand Down Expand Up @@ -130,10 +128,10 @@ func (p *Provider) InternalValidate() error {
return errors.New("ConfigureFunc and ConfigureContextFunc must not both be set")
}

var validationErrors error
var validationErrors []error
sm := schemaMap(p.Schema)
if err := sm.InternalValidate(sm); err != nil {
validationErrors = multierror.Append(validationErrors, err)
validationErrors = append(validationErrors, err)
}

// Provider-specific checks
Expand All @@ -145,17 +143,17 @@ func (p *Provider) InternalValidate() error {

for k, r := range p.ResourcesMap {
if err := r.InternalValidate(nil, true); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
validationErrors = append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
}
}

for k, r := range p.DataSourcesMap {
if err := r.InternalValidate(nil, false); err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
validationErrors = append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
}
}

return validationErrors
return errors.Join(validationErrors...)
}

func isReservedProviderFieldName(name string) bool {
Expand Down
3 changes: 1 addition & 2 deletions internal/diagutils/diagutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)

Expand All @@ -28,7 +27,7 @@ func (diags ErrorDiags) Errors() []error {
}

func (diags ErrorDiags) Error() string {
return multierror.ListFormatFunc(diags.Errors())
return errors.Join(diags.Errors()...).Error()
}

type WarningDiags diag.Diagnostics
Expand Down
8 changes: 4 additions & 4 deletions terraform/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand All @@ -17,7 +18,6 @@ import (
"sync"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/mitchellh/copystructure"

Expand Down Expand Up @@ -287,7 +287,7 @@ func (s *State) Validate() error {
s.Lock()
defer s.Unlock()

var result error
var result []error

// !!!! FOR DEVELOPERS !!!!
//
Expand All @@ -309,7 +309,7 @@ func (s *State) Validate() error {

key := strings.Join(ms.Path, ".")
if _, ok := found[key]; ok {
result = multierror.Append(result, fmt.Errorf(
result = append(result, fmt.Errorf(
strings.TrimSpace(stateValidateErrMultiModule), key))
continue
}
Expand All @@ -318,7 +318,7 @@ func (s *State) Validate() error {
}
}

return result
return errors.Join(result...)
}

// Remove removes the item in the state at the given address, returning
Expand Down

0 comments on commit af738e0

Please sign in to comment.