-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
63 lines (56 loc) · 1.87 KB
/
operation.go
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
package gobulk
import (
"errors"
"time"
)
// Operation represents a data segment like a file, it contains raw data that will be parsed into elements.
type Operation struct {
ID uint64
Iteration *Iteration
Container *Container
Type OperationType
OutputRepository string
OutputIdentifier string
Success bool
Created time.Time
Data interface{}
SubOperations []*Operation
}
// GetSubOperations returns all suboperations recursively.
func (o *Operation) GetSubOperations() []*Operation {
subops := make([]*Operation, 0, len(o.SubOperations))
for _, subop := range o.SubOperations {
subops = append(subops, subop)
subops = append(subops, subop.GetSubOperations()...)
}
return subops
}
// HasFailedSubOperations checks whether at least one of o sub operations resulted with failure.
func (o *Operation) HasFailedSubOperations() bool {
for _, subOp := range o.SubOperations {
if !subOp.Success || subOp.HasFailedSubOperations() {
return true
}
}
return false
}
// OperationType defines what a operation can do.
type OperationType string
const (
// OperationTypeCreate whether to add something to the output.
OperationTypeCreate OperationType = "create"
// OperationTypeUpdate whether to update something of the output.
OperationTypeUpdate OperationType = "update"
// OperationTypeDelete whether to delete something from the output.
OperationTypeDelete OperationType = "delete"
// OperationTypeOmit whether to keep everything as it is. use it if data are already saved correctly in output.
OperationTypeOmit OperationType = "omit"
)
// Valid checks whether the assigned operation type value is valid.
func (t OperationType) Valid() error {
switch t {
case OperationTypeCreate, OperationTypeUpdate, OperationTypeDelete, OperationTypeOmit:
return nil
}
return errors.New("invalid operation type")
}