aboutsummaryrefslogtreecommitdiff
path: root/spdxlib/described_elements.go
diff options
context:
space:
mode:
authorIan Ling <ian@iancaling.com>2021-06-15 14:59:52 -0700
committerIan Ling <ian@iancaling.com>2021-06-16 11:28:39 -0700
commit9b060e5ac8c4614be5fd5f5e14561e8c4dff9ef2 (patch)
tree82ad0ddae5914e167ababa0188d4352320d8ab87 /spdxlib/described_elements.go
parent8574b91809e949442fa0cbf3174d9ae83cd69f60 (diff)
downloadspdx-tools-9b060e5ac8c4614be5fd5f5e14561e8c4dff9ef2.tar.gz
Add relationship filter function
Signed-off-by: Ian Ling <ian@iancaling.com>
Diffstat (limited to 'spdxlib/described_elements.go')
-rw-r--r--spdxlib/described_elements.go104
1 files changed, 38 insertions, 66 deletions
diff --git a/spdxlib/described_elements.go b/spdxlib/described_elements.go
index 6158f88..e8373da 100644
--- a/spdxlib/described_elements.go
+++ b/spdxlib/described_elements.go
@@ -5,8 +5,6 @@ package spdxlib
import (
"fmt"
- "sort"
-
"github.com/spdx/tools-golang/spdx"
)
@@ -14,8 +12,7 @@ import (
// 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. If no
-// -
+// a DESCRIBES (or DESCRIBED_BY) relationship to this DOCUMENT.
func GetDescribedPackageIDs2_1(doc *spdx.Document2_1) ([]spdx.ElementID, error) {
// if nil Packages map or zero packages in it, return empty slice
if doc.Packages == nil {
@@ -37,40 +34,28 @@ func GetDescribedPackageIDs2_1(doc *spdx.Document2_1) ([]spdx.ElementID, error)
if doc.Relationships == nil {
return nil, fmt.Errorf("multiple Packages in Document but Relationships slice is nil")
}
- // collect IDs as strings so we can sort them easily
- eIDStrs := []string{}
- for _, rln := range doc.Relationships {
- if rln.Relationship == "DESCRIBES" && rln.RefA == spdx.MakeDocElementID("", "DOCUMENT") {
- // confirm RefB is actually a package in this document
- if _, ok := doc.Packages[rln.RefB.ElementRefID]; !ok {
- // if it's an unpackaged file, that's valid (no error) but don't return it
- if _, ok2 := doc.UnpackagedFiles[rln.RefB.ElementRefID]; !ok2 {
- return nil, fmt.Errorf("Document DESCRIBES %s but no such Package or unpackaged File", string(rln.RefB.ElementRefID))
- }
- }
- eIDStrs = append(eIDStrs, string(rln.RefB.ElementRefID))
- }
- if rln.Relationship == "DESCRIBED_BY" && rln.RefB == spdx.MakeDocElementID("", "DOCUMENT") {
- // confirm RefA is actually a package in this document
- // if it's an unpackaged file, that's valid (no error) but don't return it
- if _, ok := doc.Packages[rln.RefA.ElementRefID]; !ok {
- // if it's an unpackaged file, that's valid (no error) but don't return it
- if _, ok2 := doc.UnpackagedFiles[rln.RefA.ElementRefID]; !ok2 {
- return nil, fmt.Errorf("%s DESCRIBED_BY Document but no such Package or unpackaged File", string(rln.RefA.ElementRefID))
- }
- }
- eIDStrs = append(eIDStrs, string(rln.RefA.ElementRefID))
+
+ eIDs, err := FilterRelationships2_1(doc, func(relationship *spdx.Relationship2_1) *spdx.ElementID {
+ refDocument := spdx.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(eIDStrs) == 0 {
+
+ if len(eIDs) == 0 {
return nil, fmt.Errorf("no DESCRIBES or DESCRIBED_BY relationships found for this Document")
}
- // sort them, convert back to ElementIDs and return
- sort.Strings(eIDStrs)
- eIDs := []spdx.ElementID{}
- for _, eIDStr := range eIDStrs {
- eIDs = append(eIDs, spdx.ElementID(eIDStr))
- }
+
+ eIDs = SortElementIDs(eIDs)
+
return eIDs, nil
}
@@ -78,8 +63,7 @@ func GetDescribedPackageIDs2_1(doc *spdx.Document2_1) ([]spdx.ElementID, error)
// 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. If no
-// -
+// a DESCRIBES (or DESCRIBED_BY) relationship to this DOCUMENT.
func GetDescribedPackageIDs2_2(doc *spdx.Document2_2) ([]spdx.ElementID, error) {
// if nil Packages map or zero packages in it, return empty slice
if doc.Packages == nil {
@@ -101,39 +85,27 @@ func GetDescribedPackageIDs2_2(doc *spdx.Document2_2) ([]spdx.ElementID, error)
if doc.Relationships == nil {
return nil, fmt.Errorf("multiple Packages in Document but Relationships slice is nil")
}
- // collect IDs as strings so we can sort them easily
- eIDStrs := []string{}
- for _, rln := range doc.Relationships {
- if rln.Relationship == "DESCRIBES" && rln.RefA == spdx.MakeDocElementID("", "DOCUMENT") {
- // confirm RefB is actually a package in this document
- if _, ok := doc.Packages[rln.RefB.ElementRefID]; !ok {
- // if it's an unpackaged file, that's valid (no error) but don't return it
- if _, ok2 := doc.UnpackagedFiles[rln.RefB.ElementRefID]; !ok2 {
- return nil, fmt.Errorf("Document DESCRIBES %s but no such Package or unpackaged File", string(rln.RefB.ElementRefID))
- }
- }
- eIDStrs = append(eIDStrs, string(rln.RefB.ElementRefID))
- }
- if rln.Relationship == "DESCRIBED_BY" && rln.RefB == spdx.MakeDocElementID("", "DOCUMENT") {
- // confirm RefA is actually a package in this document
- // if it's an unpackaged file, that's valid (no error) but don't return it
- if _, ok := doc.Packages[rln.RefA.ElementRefID]; !ok {
- // if it's an unpackaged file, that's valid (no error) but don't return it
- if _, ok2 := doc.UnpackagedFiles[rln.RefA.ElementRefID]; !ok2 {
- return nil, fmt.Errorf("%s DESCRIBED_BY Document but no such Package or unpackaged File", string(rln.RefA.ElementRefID))
- }
- }
- eIDStrs = append(eIDStrs, string(rln.RefA.ElementRefID))
+
+ eIDs, err := FilterRelationships2_2(doc, func(relationship *spdx.Relationship2_2) *spdx.ElementID {
+ refDocument := spdx.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(eIDStrs) == 0 {
+
+ if len(eIDs) == 0 {
return nil, fmt.Errorf("no DESCRIBES or DESCRIBED_BY relationships found for this Document")
}
- // sort them, convert back to ElementIDs and return
- sort.Strings(eIDStrs)
- eIDs := []spdx.ElementID{}
- for _, eIDStr := range eIDStrs {
- eIDs = append(eIDs, spdx.ElementID(eIDStr))
- }
+
+ eIDs = SortElementIDs(eIDs)
+
return eIDs, nil
}