aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-01-19 19:23:31 +0100
committerMichael Muré <batolettre@gmail.com>2019-03-01 22:40:21 +0100
commitd10c76469d40f13e27739fd363145e89bf74c3e0 (patch)
treeff613ef1ce9400f8c208d3381bd703b186958aa3 /bug/operation.go
parent844616baf8dc628360942d57fd69f24e298e08da (diff)
downloadgit-bug-d10c76469d40f13e27739fd363145e89bf74c3e0.tar.gz
identity: somewhat getting closer !
Diffstat (limited to 'bug/operation.go')
-rw-r--r--bug/operation.go36
1 files changed, 21 insertions, 15 deletions
diff --git a/bug/operation.go b/bug/operation.go
index 8dec5644..cc5b0007 100644
--- a/bug/operation.go
+++ b/bug/operation.go
@@ -76,8 +76,6 @@ func hashOperation(op Operation) (git.Hash, error) {
return base.hash, nil
}
-// TODO: serialization with identity
-
// OpBase implement the common code for all operations
type OpBase struct {
OperationType OperationType
@@ -100,28 +98,40 @@ func newOpBase(opType OperationType, author identity.Interface, unixTime int64)
}
}
-type opBaseJson struct {
- OperationType OperationType `json:"type"`
- UnixTime int64 `json:"timestamp"`
- Metadata map[string]string `json:"metadata,omitempty"`
-}
-
-func (op *OpBase) MarshalJSON() ([]byte, error) {
- return json.Marshal(opBaseJson{
+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 := opBaseJson{}
+ 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
@@ -149,10 +159,6 @@ 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")
}