aboutsummaryrefslogtreecommitdiffstats
path: root/bug/op_edit_comment.go
diff options
context:
space:
mode:
Diffstat (limited to 'bug/op_edit_comment.go')
-rw-r--r--bug/op_edit_comment.go51
1 files changed, 17 insertions, 34 deletions
diff --git a/bug/op_edit_comment.go b/bug/op_edit_comment.go
index 2f269b02..a37ce8f7 100644
--- a/bug/op_edit_comment.go
+++ b/bug/op_edit_comment.go
@@ -4,6 +4,9 @@ import (
"encoding/json"
"fmt"
+ "github.com/pkg/errors"
+
+ "github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/timestamp"
@@ -16,16 +19,16 @@ var _ Operation = &EditCommentOperation{}
// EditCommentOperation will change a comment in the bug
type EditCommentOperation struct {
OpBase
- Target string
- Message string
- Files []git.Hash
+ Target entity.Id `json:"target"`
+ Message string `json:"message"`
+ Files []git.Hash `json:"files"`
}
func (op *EditCommentOperation) base() *OpBase {
return &op.OpBase
}
-func (op *EditCommentOperation) ID() string {
+func (op *EditCommentOperation) Id() entity.Id {
return idOperation(op)
}
@@ -38,7 +41,7 @@ func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
var target TimelineItem
for i, item := range snapshot.Timeline {
- if item.ID() == op.Target {
+ if item.Id() == op.Target {
target = snapshot.Timeline[i]
break
}
@@ -86,8 +89,8 @@ func (op *EditCommentOperation) Validate() error {
return err
}
- if !IDIsValid(op.Target) {
- return fmt.Errorf("target hash is invalid")
+ if err := op.Target.Validate(); err != nil {
+ return errors.Wrap(err, "target hash is invalid")
}
if !text.Safe(op.Message) {
@@ -97,29 +100,9 @@ func (op *EditCommentOperation) Validate() error {
return nil
}
-// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
-// MarshalJSON
-func (op *EditCommentOperation) MarshalJSON() ([]byte, error) {
- base, err := json.Marshal(op.OpBase)
- if err != nil {
- return nil, err
- }
-
- // revert back to a flat map to be able to add our own fields
- var data map[string]interface{}
- if err := json.Unmarshal(base, &data); err != nil {
- return nil, err
- }
-
- data["target"] = op.Target
- data["message"] = op.Message
- data["files"] = op.Files
-
- return json.Marshal(data)
-}
-
-// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
-// MarshalJSON
+// UnmarshalJSON is a two step JSON unmarshaling
+// This workaround is necessary to avoid the inner OpBase.MarshalJSON
+// overriding the outer op's MarshalJSON
func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
// Unmarshal OpBase and the op separately
@@ -130,7 +113,7 @@ func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
}
aux := struct {
- Target string `json:"target"`
+ Target entity.Id `json:"target"`
Message string `json:"message"`
Files []git.Hash `json:"files"`
}{}
@@ -151,7 +134,7 @@ func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
// Sign post method for gqlgen
func (op *EditCommentOperation) IsAuthored() {}
-func NewEditCommentOp(author identity.Interface, unixTime int64, target string, message string, files []git.Hash) *EditCommentOperation {
+func NewEditCommentOp(author identity.Interface, unixTime int64, target entity.Id, message string, files []git.Hash) *EditCommentOperation {
return &EditCommentOperation{
OpBase: newOpBase(EditCommentOp, author, unixTime),
Target: target,
@@ -161,11 +144,11 @@ func NewEditCommentOp(author identity.Interface, unixTime int64, target string,
}
// Convenience function to apply the operation
-func EditComment(b Interface, author identity.Interface, unixTime int64, target string, message string) (*EditCommentOperation, error) {
+func EditComment(b Interface, author identity.Interface, unixTime int64, target entity.Id, message string) (*EditCommentOperation, error) {
return EditCommentWithFiles(b, author, unixTime, target, message, nil)
}
-func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target string, message string, files []git.Hash) (*EditCommentOperation, error) {
+func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target entity.Id, message string, files []git.Hash) (*EditCommentOperation, error) {
editCommentOp := NewEditCommentOp(author, unixTime, target, message, files)
if err := editCommentOp.Validate(); err != nil {
return nil, err