identity_excerpt.go

 1package cache
 2
 3import (
 4	"encoding/gob"
 5	"fmt"
 6
 7	"github.com/MichaelMure/git-bug/identity"
 8)
 9
10// Package initialisation used to register the type for (de)serialization
11func init() {
12	gob.Register(IdentityExcerpt{})
13}
14
15// IdentityExcerpt hold a subset of the identity values to be able to sort and
16// filter identities efficiently without having to read and compile each raw
17// identity.
18type IdentityExcerpt struct {
19	Id string
20
21	Name              string
22	Login             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		Login:             i.Login(),
31		ImmutableMetadata: i.ImmutableMetadata(),
32	}
33}
34
35func (i *IdentityExcerpt) HumanId() string {
36	return identity.FormatHumanID(i.Id)
37}
38
39// DisplayName return a non-empty string to display, representing the
40// identity, based on the non-empty values.
41func (i *IdentityExcerpt) DisplayName() string {
42	switch {
43	case i.Name == "" && i.Login != "":
44		return i.Login
45	case i.Name != "" && i.Login == "":
46		return i.Name
47	case i.Name != "" && i.Login != "":
48		return fmt.Sprintf("%s (%s)", i.Name, i.Login)
49	}
50
51	panic("invalid person data")
52}
53
54/*
55 * Sorting
56 */
57
58type IdentityById []*IdentityExcerpt
59
60func (b IdentityById) Len() int {
61	return len(b)
62}
63
64func (b IdentityById) Less(i, j int) bool {
65	return b[i].Id < b[j].Id
66}
67
68func (b IdentityById) Swap(i, j int) {
69	b[i], b[j] = b[j], b[i]
70}