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