kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/configschema/marks.go (about)

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