op_noop.go

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