Skip to content

Commit 12176c9

Browse files
authored
diag: Move (Diagnostics).ToTfprotov6Diagnostics() method to internal/toproto6 package (#313)
Reference: #215
1 parent 72e4d38 commit 12176c9

File tree

8 files changed

+157
-146
lines changed

8 files changed

+157
-146
lines changed

.changelog/313.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
diag: Removed `Diagnostics` type `ToTfprotov6Diagnostics()` method. This was not intended for usage by provider developers.
3+
```

diag/diagnostic_test.go

-31
This file was deleted.

diag/diagnostics.go

-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package diag
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
54
"github.com/hashicorp/terraform-plugin-go/tftypes"
65
)
76

@@ -71,27 +70,3 @@ func (diags Diagnostics) HasError() bool {
7170

7271
return false
7372
}
74-
75-
// ToTfprotov6Diagnostics converts the diagnostics into the tfprotov6 collection type.
76-
//
77-
// Usage of this method outside the framework is not supported nor considered
78-
// for backwards compatibility promises.
79-
func (diags Diagnostics) ToTfprotov6Diagnostics() []*tfprotov6.Diagnostic {
80-
var results []*tfprotov6.Diagnostic
81-
82-
for _, diag := range diags {
83-
tfprotov6Diagnostic := &tfprotov6.Diagnostic{
84-
Detail: diag.Detail(),
85-
Severity: diag.Severity().ToTfprotov6DiagnosticSeverity(),
86-
Summary: diag.Summary(),
87-
}
88-
89-
if diagWithPath, ok := diag.(DiagnosticWithPath); ok {
90-
tfprotov6Diagnostic.Attribute = diagWithPath.Path()
91-
}
92-
93-
results = append(results, tfprotov6Diagnostic)
94-
}
95-
96-
return results
97-
}

diag/diagnostics_test.go

-78
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/google/go-cmp/cmp"
77
"github.com/hashicorp/terraform-plugin-framework/diag"
8-
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
98
"github.com/hashicorp/terraform-plugin-go/tftypes"
109
)
1110

@@ -519,80 +518,3 @@ func TestDiagnosticsHasError(t *testing.T) {
519518
})
520519
}
521520
}
522-
523-
func TestDiagnosticsToTfprotov6Diagnostics(t *testing.T) {
524-
t.Parallel()
525-
526-
testCases := map[string]struct {
527-
diags diag.Diagnostics
528-
expected []*tfprotov6.Diagnostic
529-
}{
530-
"nil": {
531-
diags: nil,
532-
expected: nil,
533-
},
534-
"Diagnostic-SeverityInvalid": {
535-
diags: diag.Diagnostics{
536-
invalidSeverityDiagnostic{},
537-
},
538-
expected: []*tfprotov6.Diagnostic{
539-
{
540-
Detail: invalidSeverityDiagnostic{}.Detail(),
541-
Severity: tfprotov6.DiagnosticSeverityInvalid,
542-
Summary: invalidSeverityDiagnostic{}.Summary(),
543-
},
544-
},
545-
},
546-
"Diagnostic": {
547-
diags: diag.Diagnostics{
548-
diag.NewErrorDiagnostic("one summary", "one detail"),
549-
diag.NewWarningDiagnostic("two summary", "two detail"),
550-
},
551-
expected: []*tfprotov6.Diagnostic{
552-
{
553-
Detail: "one detail",
554-
Severity: tfprotov6.DiagnosticSeverityError,
555-
Summary: "one summary",
556-
},
557-
{
558-
Detail: "two detail",
559-
Severity: tfprotov6.DiagnosticSeverityWarning,
560-
Summary: "two summary",
561-
},
562-
},
563-
},
564-
"DiagnosticWithPath": {
565-
diags: diag.Diagnostics{
566-
diag.NewAttributeErrorDiagnostic(tftypes.NewAttributePath(), "one summary", "one detail"),
567-
diag.NewAttributeWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("test"), "two summary", "two detail"),
568-
},
569-
expected: []*tfprotov6.Diagnostic{
570-
{
571-
Attribute: tftypes.NewAttributePath(),
572-
Detail: "one detail",
573-
Severity: tfprotov6.DiagnosticSeverityError,
574-
Summary: "one summary",
575-
},
576-
{
577-
Attribute: tftypes.NewAttributePath().WithAttributeName("test"),
578-
Detail: "two detail",
579-
Severity: tfprotov6.DiagnosticSeverityWarning,
580-
Summary: "two summary",
581-
},
582-
},
583-
},
584-
}
585-
586-
for name, tc := range testCases {
587-
name, tc := name, tc
588-
t.Run(name, func(t *testing.T) {
589-
t.Parallel()
590-
591-
got := tc.diags.ToTfprotov6Diagnostics()
592-
593-
if diff := cmp.Diff(got, tc.expected); diff != "" {
594-
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
595-
}
596-
})
597-
}
598-
}

internal/proto6server/serve.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (r getProviderSchemaResponse) toTfprotov6() *tfprotov6.GetProviderSchemaRes
101101
ProviderMeta: r.ProviderMeta,
102102
ResourceSchemas: r.ResourceSchemas,
103103
DataSourceSchemas: r.DataSourceSchemas,
104-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
104+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
105105
}
106106
}
107107

@@ -240,7 +240,7 @@ type validateProviderConfigResponse struct {
240240
func (r validateProviderConfigResponse) toTfprotov6() *tfprotov6.ValidateProviderConfigResponse {
241241
return &tfprotov6.ValidateProviderConfigResponse{
242242
PreparedConfig: r.PreparedConfig,
243-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
243+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
244244
}
245245
}
246246

@@ -353,7 +353,7 @@ type configureProviderResponse struct {
353353

354354
func (r configureProviderResponse) toTfprotov6() *tfprotov6.ConfigureProviderResponse {
355355
return &tfprotov6.ConfigureProviderResponse{
356-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
356+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
357357
}
358358
}
359359

@@ -409,7 +409,7 @@ type validateResourceConfigResponse struct {
409409

410410
func (r validateResourceConfigResponse) toTfprotov6() *tfprotov6.ValidateResourceConfigResponse {
411411
return &tfprotov6.ValidateResourceConfigResponse{
412-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
412+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
413413
}
414414
}
415415

@@ -536,7 +536,7 @@ type upgradeResourceStateResponse struct {
536536

537537
func (r upgradeResourceStateResponse) toTfprotov6() *tfprotov6.UpgradeResourceStateResponse {
538538
return &tfprotov6.UpgradeResourceStateResponse{
539-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
539+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
540540
UpgradedState: r.UpgradedState,
541541
}
542542
}
@@ -766,7 +766,7 @@ type readResourceResponse struct {
766766
func (r readResourceResponse) toTfprotov6() *tfprotov6.ReadResourceResponse {
767767
return &tfprotov6.ReadResourceResponse{
768768
NewState: r.NewState,
769-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
769+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
770770
Private: r.Private,
771771
}
772772
}
@@ -911,7 +911,7 @@ type planResourceChangeResponse struct {
911911
func (r planResourceChangeResponse) toTfprotov6() *tfprotov6.PlanResourceChangeResponse {
912912
return &tfprotov6.PlanResourceChangeResponse{
913913
PlannedState: r.PlannedState,
914-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
914+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
915915
RequiresReplace: r.RequiresReplace,
916916
PlannedPrivate: r.PlannedPrivate,
917917
}
@@ -1209,7 +1209,7 @@ func (r applyResourceChangeResponse) toTfprotov6() *tfprotov6.ApplyResourceChang
12091209
return &tfprotov6.ApplyResourceChangeResponse{
12101210
NewState: r.NewState,
12111211
Private: r.Private,
1212-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
1212+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
12131213
}
12141214
}
12151215

@@ -1532,7 +1532,7 @@ type validateDataResourceConfigResponse struct {
15321532

15331533
func (r validateDataResourceConfigResponse) toTfprotov6() *tfprotov6.ValidateDataResourceConfigResponse {
15341534
return &tfprotov6.ValidateDataResourceConfigResponse{
1535-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
1535+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
15361536
}
15371537
}
15381538

@@ -1660,7 +1660,7 @@ type readDataSourceResponse struct {
16601660
func (r readDataSourceResponse) toTfprotov6() *tfprotov6.ReadDataSourceResponse {
16611661
return &tfprotov6.ReadDataSourceResponse{
16621662
State: r.State,
1663-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
1663+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
16641664
}
16651665
}
16661666

internal/proto6server/serve_import.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-framework/diag"
77
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
8+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
910
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1011
"github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -50,12 +51,12 @@ type importResourceStateResponse struct {
5051

5152
func (r importResourceStateResponse) toTfprotov6(ctx context.Context) *tfprotov6.ImportResourceStateResponse {
5253
resp := &tfprotov6.ImportResourceStateResponse{
53-
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
54+
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
5455
}
5556

5657
for _, ir := range r.ImportedResources {
5758
irProto6, diags := ir.toTfprotov6(ctx)
58-
resp.Diagnostics = append(resp.Diagnostics, diags.ToTfprotov6Diagnostics()...)
59+
resp.Diagnostics = append(resp.Diagnostics, toproto6.Diagnostics(diags)...)
5960
if diags.HasError() {
6061
continue
6162
}

internal/toproto6/diagnostics.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package toproto6
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/diag"
5+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
6+
)
7+
8+
// Diagnostics converts the diagnostics into the tfprotov6 collection type.
9+
func Diagnostics(diagnostics diag.Diagnostics) []*tfprotov6.Diagnostic {
10+
var results []*tfprotov6.Diagnostic
11+
12+
for _, diagnostic := range diagnostics {
13+
tfprotov6Diagnostic := &tfprotov6.Diagnostic{
14+
Detail: diagnostic.Detail(),
15+
Severity: diagnostic.Severity().ToTfprotov6DiagnosticSeverity(),
16+
Summary: diagnostic.Summary(),
17+
}
18+
19+
if diagWithPath, ok := diagnostic.(diag.DiagnosticWithPath); ok {
20+
tfprotov6Diagnostic.Attribute = diagWithPath.Path()
21+
}
22+
23+
results = append(results, tfprotov6Diagnostic)
24+
}
25+
26+
return results
27+
}

0 commit comments

Comments
 (0)