aboutsummaryrefslogtreecommitdiffstats
path: root/bug/op_add_comment.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/op_add_comment.go
parent844616baf8dc628360942d57fd69f24e298e08da (diff)
downloadgit-bug-d10c76469d40f13e27739fd363145e89bf74c3e0.tar.gz
identity: somewhat getting closer !
Diffstat (limited to 'bug/op_add_comment.go')
-rw-r--r--bug/op_add_comment.go54
1 files changed, 51 insertions, 3 deletions
diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go
index 23a10419..ba5d611e 100644
--- a/bug/op_add_comment.go
+++ b/bug/op_add_comment.go
@@ -1,10 +1,10 @@
package bug
import (
+ "encoding/json"
"fmt"
"github.com/MichaelMure/git-bug/identity"
-
"github.com/MichaelMure/git-bug/util/git"
"github.com/MichaelMure/git-bug/util/text"
)
@@ -14,9 +14,9 @@ var _ Operation = &AddCommentOperation{}
// AddCommentOperation will add a new comment in the bug
type AddCommentOperation struct {
OpBase
- Message string `json:"message"`
+ Message string
// TODO: change for a map[string]util.hash to store the filename ?
- Files []git.Hash `json:"files"`
+ Files []git.Hash
}
func (op *AddCommentOperation) base() *OpBase {
@@ -67,6 +67,54 @@ func (op *AddCommentOperation) Validate() error {
return nil
}
+// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
+// MarshalJSON
+func (op *AddCommentOperation) 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["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 *AddCommentOperation) UnmarshalJSON(data []byte) error {
+ // Unmarshal OpBase and the op separately
+
+ base := OpBase{}
+ err := json.Unmarshal(data, &base)
+ if err != nil {
+ return err
+ }
+
+ aux := struct {
+ Message string `json:"message"`
+ Files []git.Hash `json:"files"`
+ }{}
+
+ err = json.Unmarshal(data, &aux)
+ if err != nil {
+ return err
+ }
+
+ op.OpBase = base
+ op.Message = aux.Message
+ op.Files = aux.Files
+
+ return nil
+}
+
// Sign post method for gqlgen
func (op *AddCommentOperation) IsAuthored() {}