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