diff options
author | Michael Muré <batolettre@gmail.com> | 2019-03-01 23:17:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-01 23:17:57 +0100 |
commit | 7260ca05bc3588c0572887a7d8f1b897c7fc13da (patch) | |
tree | 66854358df3cb9de651f7688556ec5a4b8ab1868 /bug/operation.go | |
parent | 0aefae6fcca5786f2c898029c3d6282f760f2c63 (diff) | |
parent | b6bed784e5664819250aac20b2b9690879ee6ab1 (diff) | |
download | git-bug-7260ca05bc3588c0572887a7d8f1b897c7fc13da.tar.gz |
Merge pull request #89 from MichaelMure/identity
WIP identity in git
Diffstat (limited to 'bug/operation.go')
-rw-r--r-- | bug/operation.go | 64 |
1 files changed, 53 insertions, 11 deletions
diff --git a/bug/operation.go b/bug/operation.go index 592b5616..cc5b0007 100644 --- a/bug/operation.go +++ b/bug/operation.go @@ -6,6 +6,8 @@ import ( "fmt" "time" + "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/util/git" "github.com/pkg/errors" ) @@ -76,19 +78,19 @@ func hashOperation(op Operation) (git.Hash, error) { // OpBase implement the common code for all operations type OpBase struct { - OperationType OperationType `json:"type"` - Author Person `json:"author"` - UnixTime int64 `json:"timestamp"` - Metadata map[string]string `json:"metadata,omitempty"` + OperationType OperationType + Author identity.Interface + UnixTime int64 + Metadata map[string]string // Not serialized. Store the op's hash in memory. hash git.Hash - // Not serialized. Store the extra metadata compiled from SetMetadataOperation - // in memory. + // Not serialized. Store the extra metadata in memory, + // compiled from SetMetadataOperation. extraMetadata map[string]string } // newOpBase is the constructor for an OpBase -func newOpBase(opType OperationType, author Person, unixTime int64) OpBase { +func newOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase { return OpBase{ OperationType: opType, Author: author, @@ -96,6 +98,46 @@ func newOpBase(opType OperationType, author Person, unixTime int64) OpBase { } } +func (op OpBase) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + OperationType OperationType `json:"type"` + Author identity.Interface `json:"author"` + UnixTime int64 `json:"timestamp"` + Metadata map[string]string `json:"metadata,omitempty"` + }{ + OperationType: op.OperationType, + Author: op.Author, + UnixTime: op.UnixTime, + Metadata: op.Metadata, + }) +} + +func (op *OpBase) UnmarshalJSON(data []byte) error { + aux := struct { + OperationType OperationType `json:"type"` + Author json.RawMessage `json:"author"` + UnixTime int64 `json:"timestamp"` + Metadata map[string]string `json:"metadata,omitempty"` + }{} + + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + + // delegate the decoding of the identity + author, err := identity.UnmarshalJSON(aux.Author) + if err != nil { + return err + } + + op.OperationType = aux.OperationType + op.Author = author + op.UnixTime = aux.UnixTime + op.Metadata = aux.Metadata + + return nil +} + // Time return the time when the operation was added func (op *OpBase) Time() time.Time { return time.Unix(op.UnixTime, 0) @@ -117,14 +159,14 @@ func opBaseValidate(op Operation, opType OperationType) error { return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType) } - if _, err := op.Hash(); err != nil { - return errors.Wrap(err, "op is not serializable") - } - if op.GetUnixTime() == 0 { return fmt.Errorf("time not set") } + if op.base().Author == nil { + return fmt.Errorf("author not set") + } + if err := op.base().Author.Validate(); err != nil { return errors.Wrap(err, "author") } |