diff options
author | Michael Muré <batolettre@gmail.com> | 2020-01-14 22:01:44 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-02-08 17:18:29 +0100 |
commit | 390b13c9ff69f3d42860b5f8109eb134459c40b1 (patch) | |
tree | fb2bf13f7395f424e38a6fda61771071eb923948 | |
parent | ae2f942ef907161af0aba5f3511db72cf9801dca (diff) | |
download | git-bug-390b13c9ff69f3d42860b5f8109eb134459c40b1.tar.gz |
identity: rework mutation
-rw-r--r-- | cache/identity_cache.go | 4 | ||||
-rw-r--r-- | identity/identity.go | 23 | ||||
-rw-r--r-- | identity/version.go | 12 |
3 files changed, 33 insertions, 6 deletions
diff --git a/cache/identity_cache.go b/cache/identity_cache.go index 2ae55f2d..f9c928fe 100644 --- a/cache/identity_cache.go +++ b/cache/identity_cache.go @@ -21,8 +21,8 @@ func (i *IdentityCache) notifyUpdated() error { return i.repoCache.identityUpdated(i.Identity.Id()) } -func (i *IdentityCache) AddVersion(version *identity.Version) error { - i.Identity.AddVersion(version) +func (i *IdentityCache) Mutate(f func(identity.VersionMutator) identity.VersionMutator) error { + i.Identity.Mutate(f) return i.notifyUpdated() } diff --git a/identity/identity.go b/identity/identity.go index cd47c1b7..a07bf858 100644 --- a/identity/identity.go +++ b/identity/identity.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "reflect" "strings" "time" @@ -271,8 +272,26 @@ func IsUserIdentitySet(repo repository.Repo) (bool, error) { return len(configs) == 1, nil } -func (i *Identity) AddVersion(version *Version) { - i.versions = append(i.versions, version) +// Mutate allow to create a new version of the Identity +func (i *Identity) Mutate(f func(orig VersionMutator) VersionMutator) { + orig := VersionMutator{ + Name: i.Name(), + Email: i.Email(), + Login: i.Login(), + AvatarUrl: i.AvatarUrl(), + Keys: i.Keys(), + } + mutated := f(orig) + if reflect.DeepEqual(orig, mutated) { + return + } + i.versions = append(i.versions, &Version{ + name: mutated.Name, + email: mutated.Email, + login: mutated.Login, + avatarURL: mutated.AvatarUrl, + keys: mutated.Keys, + }) } // Write the identity into the Repository. In particular, this ensure that diff --git a/identity/version.go b/identity/version.go index 95530767..b5b8dae3 100644 --- a/identity/version.go +++ b/identity/version.go @@ -24,8 +24,8 @@ type Version struct { unixTime int64 name string - email string - login string + email string // TODO: remove ? keep and use metadata for bridges ? + login string // TODO: remove avatarURL string // The set of keys valid at that time, from this version onward, until they get removed @@ -60,6 +60,14 @@ type VersionJSON struct { Metadata map[string]string `json:"metadata,omitempty"` } +type VersionMutator struct { + Name string + Email string + Login string + AvatarUrl string + Keys []Key +} + func (v *Version) MarshalJSON() ([]byte, error) { return json.Marshal(VersionJSON{ FormatVersion: formatVersion, |