1package cache
 2
 3import (
 4	"encoding/gob"
 5	"strings"
 6
 7	"github.com/MichaelMure/git-bug/entity"
 8	"github.com/MichaelMure/git-bug/identity"
 9)
10
11// Package initialisation used to register the type for (de)serialization
12func init() {
13	gob.Register(IdentityExcerpt{})
14}
15
16// IdentityExcerpt hold a subset of the identity values to be able to sort and
17// filter identities efficiently without having to read and compile each raw
18// identity.
19type IdentityExcerpt struct {
20	Id entity.Id
21
22	Name              string
23	ImmutableMetadata map[string]string
24}
25
26func NewIdentityExcerpt(i *identity.Identity) *IdentityExcerpt {
27	return &IdentityExcerpt{
28		Id:                i.Id(),
29		Name:              i.Name(),
30		ImmutableMetadata: i.ImmutableMetadata(),
31	}
32}
33
34// DisplayName return a non-empty string to display, representing the
35// identity, based on the non-empty values.
36func (i *IdentityExcerpt) DisplayName() string {
37	return i.Name
38}
39
40// Match matches a query with the identity name, login and ID prefixes
41func (i *IdentityExcerpt) Match(query string) bool {
42	return i.Id.HasPrefix(query) ||
43		strings.Contains(strings.ToLower(i.Name), query)
44}
45
46/*
47 * Sorting
48 */
49
50type IdentityById []*IdentityExcerpt
51
52func (b IdentityById) Len() int {
53	return len(b)
54}
55
56func (b IdentityById) Less(i, j int) bool {
57	return b[i].Id < b[j].Id
58}
59
60func (b IdentityById) Swap(i, j int) {
61	b[i], b[j] = b[j], b[i]
62}