2019-02-18 16:11:37 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
2019-02-24 16:17:52 +03:00
|
|
|
"fmt"
|
2019-02-18 16:11:37 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
|
|
|
)
|
|
|
|
|
2019-02-24 16:17:52 +03:00
|
|
|
// Package initialisation used to register the type for (de)serialization
|
|
|
|
func init() {
|
|
|
|
gob.Register(IdentityExcerpt{})
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
// IdentityExcerpt hold a subset of the identity values to be able to sort and
|
|
|
|
// filter identities efficiently without having to read and compile each raw
|
|
|
|
// identity.
|
|
|
|
type IdentityExcerpt struct {
|
|
|
|
Id string
|
|
|
|
|
2019-02-24 15:23:01 +03:00
|
|
|
Name string
|
|
|
|
Login string
|
|
|
|
ImmutableMetadata map[string]string
|
2019-02-18 16:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIdentityExcerpt(i *identity.Identity) *IdentityExcerpt {
|
|
|
|
return &IdentityExcerpt{
|
2019-02-24 15:23:01 +03:00
|
|
|
Id: i.Id(),
|
|
|
|
Name: i.Name(),
|
|
|
|
Login: i.Login(),
|
|
|
|
ImmutableMetadata: i.ImmutableMetadata(),
|
2019-02-18 16:11:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:17:52 +03:00
|
|
|
func (i *IdentityExcerpt) HumanId() string {
|
|
|
|
return identity.FormatHumanID(i.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisplayName return a non-empty string to display, representing the
|
|
|
|
// identity, based on the non-empty values.
|
|
|
|
func (i *IdentityExcerpt) DisplayName() string {
|
|
|
|
switch {
|
|
|
|
case i.Name == "" && i.Login != "":
|
|
|
|
return i.Login
|
|
|
|
case i.Name != "" && i.Login == "":
|
|
|
|
return i.Name
|
|
|
|
case i.Name != "" && i.Login != "":
|
|
|
|
return fmt.Sprintf("%s (%s)", i.Name, i.Login)
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("invalid person data")
|
2019-02-18 16:11:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sorting
|
|
|
|
*/
|
|
|
|
|
|
|
|
type IdentityById []*IdentityExcerpt
|
|
|
|
|
|
|
|
func (b IdentityById) Len() int {
|
|
|
|
return len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b IdentityById) Less(i, j int) bool {
|
|
|
|
return b[i].Id < b[j].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b IdentityById) Swap(i, j int) {
|
|
|
|
b[i], b[j] = b[j], b[i]
|
|
|
|
}
|