github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/check_rule_diagnostic.go (about)

     1  package addrs
     2  
     3  import "github.com/terramate-io/tf/tfdiags"
     4  
     5  // DiagnosticExtraCheckRule provides an interface for diagnostic ExtraInfo to
     6  // retrieve an embedded CheckRule from within a tfdiags.Diagnostic.
     7  type DiagnosticExtraCheckRule interface {
     8  	// DiagnosticOriginatesFromCheckRule returns the CheckRule that the
     9  	// surrounding diagnostic originated from.
    10  	DiagnosticOriginatesFromCheckRule() CheckRule
    11  }
    12  
    13  // DiagnosticOriginatesFromCheckRule checks if the provided diagnostic contains
    14  // a CheckRule as ExtraInfo and returns that CheckRule and true if it does. This
    15  // function returns an empty CheckRule and false if the diagnostic does not
    16  // contain a CheckRule.
    17  func DiagnosticOriginatesFromCheckRule(diag tfdiags.Diagnostic) (CheckRule, bool) {
    18  	maybe := tfdiags.ExtraInfo[DiagnosticExtraCheckRule](diag)
    19  	if maybe == nil {
    20  		return CheckRule{}, false
    21  	}
    22  	return maybe.DiagnosticOriginatesFromCheckRule(), true
    23  }
    24  
    25  // CheckRuleDiagnosticExtra is an object that can be attached to diagnostics
    26  // that originate from check rules.
    27  //
    28  // It implements the DiagnosticExtraCheckRule interface for retrieving the
    29  // concrete CheckRule that spawned the diagnostic.
    30  //
    31  // It also implements the tfdiags.DiagnosticExtraDoNotConsolidate interface, to
    32  // stop diagnostics created by check blocks being consolidated.
    33  //
    34  // It also implements the tfdiags.DiagnosticExtraUnwrapper interface, as nested
    35  // data blocks will attach this struct but do want to lose any extra info
    36  // embedded in the original diagnostic.
    37  type CheckRuleDiagnosticExtra struct {
    38  	CheckRule CheckRule
    39  
    40  	wrapped interface{}
    41  }
    42  
    43  var (
    44  	_ DiagnosticExtraCheckRule                = (*CheckRuleDiagnosticExtra)(nil)
    45  	_ tfdiags.DiagnosticExtraDoNotConsolidate = (*CheckRuleDiagnosticExtra)(nil)
    46  	_ tfdiags.DiagnosticExtraUnwrapper        = (*CheckRuleDiagnosticExtra)(nil)
    47  	_ tfdiags.DiagnosticExtraWrapper          = (*CheckRuleDiagnosticExtra)(nil)
    48  )
    49  
    50  func (c *CheckRuleDiagnosticExtra) UnwrapDiagnosticExtra() interface{} {
    51  	return c.wrapped
    52  }
    53  
    54  func (c *CheckRuleDiagnosticExtra) WrapDiagnosticExtra(inner interface{}) {
    55  	if c.wrapped != nil {
    56  		// This is a logical inconsistency, the caller should know whether they
    57  		// have already wrapped an extra or not.
    58  		panic("Attempted to wrap a diagnostic extra into a CheckRuleDiagnosticExtra that is already wrapping a different extra. This is a bug in Terraform, please report it.")
    59  	}
    60  	c.wrapped = inner
    61  }
    62  
    63  func (c *CheckRuleDiagnosticExtra) DoNotConsolidateDiagnostic() bool {
    64  	// Do not consolidate warnings from check blocks.
    65  	return c.CheckRule.Container.CheckableKind() == CheckableCheck
    66  }
    67  
    68  func (c *CheckRuleDiagnosticExtra) DiagnosticOriginatesFromCheckRule() CheckRule {
    69  	return c.CheckRule
    70  }