aboutsummaryrefslogtreecommitdiff
path: root/spdxlib/described_elements.go
diff options
context:
space:
mode:
authorKeith Zantow <kzantow@gmail.com>2022-10-11 13:28:47 -0400
committerKeith Zantow <kzantow@gmail.com>2022-10-11 13:28:47 -0400
commitd326e1063b8265703fae4677e7fd63eb97e2df28 (patch)
tree8fbcb16d0a5147c7d8d6cff1bfb2c86b0b22b2eb /spdxlib/described_elements.go
parent77794935eaa9bc95a1477b73ffb096f0986c1c84 (diff)
downloadspdx-tools-d326e1063b8265703fae4677e7fd63eb97e2df28.tar.gz
chore: update a few missed functions for 2.3
Signed-off-by: Keith Zantow <kzantow@gmail.com>
Diffstat (limited to 'spdxlib/described_elements.go')
-rw-r--r--spdxlib/described_elements.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/spdxlib/described_elements.go b/spdxlib/described_elements.go
index 61833b4..a2a6356 100644
--- a/spdxlib/described_elements.go
+++ b/spdxlib/described_elements.go
@@ -9,6 +9,7 @@ import (
"github.com/spdx/tools-golang/spdx/common"
"github.com/spdx/tools-golang/spdx/v2_1"
"github.com/spdx/tools-golang/spdx/v2_2"
+ "github.com/spdx/tools-golang/spdx/v2_3"
)
// GetDescribedPackageIDs2_1 returns a slice of ElementIDs for all Packages
@@ -112,3 +113,54 @@ func GetDescribedPackageIDs2_2(doc *v2_2.Document) ([]common.ElementID, error) {
return eIDs, nil
}
+
+// GetDescribedPackageIDs2_3 returns a slice of ElementIDs for all Packages
+// in this Document that it "describes," according to SPDX rules:
+// - If the document has only one Package, its ID is returned.
+// - If the document has 2+ Packages, it returns the IDs of those that have
+// a DESCRIBES (or DESCRIBED_BY) relationship to this DOCUMENT.
+func GetDescribedPackageIDs2_3(doc *v2_3.Document) ([]common.ElementID, error) {
+ // if nil Packages map or zero packages in it, return empty slice
+ if doc.Packages == nil {
+ return nil, fmt.Errorf("Packages map is nil")
+ }
+ if len(doc.Packages) == 0 {
+ return nil, fmt.Errorf("no Packages in Document")
+ }
+ if len(doc.Packages) == 1 {
+ // get first (only) one and return its ID
+ for _, pkg := range doc.Packages {
+ return []common.ElementID{pkg.PackageSPDXIdentifier}, nil
+ }
+ }
+
+ // two or more packages, so we need to go through the relationships,
+ // find DESCRIBES or DESCRIBED_BY for this DOCUMENT, verify they are
+ // valid IDs in this document's packages, and return them
+ if doc.Relationships == nil {
+ return nil, fmt.Errorf("multiple Packages in Document but Relationships slice is nil")
+ }
+
+ eIDs, err := FilterRelationships2_3(doc, func(relationship *v2_3.Relationship) *common.ElementID {
+ refDocument := common.MakeDocElementID("", "DOCUMENT")
+
+ if relationship.Relationship == "DESCRIBES" && relationship.RefA == refDocument {
+ return &relationship.RefB.ElementRefID
+ } else if relationship.Relationship == "DESCRIBED_BY" && relationship.RefB == refDocument {
+ return &relationship.RefA.ElementRefID
+ }
+
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ if len(eIDs) == 0 {
+ return nil, fmt.Errorf("no DESCRIBES or DESCRIBED_BY relationships found for this Document")
+ }
+
+ eIDs = SortElementIDs(eIDs)
+
+ return eIDs, nil
+}