opentofu/internal/tfdiags/rpc_friendly.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.4 KiB
Go

package tfdiags
import (
"encoding/gob"
)
type rpcFriendlyDiag struct {
Severity_ Severity
Summary_ string
Detail_ string
Subject_ *SourceRange
Context_ *SourceRange
}
// rpcFriendlyDiag transforms a given diagnostic so that is more friendly to
// RPC.
//
// In particular, it currently returns an object that can be serialized and
// later re-inflated using gob. This definition may grow to include other
// serializations later.
func makeRPCFriendlyDiag(diag Diagnostic) Diagnostic {
desc := diag.Description()
source := diag.Source()
return &rpcFriendlyDiag{
Severity_: diag.Severity(),
Summary_: desc.Summary,
Detail_: desc.Detail,
Subject_: source.Subject,
Context_: source.Context,
}
}
func (d *rpcFriendlyDiag) Severity() Severity {
return d.Severity_
}
func (d *rpcFriendlyDiag) Description() Description {
return Description{
Summary: d.Summary_,
Detail: d.Detail_,
}
}
func (d *rpcFriendlyDiag) Source() Source {
return Source{
Subject: d.Subject_,
Context: d.Context_,
}
}
func (d rpcFriendlyDiag) FromExpr() *FromExpr {
// RPC-friendly diagnostics cannot preserve expression information because
// expressions themselves are not RPC-friendly.
return nil
}
func (d rpcFriendlyDiag) ExtraInfo() interface{} {
// RPC-friendly diagnostics always discard any "extra information".
return nil
}
func init() {
gob.Register((*rpcFriendlyDiag)(nil))
}