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