/
diagnostics.go
48 lines (38 loc) · 1.34 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package toproto6
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/totftypes"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)
// DiagnosticSeverity converts diag.Severity into tfprotov6.DiagnosticSeverity.
func DiagnosticSeverity(s diag.Severity) tfprotov6.DiagnosticSeverity {
switch s {
case diag.SeverityError:
return tfprotov6.DiagnosticSeverityError
case diag.SeverityWarning:
return tfprotov6.DiagnosticSeverityWarning
default:
return tfprotov6.DiagnosticSeverityInvalid
}
}
// Diagnostics converts the diagnostics into the tfprotov6 collection type.
func Diagnostics(ctx context.Context, diagnostics diag.Diagnostics) []*tfprotov6.Diagnostic {
var results []*tfprotov6.Diagnostic
for _, diagnostic := range diagnostics {
tfprotov6Diagnostic := &tfprotov6.Diagnostic{
Detail: diagnostic.Detail(),
Severity: DiagnosticSeverity(diagnostic.Severity()),
Summary: diagnostic.Summary(),
}
if diagWithPath, ok := diagnostic.(diag.DiagnosticWithPath); ok {
var diags diag.Diagnostics
tfprotov6Diagnostic.Attribute, diags = totftypes.AttributePath(ctx, diagWithPath.Path())
if diags.HasError() {
results = append(results, Diagnostics(ctx, diags)...)
}
}
results = append(results, tfprotov6Diagnostic)
}
return results
}