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) Id() entity.Id {
20	return idOperation(op, &op.OpBase)
21}
22
23func (op *NoOpOperation) Apply(snapshot *Snapshot) {
24	// Nothing to do
25}
26
27func (op *NoOpOperation) Validate() error {
28	return op.OpBase.Validate(op, NoOpOp)
29}
30
31// UnmarshalJSON is a two step JSON unmarshalling
32// This workaround is necessary to avoid the inner OpBase.MarshalJSON
33// overriding the outer op's MarshalJSON
34func (op *NoOpOperation) UnmarshalJSON(data []byte) error {
35	// Unmarshal OpBase and the op separately
36
37	base := OpBase{}
38	err := json.Unmarshal(data, &base)
39	if err != nil {
40		return err
41	}
42
43	aux := struct{}{}
44
45	err = json.Unmarshal(data, &aux)
46	if err != nil {
47		return err
48	}
49
50	op.OpBase = base
51
52	return nil
53}
54
55// Sign post method for gqlgen
56func (op *NoOpOperation) IsAuthored() {}
57
58func NewNoOpOp(author identity.Interface, unixTime int64) *NoOpOperation {
59	return &NoOpOperation{
60		OpBase: newOpBase(NoOpOp, author, unixTime),
61	}
62}
63
64// Convenience function to apply the operation
65func NoOp(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*NoOpOperation, error) {
66	op := NewNoOpOp(author, unixTime)
67
68	for key, value := range metadata {
69		op.SetMetadata(key, value)
70	}
71
72	if err := op.Validate(); err != nil {
73		return nil, err
74	}
75	b.Append(op)
76	return op, nil
77}