add artifact.Identifiable by Identity() method

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-11-08 16:28:09 -05:00
parent b08a11e46d
commit 69d2b1ba3c
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
6 changed files with 28 additions and 10 deletions

View File

@ -93,8 +93,8 @@ func toRelationshipModel(relationships []artifact.Relationship) []model.Relation
result := make([]model.Relationship, len(relationships))
for i, r := range relationships {
result[i] = model.Relationship{
Parent: string(r.From),
Child: string(r.To),
Parent: string(r.From.Identity()),
Child: string(r.To.Identity()),
Type: string(r.Type),
Metadata: r.Data,
}

View File

@ -2,3 +2,7 @@ package artifact
// ID represents a unique value for each package added to a package catalog.
type ID string
type Identifiable interface {
Identity() ID
}

View File

@ -8,8 +8,8 @@ const (
type RelationshipType string
type Relationship struct {
From ID `json:"from"`
To ID `json:"to"`
From Identifiable `json:"from"`
To Identifiable `json:"to"`
Type RelationshipType `json:"type"`
Data interface{} `json:"data,omitempty"`
}

View File

@ -28,8 +28,8 @@ func ownershipByFilesRelationships(catalog *Catalog) []artifact.Relationship {
for parent, children := range relationships {
for child, files := range children {
edges = append(edges, artifact.Relationship{
From: parent,
To: child,
From: catalog.byID[parent],
To: catalog.byID[child],
Type: artifact.OwnershipByFileOverlapRelationship,
Data: ownershipByFilesMetadata{
Files: files.List(),

View File

@ -10,7 +10,16 @@ import (
"github.com/anchore/syft/syft/source"
)
type node struct {
id string
}
func (n node) Identity() artifact.ID {
return artifact.ID(n.id)
}
func TestOwnershipByFilesRelationship(t *testing.T) {
tests := []struct {
name string
pkgs []Package
@ -58,8 +67,8 @@ func TestOwnershipByFilesRelationship(t *testing.T) {
},
expectedRelations: []artifact.Relationship{
{
From: "parent",
To: "child",
From: node{"parent"},
To: node{"child"},
Type: artifact.OwnershipByFileOverlapRelationship,
Data: ownershipByFilesMetadata{
Files: []string{
@ -111,8 +120,8 @@ func TestOwnershipByFilesRelationship(t *testing.T) {
},
expectedRelations: []artifact.Relationship{
{
From: "parent",
To: "child",
From: node{"parent"},
To: node{"child"},
Type: artifact.OwnershipByFileOverlapRelationship,
Data: ownershipByFilesMetadata{
Files: []string{

View File

@ -30,6 +30,11 @@ type Package struct {
Metadata interface{} // additional data found while parsing the package source
}
func (p Package) Identity() artifact.ID {
// TODO: tie this into the fingerprint system on rebase
return p.ID
}
// Stringer to represent a package.
func (p Package) String() string {
return fmt.Sprintf("Pkg(type=%s, name=%s, version=%s)", p.Type, p.Name, p.Version)