aboutsummaryrefslogtreecommitdiffstats
path: root/identity
diff options
context:
space:
mode:
authorMichael Muré <michael.mure@consensys.net>2019-02-01 12:22:00 +0100
committerMichael Muré <batolettre@gmail.com>2019-03-01 22:40:22 +0100
commit56c6147eb6012252cf0b723b9eb6d1e841fc2f94 (patch)
treecce638adbf4a7d5b424fe9682cafc2fea5c64785 /identity
parent14b240af8fef269d2c1d5dde2fff192b656c50f3 (diff)
downloadgit-bug-56c6147eb6012252cf0b723b9eb6d1e841fc2f94.tar.gz
identity: more refactoring progress
Diffstat (limited to 'identity')
-rw-r--r--identity/common.go15
-rw-r--r--identity/identity.go54
-rw-r--r--identity/identity_stub.go85
-rw-r--r--identity/identity_test.go33
-rw-r--r--identity/resolver.go22
5 files changed, 160 insertions, 49 deletions
diff --git a/identity/common.go b/identity/common.go
index 5301471a..00feaa2d 100644
--- a/identity/common.go
+++ b/identity/common.go
@@ -23,12 +23,13 @@ func (e ErrMultipleMatch) Error() string {
//
// If the given message has a "id" field, it's considered being a proper Identity.
func UnmarshalJSON(raw json.RawMessage) (Interface, error) {
- // First try to decode as a normal Identity
- var i Identity
+ aux := &IdentityStub{}
- err := json.Unmarshal(raw, &i)
- if err == nil && i.id != "" {
- return &i, nil
+ // First try to decode and load as a normal Identity
+ err := json.Unmarshal(raw, &aux)
+ if err == nil && aux.Id() != "" {
+ return aux, nil
+ // return identityResolver.ResolveIdentity(aux.Id)
}
// abort if we have an error other than the wrong type
@@ -51,7 +52,3 @@ func UnmarshalJSON(raw json.RawMessage) (Interface, error) {
return nil, fmt.Errorf("unknown identity type")
}
-
-type Resolver interface {
- ResolveIdentity(id string) (Interface, error)
-}
diff --git a/identity/identity.go b/identity/identity.go
index 2a422789..2dafb353 100644
--- a/identity/identity.go
+++ b/identity/identity.go
@@ -48,14 +48,10 @@ func NewIdentityFull(name string, email string, login string, avatarUrl string)
}
}
-type identityJSON struct {
- Id string `json:"id"`
-}
-
// MarshalJSON will only serialize the id
func (i *Identity) MarshalJSON() ([]byte, error) {
- return json.Marshal(identityJSON{
- Id: i.Id(),
+ return json.Marshal(&IdentityStub{
+ id: i.Id(),
})
}
@@ -63,35 +59,12 @@ func (i *Identity) MarshalJSON() ([]byte, error) {
// Users of this package are expected to run Load() to load
// the remaining data from the identities data in git.
func (i *Identity) UnmarshalJSON(data []byte) error {
- aux := identityJSON{}
-
- if err := json.Unmarshal(data, &aux); err != nil {
- return err
- }
-
- i.id = aux.Id
-
- return nil
+ panic("identity should be loaded with identity.UnmarshalJSON")
}
// Read load an Identity from the identities data available in git
func Read(repo repository.Repo, id string) (*Identity, error) {
- i := &Identity{
- id: id,
- }
-
- err := i.Load(repo)
- if err != nil {
- return nil, err
- }
-
- return i, nil
-}
-
-// Load will read the corresponding identity data from git and replace any
-// data already loaded if any.
-func (i *Identity) Load(repo repository.Repo) error {
- ref := fmt.Sprintf("%s%s", identityRefPattern, i.Id())
+ ref := fmt.Sprintf("%s%s", identityRefPattern, id)
hashes, err := repo.ListCommits(ref)
@@ -99,35 +72,35 @@ func (i *Identity) Load(repo repository.Repo) error {
// TODO: this is not perfect, it might be a command invoke error
if err != nil {
- return ErrIdentityNotExist
+ return nil, ErrIdentityNotExist
}
for _, hash := range hashes {
entries, err := repo.ListEntries(hash)
if err != nil {
- return errors.Wrap(err, "can't list git tree entries")
+ return nil, errors.Wrap(err, "can't list git tree entries")
}
if len(entries) != 1 {
- return fmt.Errorf("invalid identity data at hash %s", hash)
+ return nil, fmt.Errorf("invalid identity data at hash %s", hash)
}
entry := entries[0]
if entry.Name != versionEntryName {
- return fmt.Errorf("invalid identity data at hash %s", hash)
+ return nil, fmt.Errorf("invalid identity data at hash %s", hash)
}
data, err := repo.ReadData(entry.Hash)
if err != nil {
- return errors.Wrap(err, "failed to read git blob data")
+ return nil, errors.Wrap(err, "failed to read git blob data")
}
var version Version
err = json.Unmarshal(data, &version)
if err != nil {
- return errors.Wrapf(err, "failed to decode Identity version json %s", hash)
+ return nil, errors.Wrapf(err, "failed to decode Identity version json %s", hash)
}
// tag the version with the commit hash
@@ -136,9 +109,10 @@ func (i *Identity) Load(repo repository.Repo) error {
versions = append(versions, &version)
}
- i.Versions = versions
-
- return nil
+ return &Identity{
+ id: id,
+ Versions: versions,
+ }, nil
}
// NewFromGitUser will query the repository for user detail and
diff --git a/identity/identity_stub.go b/identity/identity_stub.go
new file mode 100644
index 00000000..0163e9d4
--- /dev/null
+++ b/identity/identity_stub.go
@@ -0,0 +1,85 @@
+package identity
+
+import (
+ "encoding/json"
+
+ "github.com/MichaelMure/git-bug/repository"
+ "github.com/MichaelMure/git-bug/util/lamport"
+)
+
+var _ Interface = &IdentityStub{}
+
+// IdentityStub is an almost empty Identity, holding only the id.
+// When a normal Identity is serialized into JSON, only the id is serialized.
+// All the other data are stored in git in a chain of commit + a ref.
+// When this JSON is deserialized, an IdentityStub is returned instead, to be replaced
+// later by the proper Identity, loaded from the Repo.
+type IdentityStub struct {
+ id string
+}
+
+func (i *IdentityStub) MarshalJSON() ([]byte, error) {
+ return json.Marshal(struct {
+ Id string `json:"id"`
+ }{
+ Id: i.id,
+ })
+}
+
+func (i *IdentityStub) UnmarshalJSON(data []byte) error {
+ aux := struct {
+ Id string `json:"id"`
+ }{}
+
+ if err := json.Unmarshal(data, &aux); err != nil {
+ return err
+ }
+
+ i.id = aux.Id
+
+ return nil
+}
+
+func (i *IdentityStub) Id() string {
+ return i.id
+}
+
+func (IdentityStub) Name() string {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) Email() string {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) Login() string {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) AvatarUrl() string {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) Keys() []Key {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) ValidKeysAtTime(time lamport.Time) []Key {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) DisplayName() string {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) Validate() error {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) Commit(repo repository.Repo) error {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
+
+func (IdentityStub) IsProtected() bool {
+ panic("identities needs to be properly loaded with identity.Read()")
+}
diff --git a/identity/identity_test.go b/identity/identity_test.go
index afb804fc..f1c07e79 100644
--- a/identity/identity_test.go
+++ b/identity/identity_test.go
@@ -1,6 +1,7 @@
package identity
import (
+ "encoding/json"
"testing"
"github.com/MichaelMure/git-bug/repository"
@@ -220,3 +221,35 @@ func assertHasKeyValue(t *testing.T, metadata map[string]string, key, value stri
assert.True(t, ok)
assert.Equal(t, val, value)
}
+
+func TestJSON(t *testing.T) {
+ mockRepo := repository.NewMockRepoForTest()
+
+ identity := &Identity{
+ Versions: []*Version{
+ {
+ Name: "René Descartes",
+ Email: "rene.descartes@example.com",
+ },
+ },
+ }
+
+ // commit to make sure we have an ID
+ err := identity.Commit(mockRepo)
+ assert.Nil(t, err)
+ assert.NotEmpty(t, identity.id)
+
+ // serialize
+ data, err := json.Marshal(identity)
+ assert.NoError(t, err)
+
+ // deserialize, got a IdentityStub with the same id
+ var i Interface
+ i, err = UnmarshalJSON(data)
+ assert.NoError(t, err)
+ assert.Equal(t, identity.id, i.Id())
+
+ // make sure we can load the identity properly
+ i, err = Read(mockRepo, i.Id())
+ assert.NoError(t, err)
+}
diff --git a/identity/resolver.go b/identity/resolver.go
new file mode 100644
index 00000000..63dc994f
--- /dev/null
+++ b/identity/resolver.go
@@ -0,0 +1,22 @@
+package identity
+
+import "github.com/MichaelMure/git-bug/repository"
+
+// Resolver define the interface of an Identity resolver, able to load
+// an identity from, for example, a repo or a cache.
+type Resolver interface {
+ ResolveIdentity(id string) (Interface, error)
+}
+
+// DefaultResolver is a Resolver loading Identities directly from a Repo
+type SimpleResolver struct {
+ repo repository.Repo
+}
+
+func NewSimpleResolver(repo repository.Repo) *SimpleResolver {
+ return &SimpleResolver{repo: repo}
+}
+
+func (r *SimpleResolver) ResolveIdentity(id string) (Interface, error) {
+ return Read(r.repo, id)
+}