mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 22:12:28 +01:00
Otherwise the hash can sometimes be short if it results in a low uint64. Signed-off-by: Will Murphy <will.murphy@anchore.com>
27 lines
533 B
Go
27 lines
533 B
Go
package artifact
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/hashstructure/v2"
|
|
)
|
|
|
|
// ID represents a unique value for each package added to a package catalog.
|
|
type ID string
|
|
|
|
type Identifiable interface {
|
|
ID() ID
|
|
}
|
|
|
|
func IDByHash(obj interface{}) (ID, error) {
|
|
f, err := hashstructure.Hash(obj, hashstructure.FormatV2, &hashstructure.HashOptions{
|
|
ZeroNil: true,
|
|
SlicesAsSets: true,
|
|
})
|
|
if err != nil {
|
|
return "", fmt.Errorf("could not build ID for object=%+v: %w", obj, err)
|
|
}
|
|
|
|
return ID(fmt.Sprintf("%016x", f)), nil
|
|
}
|