operation.go

 1package bug
 2
 3import (
 4	"github.com/MichaelMure/git-bug/util/git"
 5	"time"
 6)
 7
 8// OperationType is an identifier
 9type OperationType int
10
11const (
12	_ OperationType = iota
13	CreateOp
14	SetTitleOp
15	AddCommentOp
16	SetStatusOp
17	LabelChangeOp
18)
19
20// Operation define the interface to fulfill for an edit operation of a Bug
21type Operation interface {
22	// OpType return the type of operation
23	OpType() OperationType
24	// Time return the time when the operation was added
25	Time() time.Time
26	// GetUnixTime return the unix timestamp when the operation was added
27	GetUnixTime() int64
28	// Apply the operation to a Snapshot to create the final state
29	Apply(snapshot Snapshot) Snapshot
30	// Files return the files needed by this operation
31	Files() []git.Hash
32
33	// TODO: data validation (ex: a title is a single line)
34	// Validate() bool
35}
36
37// OpBase implement the common code for all operations
38type OpBase struct {
39	OperationType OperationType
40	Author        Person
41	UnixTime      int64
42}
43
44// NewOpBase is the constructor for an OpBase
45func NewOpBase(opType OperationType, author Person) OpBase {
46	return OpBase{
47		OperationType: opType,
48		Author:        author,
49		UnixTime:      time.Now().Unix(),
50	}
51}
52
53// OpType return the type of operation
54func (op OpBase) OpType() OperationType {
55	return op.OperationType
56}
57
58// Time return the time when the operation was added
59func (op OpBase) Time() time.Time {
60	return time.Unix(op.UnixTime, 0)
61}
62
63// GetUnixTime return the unix timestamp when the operation was added
64func (op OpBase) GetUnixTime() int64 {
65	return op.UnixTime
66}
67
68// Files return the files needed by this operation
69func (op OpBase) Files() []git.Hash {
70	return nil
71}