opentofu/tfdiags/rpc_friendly.go
Martin Atkins 780e758f1e tfdiags: Allow construction of RPC-friendly Diagnostics
Due to the use of interfaces, Diagnostics is not super-friendly to the gob
encoding we currently use for plugin RPC. To mitigate this, we provide
a helper that converts all of the wrapped objects into a predictable flat
structure that we can pre-emptively register with gob.

This means that the decoded Diagnostics still has the same meaning as
the original, though the original wrapped errors (if any) are lost and
thus our errwrap integration won't be effective any longer.
2017-10-06 11:46:07 -07:00

54 lines
1.1 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 init() {
gob.Register((*rpcFriendlyDiag)(nil))
}