diff options
author | Michael Muré <batolettre@gmail.com> | 2020-11-09 00:34:48 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2021-02-14 12:17:47 +0100 |
commit | 2bf2b2d765c5003307544885b9321b32cc09d8bb (patch) | |
tree | 3832cad40ca8d19faf08a947e4f09fa004762f2d /entity | |
parent | b01aa18d3925a23ba0ad32a322617de7dc9a299e (diff) | |
download | git-bug-2bf2b2d765c5003307544885b9321b32cc09d8bb.tar.gz |
entity: unique function to generate IDs
Diffstat (limited to 'entity')
-rw-r--r-- | entity/id.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/entity/id.go b/entity/id.go index 08916987..9e724012 100644 --- a/entity/id.go +++ b/entity/id.go @@ -1,6 +1,7 @@ package entity import ( + "crypto/sha256" "fmt" "io" "strings" @@ -17,6 +18,15 @@ const UnsetId = Id("unset") // Id is an identifier for an entity or part of an entity type Id string +// DeriveId generate an Id from some data, taken from a root part of the entity. +func DeriveId(data []byte) Id { + // My understanding is that sha256 is enough to prevent collision (git use that, so ...?) + // If you read this code, I'd be happy to be schooled. + + sum := sha256.Sum256(data) + return Id(fmt.Sprintf("%x", sum)) +} + // String return the identifier as a string func (i Id) String() string { return string(i) @@ -55,6 +65,10 @@ func (i Id) MarshalGQL(w io.Writer) { // IsValid tell if the Id is valid func (i Id) Validate() error { + // Special case to + if len(i) == 40 { + return fmt.Errorf("outdated repository format, please use https://github.com/MichaelMure/git-bug-migration to upgrade") + } if len(i) != idLength { return fmt.Errorf("invalid length") } |