github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/parse/decode_result.go (about)

     1  package parse
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/turbot/go-kit/helpers"
     6  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
     7  )
     8  
     9  // struct to hold the result of a decoding operation
    10  type DecodeResult struct {
    11  	Diags   hcl.Diagnostics
    12  	Depends map[string]*modconfig.ResourceDependency
    13  }
    14  
    15  func newDecodeResult() *DecodeResult {
    16  	return &DecodeResult{Depends: make(map[string]*modconfig.ResourceDependency)}
    17  }
    18  
    19  // Merge merges this decode result with another
    20  func (p *DecodeResult) Merge(other *DecodeResult) *DecodeResult {
    21  	p.Diags = append(p.Diags, other.Diags...)
    22  	for k, v := range other.Depends {
    23  		p.Depends[k] = v
    24  	}
    25  
    26  	return p
    27  }
    28  
    29  // Success returns if the was parsing successful - true if there are no errors and no dependencies
    30  func (p *DecodeResult) Success() bool {
    31  	return !p.Diags.HasErrors() && len(p.Depends) == 0
    32  }
    33  
    34  // if the diags contains dependency errors, add dependencies to the result
    35  // otherwise add diags to the result
    36  func (p *DecodeResult) handleDecodeDiags(diags hcl.Diagnostics) {
    37  	for _, diag := range diags {
    38  		if dependency := diagsToDependency(diag); dependency != nil {
    39  			p.Depends[dependency.String()] = dependency
    40  		}
    41  	}
    42  	// only register errors if there are NOT any missing variables
    43  	if len(p.Depends) == 0 {
    44  		p.addDiags(diags)
    45  	}
    46  }
    47  
    48  // determine whether the diag is a dependency error, and if so, return a dependency object
    49  func diagsToDependency(diag *hcl.Diagnostic) *modconfig.ResourceDependency {
    50  	if helpers.StringSliceContains(missingVariableErrors, diag.Summary) {
    51  		return &modconfig.ResourceDependency{Range: diag.Expression.Range(), Traversals: diag.Expression.Variables()}
    52  	}
    53  	return nil
    54  }
    55  
    56  func (p *DecodeResult) addDiags(diags hcl.Diagnostics) {
    57  	p.Diags = append(p.Diags, diags...)
    58  }