2019-08-11 15:08:03 +03:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2020-11-09 02:34:48 +03:00
|
|
|
"crypto/sha256"
|
2019-08-11 15:08:03 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-11-08 21:18:44 +03:00
|
|
|
// sha-256
|
|
|
|
const idLength = 64
|
2019-08-11 15:08:03 +03:00
|
|
|
const humanIdLength = 7
|
|
|
|
|
|
|
|
const UnsetId = Id("unset")
|
|
|
|
|
|
|
|
// Id is an identifier for an entity or part of an entity
|
|
|
|
type Id string
|
|
|
|
|
2021-03-14 20:39:04 +03:00
|
|
|
// DeriveId generate an Id from the serialization of the object or part of the object.
|
2020-11-09 02:34:48 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:02:13 +03:00
|
|
|
// String return the identifier as a string
|
2019-08-11 15:08:03 +03:00
|
|
|
func (i Id) String() string {
|
|
|
|
return string(i)
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:02:13 +03:00
|
|
|
// Human return the identifier, shortened for human consumption
|
2019-08-11 15:08:03 +03:00
|
|
|
func (i Id) Human() string {
|
|
|
|
format := fmt.Sprintf("%%.%ds", humanIdLength)
|
|
|
|
return fmt.Sprintf(format, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Id) HasPrefix(prefix string) bool {
|
|
|
|
return strings.HasPrefix(string(i), prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalGQL implement the Unmarshaler interface for gqlgen
|
|
|
|
func (i *Id) UnmarshalGQL(v interface{}) error {
|
|
|
|
_, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("IDs must be strings")
|
|
|
|
}
|
|
|
|
|
|
|
|
*i = v.(Id)
|
|
|
|
|
|
|
|
if err := i.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "invalid ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalGQL implement the Marshaler interface for gqlgen
|
|
|
|
func (i Id) MarshalGQL(w io.Writer) {
|
|
|
|
_, _ = w.Write([]byte(`"` + i.String() + `"`))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid tell if the Id is valid
|
|
|
|
func (i Id) Validate() error {
|
2020-11-30 03:55:30 +03:00
|
|
|
// Special case to detect outdated repo
|
2020-11-09 02:34:48 +03:00
|
|
|
if len(i) == 40 {
|
|
|
|
return fmt.Errorf("outdated repository format, please use https://github.com/MichaelMure/git-bug-migration to upgrade")
|
|
|
|
}
|
2020-11-08 21:18:44 +03:00
|
|
|
if len(i) != idLength {
|
2019-08-11 15:08:03 +03:00
|
|
|
return fmt.Errorf("invalid length")
|
|
|
|
}
|
|
|
|
for _, r := range i {
|
|
|
|
if (r < 'a' || r > 'z') && (r < '0' || r > '9') {
|
|
|
|
return fmt.Errorf("invalid character")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-10 16:46:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sorting
|
|
|
|
*/
|
|
|
|
|
|
|
|
type Alphabetical []Id
|
|
|
|
|
|
|
|
func (a Alphabetical) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Alphabetical) Less(i, j int) bool {
|
|
|
|
return a[i] < a[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Alphabetical) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|