github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/common/cyclonedxhelpers/properties.go (about)

     1  package cyclonedxhelpers
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/CycloneDX/cyclonedx-go"
     7  
     8  	"github.com/anchore/syft/syft/format/common"
     9  )
    10  
    11  var (
    12  	CycloneDXFields = common.RequiredTag("cyclonedx")
    13  )
    14  
    15  func encodeProperties(obj interface{}, prefix string) (out []cyclonedx.Property) {
    16  	for _, p := range common.Sorted(common.Encode(obj, prefix, CycloneDXFields)) {
    17  		out = append(out, cyclonedx.Property{
    18  			Name:  p.Name,
    19  			Value: p.Value,
    20  		})
    21  	}
    22  	return
    23  }
    24  
    25  func decodeProperties(properties []cyclonedx.Property, prefix string) map[string]string {
    26  	labels := make(map[string]string)
    27  	for _, property := range properties {
    28  		if strings.HasPrefix(property.Name, prefix) {
    29  			labelName := strings.TrimPrefix(property.Name, prefix)
    30  			labels[labelName] = property.Value
    31  		}
    32  	}
    33  	return labels
    34  }