opentofu/internal/earlyconfig/diagnostics.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

83 lines
2.2 KiB
Go

package earlyconfig
import (
"fmt"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/hashicorp/terraform/internal/tfdiags"
)
func wrapDiagnostics(diags tfconfig.Diagnostics) tfdiags.Diagnostics {
ret := make(tfdiags.Diagnostics, len(diags))
for i, diag := range diags {
ret[i] = wrapDiagnostic(diag)
}
return ret
}
func wrapDiagnostic(diag tfconfig.Diagnostic) tfdiags.Diagnostic {
return wrappedDiagnostic{
d: diag,
}
}
type wrappedDiagnostic struct {
d tfconfig.Diagnostic
}
func (d wrappedDiagnostic) Severity() tfdiags.Severity {
switch d.d.Severity {
case tfconfig.DiagError:
return tfdiags.Error
case tfconfig.DiagWarning:
return tfdiags.Warning
default:
// Should never happen since there are no other severities
return 0
}
}
func (d wrappedDiagnostic) Description() tfdiags.Description {
// Since the inspect library doesn't produce precise source locations,
// we include the position information as part of the error message text.
// See the comment inside method "Source" for more information.
switch {
case d.d.Pos == nil:
return tfdiags.Description{
Summary: d.d.Summary,
Detail: d.d.Detail,
}
case d.d.Detail != "":
return tfdiags.Description{
Summary: d.d.Summary,
Detail: fmt.Sprintf("On %s line %d: %s", d.d.Pos.Filename, d.d.Pos.Line, d.d.Detail),
}
default:
return tfdiags.Description{
Summary: fmt.Sprintf("%s (on %s line %d)", d.d.Summary, d.d.Pos.Filename, d.d.Pos.Line),
}
}
}
func (d wrappedDiagnostic) Source() tfdiags.Source {
// Since the inspect library is constrained by the lowest common denominator
// between legacy HCL and modern HCL, it only returns ranges at whole-line
// granularity, and that isn't sufficient to populate a tfdiags.Source
// and so we'll just omit ranges altogether and include the line number in
// the Description text.
//
// Callers that want to return nicer errors should consider reacting to
// earlyconfig errors by attempting a follow-up parse with the normal
// config loader, which can produce more precise source location
// information.
return tfdiags.Source{}
}
func (d wrappedDiagnostic) FromExpr() *tfdiags.FromExpr {
return nil
}
func (d wrappedDiagnostic) ExtraInfo() interface{} {
return nil
}