tfdiags: Expose expression evaluation information from diagnostics

HCL diagnostics relating to expression evaluation include these, so this
is really just to pass that information through our Terraform-specific
diagnostics interface.
This commit is contained in:
Martin Atkins 2018-10-17 17:47:13 -07:00
parent 625145563a
commit 8d100bfde9
6 changed files with 48 additions and 4 deletions

View File

@ -1,9 +1,18 @@
package tfdiags
import (
"github.com/hashicorp/hcl2/hcl"
)
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
}
type Severity rune
@ -24,3 +33,8 @@ type Source struct {
Subject *SourceRange
Context *SourceRange
}
type FromExpr struct {
Expression hcl.Expression
EvalContext *hcl.EvalContext
}

View File

@ -2,9 +2,9 @@ package tfdiags
// diagnosticBase can be embedded in other diagnostic structs to get
// default implementations of Severity and Description. This type also
// has a default implementation of Source that returns no source location
// information, so embedders should generally override that method to
// return more useful results.
// has default implementations of Source and FromExpr that return no source
// location or expression-related information, so embedders should generally
// override those method to return more useful results where possible.
type diagnosticBase struct {
severity Severity
summary string
@ -25,3 +25,7 @@ func (d diagnosticBase) Description() Description {
func (d diagnosticBase) Source() Source {
return Source{}
}
func (d diagnosticBase) FromExpr() *FromExpr {
return nil
}

View File

@ -21,3 +21,8 @@ func (e nativeError) Source() Source {
// No source information available for a native error
return Source{}
}
func (e nativeError) FromExpr() *FromExpr {
// Native errors are not expression-related
return nil
}

View File

@ -40,6 +40,16 @@ func (d hclDiagnostic) Source() Source {
return ret
}
func (d hclDiagnostic) FromExpr() *FromExpr {
if d.diag.Expression == nil || d.diag.EvalContext == nil {
return nil
}
return &FromExpr{
Expression: d.diag.Expression,
EvalContext: d.diag.EvalContext,
}
}
// SourceRangeFromHCL constructs a SourceRange from the corresponding range
// type within the HCL package.
func SourceRangeFromHCL(hclRange hcl.Range) SourceRange {

View File

@ -48,6 +48,12 @@ func (d *rpcFriendlyDiag) Source() Source {
}
}
func (d rpcFriendlyDiag) FromExpr() *FromExpr {
// RPC-friendly diagnostics cannot preserve expression information because
// expressions themselves are not RPC-friendly.
return nil
}
func init() {
gob.Register((*rpcFriendlyDiag)(nil))
}

View File

@ -20,6 +20,11 @@ func (e simpleWarning) Description() Description {
}
func (e simpleWarning) Source() Source {
// No source information available for a native error
// No source information available for a simple warning
return Source{}
}
func (e simpleWarning) FromExpr() *FromExpr {
// Simple warnings are not expression-related
return nil
}