github.com/opentofu/opentofu@v1.7.1/internal/tfdiags/rpc_friendly.go (about)

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