aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation.go
blob: 7d71e35221e1d0a2e57a720c007282865068470b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package bug

import (
	"github.com/MichaelMure/git-bug/util"
	"time"
)

// OperationType is an identifier
type OperationType int

const (
	_ OperationType = iota
	CreateOp
	SetTitleOp
	AddCommentOp
	SetStatusOp
	LabelChangeOp
)

// Operation define the interface to fulfill for an edit operation of a Bug
type Operation interface {
	// OpType return the type of operation
	OpType() OperationType
	// Time return the time when the operation was added
	Time() time.Time
	// unixTime return the unix timestamp when the operation was added
	UnixTime() int64
	// Apply the operation to a Snapshot to create the final state
	Apply(snapshot Snapshot) Snapshot
	// Files return the files needed by this operation
	Files() []util.Hash

	// TODO: data validation (ex: a title is a single line)
	// Validate() bool
}

// OpBase implement the common code for all operations
type OpBase struct {
	OperationType OperationType
	Author        Person
	unixTime      int64
}

// NewOpBase is the constructor for an OpBase
func NewOpBase(opType OperationType, author Person) OpBase {
	return OpBase{
		OperationType: opType,
		Author:        author,
		unixTime:      time.Now().Unix(),
	}
}

// OpType return the type of operation
func (op OpBase) OpType() OperationType {
	return op.OperationType
}

// Time return the time when the operation was added
func (op OpBase) Time() time.Time {
	return time.Unix(op.unixTime, 0)
}

// unixTime return the unix timestamp when the operation was added
func (op OpBase) UnixTime() int64 {
	return op.unixTime
}

// Files return the files needed by this operation
func (op OpBase) Files() []util.Hash {
	return nil
}