2018-07-13 17:14:00 +03:00
|
|
|
package bug
|
|
|
|
|
|
|
|
type OperationIterator struct {
|
|
|
|
bug *Bug
|
|
|
|
packIndex int
|
|
|
|
opIndex int
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:11:38 +03:00
|
|
|
func NewOperationIterator(bug Interface) *OperationIterator {
|
2018-07-13 17:14:00 +03:00
|
|
|
return &OperationIterator{
|
2018-08-23 20:11:38 +03:00
|
|
|
bug: bugFromInterface(bug),
|
2018-07-13 17:14:00 +03:00
|
|
|
packIndex: 0,
|
|
|
|
opIndex: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *OperationIterator) Next() bool {
|
|
|
|
// Special case of the staging area
|
2018-07-13 22:21:24 +03:00
|
|
|
if it.packIndex == len(it.bug.packs) {
|
|
|
|
pack := it.bug.staging
|
2018-07-13 17:14:00 +03:00
|
|
|
it.opIndex++
|
|
|
|
return it.opIndex < len(pack.Operations)
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
if it.packIndex >= len(it.bug.packs) {
|
2018-07-13 17:14:00 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
pack := it.bug.packs[it.packIndex]
|
2018-07-13 17:14:00 +03:00
|
|
|
|
|
|
|
it.opIndex++
|
|
|
|
|
|
|
|
if it.opIndex < len(pack.Operations) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this iterator doesn't handle the empty pack case
|
|
|
|
it.opIndex = 0
|
|
|
|
it.packIndex++
|
|
|
|
|
|
|
|
// Special case of the non-empty staging area
|
2018-07-13 22:21:24 +03:00
|
|
|
if it.packIndex == len(it.bug.packs) && len(it.bug.staging.Operations) > 0 {
|
2018-07-13 17:14:00 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
return it.packIndex < len(it.bug.packs)
|
2018-07-13 17:14:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *OperationIterator) Value() Operation {
|
|
|
|
// Special case of the staging area
|
2018-07-13 22:21:24 +03:00
|
|
|
if it.packIndex == len(it.bug.packs) {
|
|
|
|
pack := it.bug.staging
|
2018-07-13 17:14:00 +03:00
|
|
|
|
|
|
|
if it.opIndex >= len(pack.Operations) {
|
|
|
|
panic("Iterator is not valid anymore")
|
|
|
|
}
|
|
|
|
|
|
|
|
return pack.Operations[it.opIndex]
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
if it.packIndex >= len(it.bug.packs) {
|
2018-07-13 17:14:00 +03:00
|
|
|
panic("Iterator is not valid anymore")
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
pack := it.bug.packs[it.packIndex]
|
2018-07-13 17:14:00 +03:00
|
|
|
|
|
|
|
if it.opIndex >= len(pack.Operations) {
|
|
|
|
panic("Iterator is not valid anymore")
|
|
|
|
}
|
|
|
|
|
|
|
|
return pack.Operations[it.opIndex]
|
|
|
|
}
|