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)) result := make([]model.Relationship, len(relationships))
for i, r := range relationships { for i, r := range relationships {
result[i] = model.Relationship{ result[i] = model.Relationship{
Parent: string(r.From), Parent: string(r.From.Identity()),
Child: string(r.To), Child: string(r.To.Identity()),
Type: string(r.Type), Type: string(r.Type),
Metadata: r.Data, Metadata: r.Data,
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -30,6 +30,11 @@ type Package struct {
Metadata interface{} // additional data found while parsing the package source 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. // Stringer to represent a package.
func (p Package) String() string { func (p Package) String() string {
return fmt.Sprintf("Pkg(type=%s, name=%s, version=%s)", p.Type, p.Name, p.Version) return fmt.Sprintf("Pkg(type=%s, name=%s, version=%s)", p.Type, p.Name, p.Version)