aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation.go
diff options
context:
space:
mode:
Diffstat (limited to 'bug/operation.go')
-rw-r--r--bug/operation.go64
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")
}