opentofu/internal/tfdiags/diagnostic.go
Martin Atkins 8405f46bc5 tfdiags: Expose the "extra information" concept from HCL
HCL's diagnostic model now includes the idea of "extra information" which
works by attaching an initially-opaque interface value to each diagnostic
and then asking callers to type-assert against that value to sniff for
particular interfaces in order to discover additional machine-readable
context about a certain diagnostic message.

This commit echoes that idea into our tfdiags API, for now only for
diagnostics that are backed by an hcl.Diagnostic. All other implementations
of the diagnostic interface just always return nil, which means they never
carry any "extra information".

As is typical for our wrapping abstraction, we have here also a modified
copy of HCL's helper function for conveniently probing a diagnostic for
information of a particular type, designed to work with our diagnostic
interface instead of HCL's concrete diagnostic type.
2022-06-23 13:52:23 -07:00

65 lines
1.5 KiB
Go

package tfdiags
import (
"fmt"
"github.com/hashicorp/hcl/v2"
)
type Diagnostic interface {
Severity() Severity
Description() Description
Source() Source
// FromExpr returns the expression-related context for the diagnostic, if
// available. Returns nil if the diagnostic is not related to an
// expression evaluation.
FromExpr() *FromExpr
// ExtraInfo returns the raw extra information value. This is a low-level
// API which requires some work on the part of the caller to properly
// access associated information, so in most cases it'll be more convienient
// to use the package-level ExtraInfo function to try to unpack a particular
// specialized interface from this value.
ExtraInfo() interface{}
}
type Severity rune
//go:generate go run golang.org/x/tools/cmd/stringer -type=Severity
const (
Error Severity = 'E'
Warning Severity = 'W'
)
// ToHCL converts a Severity to the equivalent HCL diagnostic severity.
func (s Severity) ToHCL() hcl.DiagnosticSeverity {
switch s {
case Warning:
return hcl.DiagWarning
case Error:
return hcl.DiagError
default:
// The above should always be exhaustive for all of the valid
// Severity values in this package.
panic(fmt.Sprintf("unknown diagnostic severity %s", s))
}
}
type Description struct {
Address string
Summary string
Detail string
}
type Source struct {
Subject *SourceRange
Context *SourceRange
}
type FromExpr struct {
Expression hcl.Expression
EvalContext *hcl.EvalContext
}