From d2483d83dd52365741f51eca106aa18c4e8d6420 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 16 Feb 2019 17:32:30 +0100 Subject: identity: I can compile again !! --- identity/identity.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'identity/identity.go') diff --git a/identity/identity.go b/identity/identity.go index 725362f9..a0800bcd 100644 --- a/identity/identity.go +++ b/identity/identity.go @@ -241,15 +241,7 @@ func (i *Identity) AddVersion(version *Version) { func (i *Identity) Commit(repo repository.Repo) error { // Todo: check for mismatch between memory and commited data - needCommit := false - for _, v := range i.versions { - if v.commitHash == "" { - needCommit = true - break - } - } - - if !needCommit { + if !i.NeedCommit() { return fmt.Errorf("can't commit an identity with no pending version") } @@ -313,6 +305,23 @@ func (i *Identity) Commit(repo repository.Repo) error { return nil } +func (i *Identity) CommitAsNeeded(repo repository.Repo) error { + if !i.NeedCommit() { + return nil + } + return i.Commit(repo) +} + +func (i *Identity) NeedCommit() bool { + for _, v := range i.versions { + if v.commitHash == "" { + return true + } + } + + return false +} + // Merge will merge a different version of the same Identity // // To make sure that an Identity history can't be altered, a strict fast-forward @@ -379,6 +388,10 @@ func (i *Identity) Merge(repo repository.Repo, other *Identity) (bool, error) { func (i *Identity) Validate() error { lastTime := lamport.Time(0) + if len(i.versions) == 0 { + return fmt.Errorf("no version") + } + for _, v := range i.versions { if err := v.Validate(); err != nil { return err @@ -391,6 +404,11 @@ func (i *Identity) Validate() error { lastTime = v.time } + // The identity ID should be the hash of the first commit + if i.versions[0].commitHash != "" && string(i.versions[0].commitHash) != i.id { + return fmt.Errorf("identity id should be the first commit hash") + } + return nil } -- cgit