aboutsummaryrefslogtreecommitdiffstats
path: root/cache/identity_excerpt.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-03-01 23:17:57 +0100
committerGitHub <noreply@github.com>2019-03-01 23:17:57 +0100
commit7260ca05bc3588c0572887a7d8f1b897c7fc13da (patch)
tree66854358df3cb9de651f7688556ec5a4b8ab1868 /cache/identity_excerpt.go
parent0aefae6fcca5786f2c898029c3d6282f760f2c63 (diff)
parentb6bed784e5664819250aac20b2b9690879ee6ab1 (diff)
downloadgit-bug-7260ca05bc3588c0572887a7d8f1b897c7fc13da.tar.gz
Merge pull request #89 from MichaelMure/identity
WIP identity in git
Diffstat (limited to 'cache/identity_excerpt.go')
-rw-r--r--cache/identity_excerpt.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/cache/identity_excerpt.go b/cache/identity_excerpt.go
new file mode 100644
index 00000000..2a13bc60
--- /dev/null
+++ b/cache/identity_excerpt.go
@@ -0,0 +1,70 @@
+package cache
+
+import (
+ "encoding/gob"
+ "fmt"
+
+ "github.com/MichaelMure/git-bug/identity"
+)
+
+// Package initialisation used to register the type for (de)serialization
+func init() {
+ gob.Register(IdentityExcerpt{})
+}
+
+// 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
+
+ Name string
+ Login string
+ ImmutableMetadata map[string]string
+}
+
+func NewIdentityExcerpt(i *identity.Identity) *IdentityExcerpt {
+ return &IdentityExcerpt{
+ Id: i.Id(),
+ Name: i.Name(),
+ Login: i.Login(),
+ ImmutableMetadata: i.ImmutableMetadata(),
+ }
+}
+
+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")
+}
+
+/*
+ * 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]
+}