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