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

     1  package body
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/hashicorp/hcl/v2/hclsyntax"
     6  )
     7  
     8  func CollectAttributes(bodies ...hcl.Body) []*hclsyntax.Attribute {
     9  	allAttributes := make([]*hclsyntax.Attribute, 0)
    10  
    11  	for _, b := range bodies {
    12  		sb, _ := b.(*hclsyntax.Body)
    13  		for _, attr := range sb.Attributes {
    14  			allAttributes = append(allAttributes, attr)
    15  		}
    16  
    17  		for _, block := range sb.Blocks {
    18  			allAttributes = append(allAttributes, CollectAttributes(block.Body)...)
    19  		}
    20  	}
    21  
    22  	return allAttributes
    23  }
    24  
    25  func CollectBlockTypes(bodies ...hcl.Body) []string {
    26  	unique := make(map[string]struct{})
    27  
    28  	addUniqueFn := func(types ...string) {
    29  		for _, t := range types {
    30  			if _, exist := unique[t]; exist {
    31  				continue
    32  			}
    33  			unique[t] = struct{}{}
    34  		}
    35  	}
    36  
    37  	for _, b := range bodies {
    38  		sb, _ := b.(*hclsyntax.Body)
    39  		for _, block := range sb.Blocks {
    40  			nested := append(append([]string{}, block.Type), CollectBlockTypes(block.Body)...)
    41  			addUniqueFn(nested...)
    42  		}
    43  	}
    44  
    45  	var result []string
    46  	for u := range unique {
    47  		result = append(result, u)
    48  	}
    49  
    50  	return result
    51  }
    52  
    53  func CollectExpressions(bodies ...hcl.Body) []hclsyntax.Expression {
    54  	allExpressions := make([]hclsyntax.Expression, 0)
    55  	for _, attr := range CollectAttributes(bodies...) {
    56  		allExpressions = append(allExpressions, attr.Expr)
    57  	}
    58  
    59  	return allExpressions
    60  }