1package bug
2
3import (
4 "github.com/kevinburke/go.uuid"
5)
6
7// Bug hold the data of a bug thread, organized in a way close to
8// how it will be persisted inside Git. This is the datastructure
9// used for merge of two different version.
10type Bug struct {
11 // Id used as unique identifier
12 Id uuid.UUID
13
14 // TODO: need a way to order bugs
15 // Probably a Lamport clock
16
17 Packs []OperationPack
18
19 Staging OperationPack
20}
21
22// Create a new Bug
23func NewBug() (*Bug, error) {
24
25 // Creating UUID Version 4
26 id, err := uuid.ID4()
27
28 if err != nil {
29 return nil, err
30 }
31
32 return &Bug{
33 Id: id,
34 }, nil
35}
36
37// IsValid check if the Bug data is valid
38func (bug *Bug) IsValid() bool {
39 // non-empty
40 if len(bug.Packs) == 0 {
41 return false
42 }
43
44 // check if each pack is valid
45 for _, pack := range bug.Packs {
46 if !pack.IsValid() {
47 return false
48 }
49 }
50
51 // The very first Op should be a CREATE
52 firstOp := bug.Packs[0].Operations[0]
53 if firstOp.OpType() != CREATE {
54 return false
55 }
56
57 // Check that there is no more CREATE op
58 it := NewOperationIterator(bug)
59 createCount := 0
60 for it.Next() {
61 if it.Value().OpType() == CREATE {
62 createCount++
63 }
64 }
65
66 if createCount != 1 {
67 return false
68 }
69
70 return true
71}
72
73func (bug *Bug) Append(op Operation) {
74 bug.Staging.Append(op)
75}
76
77func (bug *Bug) Commit() {
78 bug.Packs = append(bug.Packs, bug.Staging)
79 bug.Staging = OperationPack{}
80}
81
82func (bug *Bug) HumanId() string {
83 return bug.Id.String()
84}