operation_pack.go

 1package bug
 2
 3import (
 4	"encoding/json"
 5	"github.com/MichaelMure/git-bug/repository"
 6	"github.com/MichaelMure/git-bug/util"
 7)
 8
 9// OperationPack represent an ordered set of operation to apply
10// to a Bug. These operations are stored in a single Git commit.
11//
12// These commits will be linked together in a linear chain of commits
13// inside Git to form the complete ordered chain of operation to
14// apply to get the final state of the Bug
15type OperationPack struct {
16	Operations []Operation `json:"ops"`
17	hash       util.Hash
18}
19
20func Parse() (OperationPack, error) {
21	// TODO
22	return OperationPack{}, nil
23}
24
25func (opp *OperationPack) Serialize() ([]byte, error) {
26	jsonBytes, err := json.Marshal(*opp)
27	if err != nil {
28		return nil, err
29	}
30
31	return jsonBytes, nil
32}
33
34// Append a new operation to the pack
35func (opp *OperationPack) Append(op Operation) {
36	opp.Operations = append(opp.Operations, op)
37}
38
39func (opp *OperationPack) IsEmpty() bool {
40	return len(opp.Operations) == 0
41}
42
43func (opp *OperationPack) IsValid() bool {
44	return !opp.IsEmpty()
45}
46
47func (opp *OperationPack) Write(repo repository.Repo) (util.Hash, error) {
48	data, err := opp.Serialize()
49
50	if err != nil {
51		return "", err
52	}
53
54	hash, err := repo.StoreData(data)
55
56	if err != nil {
57		return "", err
58	}
59
60	return hash, nil
61}