github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/tfdiags/rpc_friendly.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package tfdiags
     5  
     6  import (
     7  	"encoding/gob"
     8  )
     9  
    10  type rpcFriendlyDiag struct {
    11  	Severity_ Severity
    12  	Summary_  string
    13  	Detail_   string
    14  	Subject_  *SourceRange
    15  	Context_  *SourceRange
    16  }
    17  
    18  // rpcFriendlyDiag transforms a given diagnostic so that is more friendly to
    19  // RPC.
    20  //
    21  // In particular, it currently returns an object that can be serialized and
    22  // later re-inflated using gob. This definition may grow to include other
    23  // serializations later.
    24  func makeRPCFriendlyDiag(diag Diagnostic) Diagnostic {
    25  	desc := diag.Description()
    26  	source := diag.Source()
    27  	return &rpcFriendlyDiag{
    28  		Severity_: diag.Severity(),
    29  		Summary_:  desc.Summary,
    30  		Detail_:   desc.Detail,
    31  		Subject_:  source.Subject,
    32  		Context_:  source.Context,
    33  	}
    34  }
    35  
    36  func (d *rpcFriendlyDiag) Severity() Severity {
    37  	return d.Severity_
    38  }
    39  
    40  func (d *rpcFriendlyDiag) Description() Description {
    41  	return Description{
    42  		Summary: d.Summary_,
    43  		Detail:  d.Detail_,
    44  	}
    45  }
    46  
    47  func (d *rpcFriendlyDiag) Source() Source {
    48  	return Source{
    49  		Subject: d.Subject_,
    50  		Context: d.Context_,
    51  	}
    52  }
    53  
    54  func (d rpcFriendlyDiag) FromExpr() *FromExpr {
    55  	// RPC-friendly diagnostics cannot preserve expression information because
    56  	// expressions themselves are not RPC-friendly.
    57  	return nil
    58  }
    59  
    60  func (d rpcFriendlyDiag) ExtraInfo() interface{} {
    61  	// RPC-friendly diagnostics always discard any "extra information".
    62  	return nil
    63  }
    64  
    65  func init() {
    66  	gob.Register((*rpcFriendlyDiag)(nil))
    67  }