git-bug/bug/operation_iterator.go

73 lines
1.4 KiB
Go
Raw Normal View History

2018-07-13 17:14:00 +03:00
package bug
type OperationIterator struct {
bug *Bug
packIndex int
opIndex int
}
func NewOperationIterator(bug Interface) *OperationIterator {
2018-07-13 17:14:00 +03:00
return &OperationIterator{
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
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)
}
if it.packIndex >= len(it.bug.packs) {
2018-07-13 17:14:00 +03:00
return false
}
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
if it.packIndex == len(it.bug.packs) && len(it.bug.staging.Operations) > 0 {
2018-07-13 17:14:00 +03:00
return true
}
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
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]
}
if it.packIndex >= len(it.bug.packs) {
2018-07-13 17:14:00 +03:00
panic("Iterator is not valid anymore")
}
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]
}