|
| 1 | +package diag |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 5 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 6 | +) |
| 7 | + |
| 8 | +// Diagnostics represents a collection of diagnostics. |
| 9 | +// |
| 10 | +// While this collection is ordered, the order is not guaranteed as reliable |
| 11 | +// or consistent. |
| 12 | +type Diagnostics []Diagnostic |
| 13 | + |
| 14 | +// AddAttributeError adds a generic attribute error diagnostic to the collection. |
| 15 | +func (diags *Diagnostics) AddAttributeError(path *tftypes.AttributePath, summary string, detail string) { |
| 16 | + diags.Append(NewAttributeErrorDiagnostic(path, summary, detail)) |
| 17 | +} |
| 18 | + |
| 19 | +// AddAttributeWarning adds a generic attribute warning diagnostic to the collection. |
| 20 | +func (diags *Diagnostics) AddAttributeWarning(path *tftypes.AttributePath, summary string, detail string) { |
| 21 | + diags.Append(NewAttributeWarningDiagnostic(path, summary, detail)) |
| 22 | +} |
| 23 | + |
| 24 | +// AddError adds a generic error diagnostic to the collection. |
| 25 | +func (diags *Diagnostics) AddError(summary string, detail string) { |
| 26 | + diags.Append(NewErrorDiagnostic(summary, detail)) |
| 27 | +} |
| 28 | + |
| 29 | +// AddWarning adds a generic warning diagnostic to the collection. |
| 30 | +func (diags *Diagnostics) AddWarning(summary string, detail string) { |
| 31 | + diags.Append(NewWarningDiagnostic(summary, detail)) |
| 32 | +} |
| 33 | + |
| 34 | +// Append adds non-empty and non-duplicate diagnostics to the collection. |
| 35 | +func (diags *Diagnostics) Append(in ...Diagnostic) { |
| 36 | + for _, diag := range in { |
| 37 | + if diag == nil { |
| 38 | + continue |
| 39 | + } |
| 40 | + |
| 41 | + if diags.Contains(diag) { |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + if diags == nil { |
| 46 | + *diags = Diagnostics{diag} |
| 47 | + } else { |
| 48 | + *diags = append(*diags, diag) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Contains returns true if the collection contains an equal Diagnostic. |
| 54 | +func (diags Diagnostics) Contains(in Diagnostic) bool { |
| 55 | + for _, diag := range diags { |
| 56 | + if diag.Equal(in) { |
| 57 | + return true |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return false |
| 62 | +} |
| 63 | + |
| 64 | +// HasError returns true if the collection has an error severity Diagnostic. |
| 65 | +func (diags Diagnostics) HasError() bool { |
| 66 | + for _, diag := range diags { |
| 67 | + if diag.Severity() == SeverityError { |
| 68 | + return true |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return false |
| 73 | +} |
| 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 | +} |
0 commit comments