github.com/avenga/couper@v1.12.2/config/body/body.go (about)

     1  package body
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/hashicorp/hcl/v2/hclsyntax"
     6  	"github.com/zclconf/go-cty/cty"
     7  )
     8  
     9  func NewHCLSyntaxBodyWithAttr(name string, value cty.Value, rng hcl.Range) *hclsyntax.Body {
    10  	return &hclsyntax.Body{
    11  		Attributes: hclsyntax.Attributes{
    12  			name: {
    13  				Name: name,
    14  				Expr: &hclsyntax.LiteralValueExpr{Val: value, SrcRange: rng},
    15  			},
    16  		},
    17  	}
    18  }
    19  
    20  func NewHCLSyntaxBodyWithStringAttr(name, value string) *hclsyntax.Body {
    21  	return NewHCLSyntaxBodyWithAttr(name, cty.StringVal(value), hcl.Range{})
    22  }
    23  
    24  func MergeBodies(dest, src *hclsyntax.Body, replace bool) *hclsyntax.Body {
    25  	if src == dest {
    26  		return dest
    27  	}
    28  	for k, v := range src.Attributes {
    29  		if _, set := dest.Attributes[k]; replace || !set {
    30  			dest.Attributes[k] = v
    31  		}
    32  	}
    33  	for _, bl := range src.Blocks {
    34  		dest.Blocks = append(dest.Blocks, bl)
    35  	}
    36  	return dest
    37  }
    38  
    39  func BlocksOfType(body *hclsyntax.Body, blockType string) []*hclsyntax.Block {
    40  	var blocks []*hclsyntax.Block
    41  	for _, bl := range body.Blocks {
    42  		if bl.Type == blockType {
    43  			blocks = append(blocks, bl)
    44  		}
    45  	}
    46  	return blocks
    47  }
    48  
    49  func RenameAttribute(body *hclsyntax.Body, old, new string) {
    50  	if attr, ok := body.Attributes[old]; ok {
    51  		attr.Name = new
    52  		body.Attributes[new] = attr
    53  		delete(body.Attributes, old)
    54  	}
    55  }