1package bug
2
3import (
4 "encoding/json"
5
6 "github.com/MichaelMure/git-bug/identity"
7)
8
9var _ Operation = &NoOpOperation{}
10
11// NoOpOperation is an operation that does not change the bug state. It can
12// however be used to store arbitrary metadata in the bug history, for example
13// to support a bridge feature.
14type NoOpOperation struct {
15 OpBase
16}
17
18func (op *NoOpOperation) base() *OpBase {
19 return &op.OpBase
20}
21
22func (op *NoOpOperation) ID() string {
23 return idOperation(op)
24}
25
26func (op *NoOpOperation) Apply(snapshot *Snapshot) {
27 // Nothing to do
28}
29
30func (op *NoOpOperation) Validate() error {
31 return opBaseValidate(op, NoOpOp)
32}
33
34// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
35// MarshalJSON
36func (op *NoOpOperation) MarshalJSON() ([]byte, error) {
37 base, err := json.Marshal(op.OpBase)
38 if err != nil {
39 return nil, err
40 }
41
42 // revert back to a flat map to be able to add our own fields
43 var data map[string]interface{}
44 if err := json.Unmarshal(base, &data); err != nil {
45 return nil, err
46 }
47
48 return json.Marshal(data)
49}
50
51// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
52// MarshalJSON
53func (op *NoOpOperation) UnmarshalJSON(data []byte) error {
54 // Unmarshal OpBase and the op separately
55
56 base := OpBase{}
57 err := json.Unmarshal(data, &base)
58 if err != nil {
59 return err
60 }
61
62 aux := struct{}{}
63
64 err = json.Unmarshal(data, &aux)
65 if err != nil {
66 return err
67 }
68
69 op.OpBase = base
70
71 return nil
72}
73
74// Sign post method for gqlgen
75func (op *NoOpOperation) IsAuthored() {}
76
77func NewNoOpOp(author identity.Interface, unixTime int64) *NoOpOperation {
78 return &NoOpOperation{
79 OpBase: newOpBase(NoOpOp, author, unixTime),
80 }
81}
82
83// Convenience function to apply the operation
84func NoOp(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*NoOpOperation, error) {
85 op := NewNoOpOp(author, unixTime)
86
87 for key, value := range metadata {
88 op.SetMetadata(key, value)
89 }
90
91 if err := op.Validate(); err != nil {
92 return nil, err
93 }
94 b.Append(op)
95 return op, nil
96}