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.go58
1 files changed, 55 insertions, 3 deletions
diff --git a/bug/op_edit_comment.go b/bug/op_edit_comment.go
index 9e0afc02..3ff16653 100644
--- a/bug/op_edit_comment.go
+++ b/bug/op_edit_comment.go
@@ -1,6 +1,7 @@
package bug
import (
+ "encoding/json"
"fmt"
"github.com/MichaelMure/git-bug/identity"
@@ -14,9 +15,9 @@ var _ Operation = &EditCommentOperation{}
// EditCommentOperation will change a comment in the bug
type EditCommentOperation struct {
OpBase
- Target git.Hash `json:"target"`
- Message string `json:"message"`
- Files []git.Hash `json:"files"`
+ Target git.Hash
+ Message string
+ Files []git.Hash
}
func (op *EditCommentOperation) base() *OpBase {
@@ -94,6 +95,57 @@ 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
+func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
+ // Unmarshal OpBase and the op separately
+
+ base := OpBase{}
+ err := json.Unmarshal(data, &base)
+ if err != nil {
+ return err
+ }
+
+ aux := struct {
+ Target git.Hash `json:"target"`
+ Message string `json:"message"`
+ Files []git.Hash `json:"files"`
+ }{}
+
+ err = json.Unmarshal(data, &aux)
+ if err != nil {
+ return err
+ }
+
+ op.OpBase = base
+ op.Target = aux.Target
+ op.Message = aux.Message
+ op.Files = aux.Files
+
+ return nil
+}
+
// Sign post method for gqlgen
func (op *EditCommentOperation) IsAuthored() {}