github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/cpe/merge_cpes.go (about)

     1  package cpe
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  )
     7  
     8  // Merge returns unique SourcedCPEs that are found in A or B
     9  // Two SourcedCPEs are identical if their source and normalized string are identical
    10  func Merge(a, b []CPE) []CPE {
    11  	var result []CPE
    12  	dedupe := make(map[string]CPE)
    13  	key := func(scpe CPE) string {
    14  		return fmt.Sprintf("%s:%s", scpe.Source.String(), scpe.Attributes.BindToFmtString())
    15  	}
    16  	for _, s := range a {
    17  		dedupe[key(s)] = s
    18  	}
    19  	for _, s := range b {
    20  		dedupe[key(s)] = s
    21  	}
    22  	for _, val := range dedupe {
    23  		result = append(result, val)
    24  	}
    25  	sort.Sort(BySourceThenSpecificity(result))
    26  	return result
    27  }