🗂️ CPE Set
The set module provides CPESet, a collection of unique CPEs keyed by their URI, with set-algebra operations (union, intersection, difference), filtering (basic and advanced), sorting, relation checks (subset/superset/equal), and related-CPE discovery. The standalone FromArray constructor builds a set from a slice.
Type: CPESet
type CPESet struct {
items map[string]*CPE // keyed by URI
Name string
Description string
}A set of unique CPEs. Uniqueness is based on the CPE URI (GetURI). The items map is unexported; use the provided methods to read and mutate the set.
🆕 NewCPESet
func NewCPESet(name string, description string) *CPESetCreates an empty CPESet with the given name and description.
| Parameter | Type | Description |
|---|---|---|
name | string | Set name |
description | string | Set description |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new empty set |
ms := cpeskills.NewCPESet("Microsoft Products", "Collection of Microsoft CPEs")➕ Add
func (s *CPESet) Add(cpe *CPE)Adds a CPE to the set. A nil CPE or one with an empty URI is ignored. Adding an already-present CPE (same URI) has no effect.
| Parameter | Type | Description |
|---|---|---|
cpe | *CPE | The CPE to add |
windows := cpeskills.MustParse("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")
ms.Add(windows)➖ Remove
func (s *CPESet) Remove(cpe *CPE) boolRemoves a CPE from the set. Returns true if the CPE was present and removed, false otherwise (including nil or empty-URI input).
| Parameter | Type | Description |
|---|---|---|
cpe | *CPE | The CPE to remove |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if removed, false if not present |
removed := ms.Remove(windows)
fmt.Println(removed) // true❓ Contains
func (s *CPESet) Contains(cpe *CPE) boolReports whether the set contains a CPE with the same URI. Returns false for nil or empty-URI input.
| Parameter | Type | Description |
|---|---|---|
cpe | *CPE | The CPE to look up |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if present, false otherwise |
fmt.Println(ms.Contains(windows)) // true🔢 Size
func (s *CPESet) Size() intReturns the number of CPEs in the set.
| Return | Type | Description |
|---|---|---|
| #1 | int | The set size |
fmt.Println(ms.Size())🧹 Clear
func (s *CPESet) Clear()Removes all CPEs from the set, leaving it empty.
ms.Clear()
fmt.Println(ms.Size()) // 0∪ Union
func (s *CPESet) Union(other *CPESet) *CPESetReturns a new set containing every CPE in either s or other.
| Parameter | Type | Description |
|---|---|---|
other | *CPESet | The other set |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new set that is the union |
all := ms.Union(appleSet)
fmt.Println(all.Size())∩ Intersection
func (s *CPESet) Intersection(other *CPESet) *CPESetReturns a new set containing only the CPEs present in both s and other. Iterates the smaller set for efficiency.
| Parameter | Type | Description |
|---|---|---|
other | *CPESet | The other set |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new set that is the intersection |
vulnWindows := windowsSet.Intersection(vulnerableSet)∖ Difference
func (s *CPESet) Difference(other *CPESet) *CPESetReturns a new set containing the CPEs in s that are not in other.
| Parameter | Type | Description |
|---|---|---|
other | *CPESet | The set to subtract |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new set that is the difference s \ other |
supported := allWindowsSet.Difference(outdatedSet)🔍 Filter
func (s *CPESet) Filter(criteria *CPE, options *MatchOptions) *CPESetReturns a new set containing the CPEs in s that match criteria under options (using the same matching logic as Search). options == nil uses default options.
| Parameter | Type | Description |
|---|---|---|
criteria | *CPE | The filter criteria |
options | *MatchOptions | Match options, nil for defaults |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new filtered set |
criteria := &cpeskills.CPE{
Vendor: cpeskills.Vendor("microsoft"),
ProductName: cpeskills.Product("windows"),
}
windows := all.Filter(criteria, nil)🎛️ AdvancedFilter
func (s *CPESet) AdvancedFilter(criteria *CPE, options *AdvancedMatchOptions) *CPESetReturns a new set containing the CPEs in s that match criteria under the advanced options (via AdvancedMatchCPE). options == nil uses default advanced options.
| Parameter | Type | Description |
|---|---|---|
criteria | *CPE | The filter criteria |
options | *AdvancedMatchOptions | Advanced options, nil for defaults |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new advanced-filtered set |
opts := cpeskills.NewAdvancedMatchOptions()
opts.MatchMode = "distance"
opts.ScoreThreshold = 0.7
related := all.AdvancedFilter(criteria, opts)📋 ToSlice
func (s *CPESet) ToSlice() []*CPEReturns a slice of all CPEs in the set. Order is not guaranteed (map iteration order).
| Return | Type | Description |
|---|---|---|
| #1 | []*CPE | All CPEs in the set |
for _, c := range ms.ToSlice() {
fmt.Println(c.GetURI())
}↕️ Sort
func (s *CPESet) Sort(sortBy string, ascending bool) []*CPEReturns a sorted slice of the set's CPEs. The set itself is not modified. sortBy selects the field: "part", "vendor", "product", "version" (compared via the versions package), or any other value (defaults to the 2.3 URI). ascending controls direction.
| Parameter | Type | Description |
|---|---|---|
sortBy | string | Sort field: part/vendor/product/version/other(URI) |
ascending | bool | true ascending, false descending |
| Return | Type | Description |
|---|---|---|
| #1 | []*CPE | The sorted CPE slice |
byProduct := ms.Sort("product", true)
byVersionDesc := ms.Sort("version", false)🟰 Equals
func (s *CPESet) Equals(other *CPESet) boolReports whether s and other contain exactly the same CPEs (same URIs). Returns false when sizes differ.
| Parameter | Type | Description |
|---|---|---|
other | *CPESet | The set to compare with |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if equal, false otherwise |
fmt.Println(set1.Equals(set2))⊆ IsSubsetOf
func (s *CPESet) IsSubsetOf(other *CPESet) boolReports whether every CPE in s is also in other. Returns false early when s is larger than other.
| Parameter | Type | Description |
|---|---|---|
other | *CPESet | The candidate superset |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if s ⊆ other, false otherwise |
fmt.Println(windows10Set.IsSubsetOf(windowsSet))⊇ IsSupersetOf
func (s *CPESet) IsSupersetOf(other *CPESet) boolReports whether every CPE in other is also in s. Implemented as other.IsSubsetOf(s).
| Parameter | Type | Description |
|---|---|---|
other | *CPESet | The candidate subset |
| Return | Type | Description |
|---|---|---|
| #1 | bool | true if s ⊇ other, false otherwise |
fmt.Println(windowsSet.IsSupersetOf(windows10Set))📝 ToString
func (s *CPESet) ToString() stringReturns a multi-line textual summary: the set name, description, size, and each CPE's 2.3 URI (sorted by URI for stable output).
| Return | Type | Description |
|---|---|---|
| #1 | string | The set's string representation |
fmt.Println(ms.ToString())🔗 FindRelated
func (s *CPESet) FindRelated(cpe *CPE, options *AdvancedMatchOptions) *CPESetReturns a new set of CPEs related to cpe, using distance-mode advanced matching with a relaxed threshold (MatchMode = "distance", ScoreThreshold = 0.6). When options is nil, default advanced options are used and then overridden with the relaxed settings. Any options you pass is mutated in place to set MatchMode and ScoreThreshold.
| Parameter | Type | Description |
|---|---|---|
cpe | *CPE | The reference CPE |
options | *AdvancedMatchOptions | Advanced options, nil for defaults |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new set of related CPEs |
windows10 := cpeskills.MustParse("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")
related := all.FindRelated(windows10, nil)
fmt.Printf("%d related CPEs\n", related.Size())🏗️ FromArray
func FromArray(cpes []*CPE, name string, description string) *CPESetCreates a new set from a CPE slice, adding each element. nil entries in the slice are ignored by Add.
| Parameter | Type | Description |
|---|---|---|
cpes | []*CPE | The CPEs to seed the set with |
name | string | Set name |
description | string | Set description |
| Return | Type | Description |
|---|---|---|
| #1 | *CPESet | A new set containing the given CPEs |
set := cpeskills.FromArray([]*cpeskills.CPE{w10, w11}, "Microsoft", "Windows set")