xref: /aosp_15_r20/external/spdx-tools/spdxlib/relationships.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package spdxlib
4
5import (
6	"github.com/spdx/tools-golang/spdx/common"
7	"github.com/spdx/tools-golang/spdx/v2_1"
8	"github.com/spdx/tools-golang/spdx/v2_2"
9	"github.com/spdx/tools-golang/spdx/v2_3"
10)
11
12// FilterRelationships2_1 returns a slice of Element IDs returned by the given filter closure. The closure is passed
13// one relationship at a time, and it can return an ElementID or nil.
14func FilterRelationships2_1(doc *v2_1.Document, filter func(*v2_1.Relationship) *common.ElementID) ([]common.ElementID, error) {
15	elementIDs := []common.ElementID{}
16
17	for _, relationship := range doc.Relationships {
18		if id := filter(relationship); id != nil {
19			elementIDs = append(elementIDs, *id)
20		}
21	}
22
23	return elementIDs, nil
24}
25
26// FilterRelationships2_2 returns a slice of Element IDs returned by the given filter closure. The closure is passed
27// one relationship at a time, and it can return an ElementID or nil.
28func FilterRelationships2_2(doc *v2_2.Document, filter func(*v2_2.Relationship) *common.ElementID) ([]common.ElementID, error) {
29	elementIDs := []common.ElementID{}
30
31	for _, relationship := range doc.Relationships {
32		if id := filter(relationship); id != nil {
33			elementIDs = append(elementIDs, *id)
34		}
35	}
36
37	return elementIDs, nil
38}
39
40// FilterRelationships2_3 returns a slice of Element IDs returned by the given filter closure. The closure is passed
41// one relationship at a time, and it can return an ElementID or nil.
42func FilterRelationships2_3(doc *v2_3.Document, filter func(*v2_3.Relationship) *common.ElementID) ([]common.ElementID, error) {
43	elementIDs := []common.ElementID{}
44
45	for _, relationship := range doc.Relationships {
46		if id := filter(relationship); id != nil {
47			elementIDs = append(elementIDs, *id)
48		}
49	}
50
51	return elementIDs, nil
52}
53