github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/artifact/id.go (about)

     1  package artifact
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/hashstructure/v2"
     7  )
     8  
     9  // ID represents a unique value for each package added to a package catalog.
    10  type ID string
    11  
    12  type Identifiable interface {
    13  	ID() ID
    14  }
    15  
    16  func IDByHash(obj interface{}) (ID, error) {
    17  	f, err := hashstructure.Hash(obj, hashstructure.FormatV2, &hashstructure.HashOptions{
    18  		ZeroNil:      true,
    19  		SlicesAsSets: true,
    20  	})
    21  	if err != nil {
    22  		return "", fmt.Errorf("could not build ID for object=%+v: %w", obj, err)
    23  	}
    24  
    25  	return ID(fmt.Sprintf("%016x", f)), nil
    26  }