Skip to content

Commit 692d3b9

Browse files
committed
address RFC and PR comments
1 parent 63b234d commit 692d3b9

File tree

14 files changed

+256
-263
lines changed

14 files changed

+256
-263
lines changed

diag/diagnostic.go

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,36 @@
11
package diag
22

33
import (
4-
"errors"
5-
"fmt"
6-
7-
"github.com/hashicorp/go-multierror"
84
"github.com/zclconf/go-cty/cty"
95
)
106

11-
type Diagnostics []*Diagnostic
7+
type Diagnostics []Diagnostic
128

13-
func (diags Diagnostics) Append(i interface{}) Diagnostics {
14-
switch v := i.(type) {
15-
case Diagnostics:
16-
if len(v) != 0 {
17-
diags = append(diags, v...)
18-
}
19-
case Diagnostic:
20-
diags = append(diags, &v)
21-
case *Diagnostic:
22-
diags = append(diags, v)
23-
case error:
24-
if v != nil {
25-
diags = append(diags, &Diagnostic{
26-
Severity: Error,
27-
Summary: v.Error(),
28-
})
29-
}
30-
}
31-
return diags
32-
}
33-
34-
func (diags Diagnostics) Err() error {
35-
return errors.New(multierror.ListFormatFunc(diags.Errors()))
36-
}
37-
38-
func (diags Diagnostics) HasErrors() bool {
39-
for _, d := range diags {
40-
if d.Severity == Error {
9+
func (diags Diagnostics) HasError() bool {
10+
for i := range diags {
11+
if diags[i].Severity == Error {
4112
return true
4213
}
4314
}
4415
return false
4516
}
4617

47-
func (diags Diagnostics) Errors() []error {
48-
var errs []error
49-
for _, d := range diags {
50-
if d.Severity == Error {
51-
errs = append(errs, error(d))
52-
}
53-
}
54-
return errs
55-
}
56-
57-
func (diags Diagnostics) Warnings() []string {
58-
var warns []string
59-
for _, d := range diags {
60-
if d.Severity == Warning {
61-
warns = append(warns, d.Error())
62-
}
63-
}
64-
return warns
65-
}
66-
6718
type Diagnostic struct {
6819
Severity Severity
6920
Summary string
7021
Detail string
7122
AttributePath cty.Path
7223
}
7324

74-
func (d *Diagnostic) Error() string {
75-
s := fmt.Sprintf("%s: %s", d.Severity, d.Summary)
76-
if d.Detail != "" {
77-
s = fmt.Sprintf("%s: %s", s, d.Detail)
25+
func FromErr(err error) Diagnostic {
26+
return Diagnostic{
27+
Severity: Error,
28+
Summary: err.Error(),
7829
}
79-
return s
8030
}
8131

8232
type Severity int
8333

84-
//go:generate go run golang.org/x/tools/cmd/stringer -type=Severity
85-
8634
const (
8735
Error Severity = iota
8836
Warning

diag/path.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package diag
2+
3+
import "github.com/zclconf/go-cty/cty"
4+
5+
func JoinPath(parent cty.Path, child cty.Path) cty.Path {
6+
if len(parent) > len(child) {
7+
tmp := parent
8+
parent = child
9+
child = tmp
10+
}
11+
if child.HasPrefix(parent) {
12+
return child
13+
} else {
14+
return append(parent, child...)
15+
}
16+
}

diag/severity_string.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

helper/schema/diagnostic.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package schema
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
)
10+
11+
type errorDiags diag.Diagnostics
12+
13+
func (diags errorDiags) Errors() []error {
14+
var es []error
15+
for i := range diags {
16+
if diags[i].Severity == diag.Error {
17+
s := fmt.Sprintf("Error: %s", diags[i].Summary)
18+
if diags[i].Detail != "" {
19+
s = fmt.Sprintf("%s: %s", s, diags[i].Detail)
20+
}
21+
es = append(es, errors.New(s))
22+
}
23+
}
24+
return es
25+
}
26+
27+
func (diags errorDiags) Error() string {
28+
return multierror.ListFormatFunc(diags.Errors())
29+
}
30+
31+
type warningDiags diag.Diagnostics
32+
33+
func (diags warningDiags) Warnings() []string {
34+
var ws []string
35+
for i := range diags {
36+
if diags[i].Severity == diag.Warning {
37+
s := fmt.Sprintf("Warning: %s", diags[i].Summary)
38+
if diags[i].Detail != "" {
39+
s = fmt.Sprintf("%s: %s", s, diags[i].Detail)
40+
}
41+
ws = append(ws, s)
42+
}
43+
}
44+
return ws
45+
}

helper/schema/provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (p *Provider) GetSchema(req *terraform.ProviderSchemaRequest) (*terraform.P
190190
// set.
191191
func (p *Provider) Validate(c *terraform.ResourceConfig) diag.Diagnostics {
192192
if err := p.InternalValidate(); err != nil {
193-
return []*diag.Diagnostic{
193+
return []diag.Diagnostic{
194194
{
195195
Severity: diag.Error,
196196
Summary: "InternalValidate",
@@ -217,7 +217,7 @@ func (p *Provider) ValidateResource(
217217
t string, c *terraform.ResourceConfig) diag.Diagnostics {
218218
r, ok := p.ResourcesMap[t]
219219
if !ok {
220-
return []*diag.Diagnostic{
220+
return []diag.Diagnostic{
221221
{
222222
Severity: diag.Error,
223223
Summary: fmt.Sprintf("Provider doesn't support resource: %s", t),
@@ -381,7 +381,7 @@ func (p *Provider) ValidateDataSource(
381381
t string, c *terraform.ResourceConfig) diag.Diagnostics {
382382
r, ok := p.DataSourcesMap[t]
383383
if !ok {
384-
return []*diag.Diagnostic{
384+
return []diag.Diagnostic{
385385
{
386386
Severity: diag.Error,
387387
Summary: fmt.Sprintf("Provider doesn't support data source: %s", t),

helper/schema/provider_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ func TestProviderValidate(t *testing.T) {
283283
for i, tc := range cases {
284284
c := terraform.NewResourceConfigRaw(tc.Config)
285285
diags := tc.P.Validate(c)
286-
if diags.HasErrors() != tc.Err {
287-
t.Fatalf("%d: %#v", i, diags.Err())
286+
if diags.HasError() != tc.Err {
287+
t.Fatalf("%d: %#v", i, diags)
288288
}
289289
}
290290
}
@@ -396,8 +396,8 @@ func TestProviderValidateResource(t *testing.T) {
396396
for i, tc := range cases {
397397
c := terraform.NewResourceConfigRaw(tc.Config)
398398
diags := tc.P.ValidateResource(tc.Type, c)
399-
if diags.HasErrors() != tc.Err {
400-
t.Fatalf("%d: %#v", i, diags.Err())
399+
if diags.HasError() != tc.Err {
400+
t.Fatalf("%d: %#v", i, diags)
401401
}
402402
}
403403
}

0 commit comments

Comments
 (0)