aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation_iterator.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-13 16:14:00 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-13 16:14:00 +0200
commit078545538e6e6bf7a050fe6602a42a61fb5203e9 (patch)
tree8f8239efb0b74f0a9d4e5a50a7245668b0bb8e2d /bug/operation_iterator.go
parentbc12fee58e8bd86672793ae37d9f924158afb482 (diff)
downloadgit-bug-078545538e6e6bf7a050fe6602a42a61fb5203e9.tar.gz
add a bug's operation iterator
Diffstat (limited to 'bug/operation_iterator.go')
-rw-r--r--bug/operation_iterator.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/bug/operation_iterator.go b/bug/operation_iterator.go
new file mode 100644
index 00000000..a576ddd6
--- /dev/null
+++ b/bug/operation_iterator.go
@@ -0,0 +1,72 @@
+package bug
+
+type OperationIterator struct {
+ bug *Bug
+ packIndex int
+ opIndex int
+}
+
+func NewOperationIterator(bug *Bug) *OperationIterator {
+ return &OperationIterator{
+ bug: bug,
+ packIndex: 0,
+ opIndex: -1,
+ }
+}
+
+func (it *OperationIterator) Next() bool {
+ // Special case of the staging area
+ if it.packIndex == len(it.bug.Packs)+1 {
+ pack := it.bug.Staging
+ it.opIndex++
+ return it.opIndex < len(pack.Operations)
+ }
+
+ if it.packIndex >= len(it.bug.Packs) {
+ return false
+ }
+
+ pack := it.bug.Packs[it.packIndex]
+
+ 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)+1 && len(it.bug.Staging.Operations) > 0 {
+ return true
+ }
+
+ return it.packIndex < len(it.bug.Packs)
+}
+
+func (it *OperationIterator) Value() Operation {
+ // Special case of the staging area
+ if it.packIndex == len(it.bug.Packs)+1 {
+ pack := it.bug.Staging
+
+ if it.opIndex >= len(pack.Operations) {
+ panic("Iterator is not valid anymore")
+ }
+
+ return pack.Operations[it.opIndex]
+ }
+
+ if it.packIndex >= len(it.bug.Packs) {
+ panic("Iterator is not valid anymore")
+ }
+
+ pack := it.bug.Packs[it.packIndex]
+
+ if it.opIndex >= len(pack.Operations) {
+ panic("Iterator is not valid anymore")
+ }
+
+ return pack.Operations[it.opIndex]
+}