github.com/opentofu/opentofu@v1.7.1/internal/tfdiags/override.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package tfdiags 7 8 // overriddenDiagnostic implements the Diagnostic interface by wrapping another 9 // Diagnostic while overriding the severity of the original Diagnostic. 10 type overriddenDiagnostic struct { 11 original Diagnostic 12 severity Severity 13 extra interface{} 14 } 15 16 var _ Diagnostic = overriddenDiagnostic{} 17 18 // OverrideAll accepts a set of Diagnostics and wraps them with a new severity 19 // and, optionally, a new ExtraInfo. 20 func OverrideAll(originals Diagnostics, severity Severity, createExtra func() DiagnosticExtraWrapper) Diagnostics { 21 var diags Diagnostics 22 for _, diag := range originals { 23 diags = diags.Append(Override(diag, severity, createExtra)) 24 } 25 return diags 26 } 27 28 // Override matches OverrideAll except it operates over a single Diagnostic 29 // rather than multiple Diagnostics. 30 func Override(original Diagnostic, severity Severity, createExtra func() DiagnosticExtraWrapper) Diagnostic { 31 extra := original.ExtraInfo() 32 if createExtra != nil { 33 nw := createExtra() 34 nw.WrapDiagnosticExtra(extra) 35 extra = nw 36 } 37 38 return overriddenDiagnostic{ 39 original: original, 40 severity: severity, 41 extra: extra, 42 } 43 } 44 45 // UndoOverride will return the original diagnostic that was overridden within 46 // the OverrideAll function. 47 // 48 // If the provided Diagnostic was never overridden then it is simply returned 49 // unchanged. 50 func UndoOverride(diag Diagnostic) Diagnostic { 51 if override, ok := diag.(overriddenDiagnostic); ok { 52 return override.original 53 } 54 55 // Then it wasn't overridden, so we'll just return the diag unchanged. 56 return diag 57 } 58 59 func (o overriddenDiagnostic) Severity() Severity { 60 return o.severity 61 } 62 63 func (o overriddenDiagnostic) Description() Description { 64 return o.original.Description() 65 } 66 67 func (o overriddenDiagnostic) Source() Source { 68 return o.original.Source() 69 } 70 71 func (o overriddenDiagnostic) FromExpr() *FromExpr { 72 return o.original.FromExpr() 73 } 74 75 func (o overriddenDiagnostic) ExtraInfo() interface{} { 76 return o.extra 77 }