github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/configs/configschema/marks.go (about)

     1  package configschema
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  )
     8  
     9  // ValueMarks returns a set of path value marks for a given value and path,
    10  // based on the sensitive flag for each attribute within the schema. Nested
    11  // blocks are descended (if present in the given value).
    12  func (b *Block) ValueMarks(val cty.Value, path cty.Path) []cty.PathValueMarks {
    13  	var pvm []cty.PathValueMarks
    14  	for name, attrS := range b.Attributes {
    15  		if attrS.Sensitive {
    16  			// Create a copy of the path, with this step added, to add to our PathValueMarks slice
    17  			attrPath := make(cty.Path, len(path), len(path)+1)
    18  			copy(attrPath, path)
    19  			attrPath = append(path, cty.GetAttrStep{Name: name})
    20  			pvm = append(pvm, cty.PathValueMarks{
    21  				Path:  attrPath,
    22  				Marks: cty.NewValueMarks("sensitive"),
    23  			})
    24  		}
    25  	}
    26  
    27  	if val.IsNull() {
    28  		return pvm
    29  	}
    30  	for name, blockS := range b.BlockTypes {
    31  		// If our block doesn't contain any sensitive attributes, skip inspecting it
    32  		if !blockS.Block.ContainsSensitive() {
    33  			continue
    34  		}
    35  
    36  		blockV := val.GetAttr(name)
    37  		if blockV.IsNull() || !blockV.IsKnown() {
    38  			continue
    39  		}
    40  
    41  		// Create a copy of the path, with this step added, to add to our PathValueMarks slice
    42  		blockPath := make(cty.Path, len(path), len(path)+1)
    43  		copy(blockPath, path)
    44  		blockPath = append(path, cty.GetAttrStep{Name: name})
    45  
    46  		switch blockS.Nesting {
    47  		case NestingSingle, NestingGroup:
    48  			pvm = append(pvm, blockS.Block.ValueMarks(blockV, blockPath)...)
    49  		case NestingList, NestingMap, NestingSet:
    50  			for it := blockV.ElementIterator(); it.Next(); {
    51  				idx, blockEV := it.Element()
    52  				morePaths := blockS.Block.ValueMarks(blockEV, append(blockPath, cty.IndexStep{Key: idx}))
    53  				pvm = append(pvm, morePaths...)
    54  			}
    55  		default:
    56  			panic(fmt.Sprintf("unsupported nesting mode %s", blockS.Nesting))
    57  		}
    58  	}
    59  	return pvm
    60  }