operation.go

  1package bug
  2
  3import (
  4	"crypto/sha256"
  5	"encoding/json"
  6	"fmt"
  7	"time"
  8
  9	"github.com/MichaelMure/git-bug/identity"
 10
 11	"github.com/MichaelMure/git-bug/util/git"
 12	"github.com/pkg/errors"
 13)
 14
 15// OperationType is an operation type identifier
 16type OperationType int
 17
 18const (
 19	_ OperationType = iota
 20	CreateOp
 21	SetTitleOp
 22	AddCommentOp
 23	SetStatusOp
 24	LabelChangeOp
 25	EditCommentOp
 26	NoOpOp
 27	SetMetadataOp
 28)
 29
 30// Operation define the interface to fulfill for an edit operation of a Bug
 31type Operation interface {
 32	// base return the OpBase of the Operation, for package internal use
 33	base() *OpBase
 34	// Hash return the hash of the operation, to be used for back references
 35	Hash() (git.Hash, error)
 36	// Time return the time when the operation was added
 37	Time() time.Time
 38	// GetUnixTime return the unix timestamp when the operation was added
 39	GetUnixTime() int64
 40	// GetFiles return the files needed by this operation
 41	GetFiles() []git.Hash
 42	// Apply the operation to a Snapshot to create the final state
 43	Apply(snapshot *Snapshot)
 44	// Validate check if the operation is valid (ex: a title is a single line)
 45	Validate() error
 46	// SetMetadata store arbitrary metadata about the operation
 47	SetMetadata(key string, value string)
 48	// GetMetadata retrieve arbitrary metadata about the operation
 49	GetMetadata(key string) (string, bool)
 50	// AllMetadata return all metadata for this operation
 51	AllMetadata() map[string]string
 52}
 53
 54func hashRaw(data []byte) git.Hash {
 55	hasher := sha256.New()
 56	// Write can't fail
 57	_, _ = hasher.Write(data)
 58	return git.Hash(fmt.Sprintf("%x", hasher.Sum(nil)))
 59}
 60
 61// hash compute the hash of the serialized operation
 62func hashOperation(op Operation) (git.Hash, error) {
 63	base := op.base()
 64
 65	if base.hash != "" {
 66		return base.hash, nil
 67	}
 68
 69	data, err := json.Marshal(op)
 70	if err != nil {
 71		return "", err
 72	}
 73
 74	base.hash = hashRaw(data)
 75
 76	return base.hash, nil
 77}
 78
 79// TODO: serialization with identity
 80
 81// OpBase implement the common code for all operations
 82type OpBase struct {
 83	OperationType OperationType
 84	Author        identity.Interface
 85	UnixTime      int64
 86	Metadata      map[string]string
 87	// Not serialized. Store the op's hash in memory.
 88	hash git.Hash
 89	// Not serialized. Store the extra metadata in memory,
 90	// compiled from SetMetadataOperation.
 91	extraMetadata map[string]string
 92}
 93
 94// newOpBase is the constructor for an OpBase
 95func newOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase {
 96	return OpBase{
 97		OperationType: opType,
 98		Author:        author,
 99		UnixTime:      unixTime,
100	}
101}
102
103type opBaseJson struct {
104	OperationType OperationType     `json:"type"`
105	UnixTime      int64             `json:"timestamp"`
106	Metadata      map[string]string `json:"metadata,omitempty"`
107}
108
109func (op *OpBase) MarshalJSON() ([]byte, error) {
110	return json.Marshal(opBaseJson{
111		OperationType: op.OperationType,
112		UnixTime:      op.UnixTime,
113		Metadata:      op.Metadata,
114	})
115}
116
117func (op *OpBase) UnmarshalJSON(data []byte) error {
118	aux := opBaseJson{}
119
120	if err := json.Unmarshal(data, &aux); err != nil {
121		return err
122	}
123
124	op.OperationType = aux.OperationType
125	op.UnixTime = aux.UnixTime
126	op.Metadata = aux.Metadata
127
128	return nil
129}
130
131// Time return the time when the operation was added
132func (op *OpBase) Time() time.Time {
133	return time.Unix(op.UnixTime, 0)
134}
135
136// GetUnixTime return the unix timestamp when the operation was added
137func (op *OpBase) GetUnixTime() int64 {
138	return op.UnixTime
139}
140
141// GetFiles return the files needed by this operation
142func (op *OpBase) GetFiles() []git.Hash {
143	return nil
144}
145
146// Validate check the OpBase for errors
147func opBaseValidate(op Operation, opType OperationType) error {
148	if op.base().OperationType != opType {
149		return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
150	}
151
152	if _, err := op.Hash(); err != nil {
153		return errors.Wrap(err, "op is not serializable")
154	}
155
156	if op.GetUnixTime() == 0 {
157		return fmt.Errorf("time not set")
158	}
159
160	if op.base().Author == nil {
161		return fmt.Errorf("author not set")
162	}
163
164	if err := op.base().Author.Validate(); err != nil {
165		return errors.Wrap(err, "author")
166	}
167
168	for _, hash := range op.GetFiles() {
169		if !hash.IsValid() {
170			return fmt.Errorf("file with invalid hash %v", hash)
171		}
172	}
173
174	return nil
175}
176
177// SetMetadata store arbitrary metadata about the operation
178func (op *OpBase) SetMetadata(key string, value string) {
179	if op.Metadata == nil {
180		op.Metadata = make(map[string]string)
181	}
182
183	op.Metadata[key] = value
184	op.hash = ""
185}
186
187// GetMetadata retrieve arbitrary metadata about the operation
188func (op *OpBase) GetMetadata(key string) (string, bool) {
189	val, ok := op.Metadata[key]
190
191	if ok {
192		return val, true
193	}
194
195	// extraMetadata can't replace the original operations value if any
196	val, ok = op.extraMetadata[key]
197
198	return val, ok
199}
200
201// AllMetadata return all metadata for this operation
202func (op *OpBase) AllMetadata() map[string]string {
203	result := make(map[string]string)
204
205	for key, val := range op.extraMetadata {
206		result[key] = val
207	}
208
209	// Original metadata take precedence
210	for key, val := range op.Metadata {
211		result[key] = val
212	}
213
214	return result
215}