aboutsummaryrefslogtreecommitdiff
path: root/spdxlib/documents.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/documents.go
parent8574b91809e949442fa0cbf3174d9ae83cd69f60 (diff)
downloadspdx-tools-9b060e5ac8c4614be5fd5f5e14561e8c4dff9ef2.tar.gz
Add relationship filter function
Signed-off-by: Ian Ling <ian@iancaling.com>
Diffstat (limited to 'spdxlib/documents.go')
-rw-r--r--spdxlib/documents.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/spdxlib/documents.go b/spdxlib/documents.go
new file mode 100644
index 0000000..0f90a02
--- /dev/null
+++ b/spdxlib/documents.go
@@ -0,0 +1,66 @@
+package spdxlib
+
+import (
+ "fmt"
+ "github.com/spdx/tools-golang/spdx"
+)
+
+// ValidateDocument2_1 returns an error if the Document is found to be invalid, or nil if the Document is valid.
+// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a
+// Package or an UnpackagedFile.
+func ValidateDocument2_1(doc *spdx.Document2_1) error {
+ // cache a map of valid package IDs for quick lookups
+ validElementIDs := make(map[spdx.ElementID]bool)
+ for _, docPackage := range doc.Packages {
+ validElementIDs[docPackage.PackageSPDXIdentifier] = true
+ }
+
+ for _, unpackagedFile := range doc.UnpackagedFiles {
+ validElementIDs[unpackagedFile.FileSPDXIdentifier] = true
+ }
+
+ // add the Document element ID
+ validElementIDs[spdx.MakeDocElementID("", "DOCUMENT").ElementRefID] = true
+
+ for _, relationship := range doc.Relationships {
+ if !validElementIDs[relationship.RefA.ElementRefID] {
+ return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID))
+ }
+
+ if !validElementIDs[relationship.RefB.ElementRefID] {
+ return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID))
+ }
+ }
+
+ return nil
+}
+
+// ValidateDocument2_2 returns an error if the Document is found to be invalid, or nil if the Document is valid.
+// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a
+// Package or an UnpackagedFile.
+func ValidateDocument2_2(doc *spdx.Document2_2) error {
+ // cache a map of package IDs for quick lookups
+ validElementIDs := make(map[spdx.ElementID]bool)
+ for _, docPackage := range doc.Packages {
+ validElementIDs[docPackage.PackageSPDXIdentifier] = true
+ }
+
+ for _, unpackagedFile := range doc.UnpackagedFiles {
+ validElementIDs[unpackagedFile.FileSPDXIdentifier] = true
+ }
+
+ // add the Document element ID
+ validElementIDs[spdx.MakeDocElementID("", "DOCUMENT").ElementRefID] = true
+
+ for _, relationship := range doc.Relationships {
+ if !validElementIDs[relationship.RefA.ElementRefID] {
+ return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID))
+ }
+
+ if !validElementIDs[relationship.RefB.ElementRefID] {
+ return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID))
+ }
+ }
+
+ return nil
+}