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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package tfdiags
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/hashicorp/hcl/v2"
    10  )
    11  
    12  type Diagnostic interface {
    13  	Severity() Severity
    14  	Description() Description
    15  	Source() Source
    16  
    17  	// FromExpr returns the expression-related context for the diagnostic, if
    18  	// available. Returns nil if the diagnostic is not related to an
    19  	// expression evaluation.
    20  	FromExpr() *FromExpr
    21  
    22  	// ExtraInfo returns the raw extra information value. This is a low-level
    23  	// API which requires some work on the part of the caller to properly
    24  	// access associated information, so in most cases it'll be more convienient
    25  	// to use the package-level ExtraInfo function to try to unpack a particular
    26  	// specialized interface from this value.
    27  	ExtraInfo() interface{}
    28  }
    29  
    30  type Severity rune
    31  
    32  //go:generate go run golang.org/x/tools/cmd/stringer -type=Severity
    33  
    34  const (
    35  	Error   Severity = 'E'
    36  	Warning Severity = 'W'
    37  )
    38  
    39  // ToHCL converts a Severity to the equivalent HCL diagnostic severity.
    40  func (s Severity) ToHCL() hcl.DiagnosticSeverity {
    41  	switch s {
    42  	case Warning:
    43  		return hcl.DiagWarning
    44  	case Error:
    45  		return hcl.DiagError
    46  	default:
    47  		// The above should always be exhaustive for all of the valid
    48  		// Severity values in this package.
    49  		panic(fmt.Sprintf("unknown diagnostic severity %s", s))
    50  	}
    51  }
    52  
    53  type Description struct {
    54  	Address string
    55  	Summary string
    56  	Detail  string
    57  }
    58  
    59  type Source struct {
    60  	Subject *SourceRange
    61  	Context *SourceRange
    62  }
    63  
    64  type FromExpr struct {
    65  	Expression  hcl.Expression
    66  	EvalContext *hcl.EvalContext
    67  }