aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation_pack.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-30 11:10:03 +0200
committerGitHub <noreply@github.com>2018-09-30 11:10:03 +0200
commitd71bb7dd7632780cf5aad5fda84027fa03a9d0f0 (patch)
treedba6c3c0bab18f41e21cd36a9fe05d1d27a574d4 /bug/operation_pack.go
parent8fdd6bf99c111c3756056e87ffd9209875ac5c1f (diff)
parentbad9cda969b49bf1bce6799056476ac4684892df (diff)
downloadgit-bug-d71bb7dd7632780cf5aad5fda84027fa03a9d0f0.tar.gz
Merge pull request #54 from MichaelMure/editablecomment
Core support for editable comments
Diffstat (limited to 'bug/operation_pack.go')
-rw-r--r--bug/operation_pack.go58
1 files changed, 36 insertions, 22 deletions
diff --git a/bug/operation_pack.go b/bug/operation_pack.go
index 03d538d5..f33d94bf 100644
--- a/bug/operation_pack.go
+++ b/bug/operation_pack.go
@@ -3,7 +3,6 @@ package bug
import (
"encoding/json"
"fmt"
- "reflect"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/git"
@@ -25,17 +24,6 @@ type OperationPack struct {
commitHash git.Hash
}
-// hold the different operation type to instantiate to parse JSON
-var operations map[OperationType]reflect.Type
-
-// Register will register a new type of Operation to be able to parse the corresponding JSON
-func Register(t OperationType, op interface{}) {
- if operations == nil {
- operations = make(map[OperationType]reflect.Type)
- }
- operations[t] = reflect.TypeOf(op)
-}
-
func (opp *OperationPack) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Version uint `json:"version"`
@@ -69,25 +57,51 @@ func (opp *OperationPack) UnmarshalJSON(data []byte) error {
return err
}
- opType, ok := operations[t.OperationType]
- if !ok {
- return fmt.Errorf("unknown operation type %v", t.OperationType)
- }
-
- op := reflect.New(opType).Interface()
-
- if err := json.Unmarshal(raw, op); err != nil {
+ op, err := opp.unmarshalOp(raw, t.OperationType)
+ if err != nil {
return err
}
- deref := reflect.ValueOf(op).Elem().Interface()
+ // Compute the hash of the operation
+ op.base().hash = hashRaw(raw)
- opp.Operations = append(opp.Operations, deref.(Operation))
+ opp.Operations = append(opp.Operations, op)
}
return nil
}
+func (opp *OperationPack) unmarshalOp(raw []byte, _type OperationType) (Operation, error) {
+ switch _type {
+ case CreateOp:
+ op := &CreateOperation{}
+ err := json.Unmarshal(raw, &op)
+ return op, err
+ case SetTitleOp:
+ op := &SetTitleOperation{}
+ err := json.Unmarshal(raw, &op)
+ return op, err
+ case AddCommentOp:
+ op := &AddCommentOperation{}
+ err := json.Unmarshal(raw, &op)
+ return op, err
+ case SetStatusOp:
+ op := &SetStatusOperation{}
+ err := json.Unmarshal(raw, &op)
+ return op, err
+ case LabelChangeOp:
+ op := &LabelChangeOperation{}
+ err := json.Unmarshal(raw, &op)
+ return op, err
+ case EditCommentOp:
+ op := &EditCommentOperation{}
+ err := json.Unmarshal(raw, &op)
+ return op, err
+ default:
+ return nil, fmt.Errorf("unknown operation type %v", _type)
+ }
+}
+
// Append a new operation to the pack
func (opp *OperationPack) Append(op Operation) {
opp.Operations = append(opp.Operations, op)