Or will take all items from the current depth and add them to a parent OrGen. Either the previous depth is an OrGen, or the stack will increment the sub depth to insert an OrGen.
()
| 64 | // Or will take all items from the current depth and add them to a parent OrGen. Either the previous depth is an OrGen, |
| 65 | // or the stack will increment the sub depth to insert an OrGen. |
| 66 | func (sgs *StatementGeneratorStack) Or() error { |
| 67 | if sgs.stack.Empty() { |
| 68 | return errors.Errorf("cannot apply Or to an empty stack") |
| 69 | } |
| 70 | parentElement := sgs.stack.PeekDepth(1) |
| 71 | current := sgs.aggregate(sgs.stack.Pop().generators) |
| 72 | if parentElement == nil { |
| 73 | // We're the root, so we put an OrGen as the root, and make the current a child of the new OrGen |
| 74 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth, Or(current))) |
| 75 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth)) |
| 76 | } else if len(parentElement.generators) == 0 { |
| 77 | if parentElement.depth == sgs.depth { |
| 78 | // We're still at the same depth, so it's safe to append an OrGen |
| 79 | parentElement.Append(Or(current)) |
| 80 | } else { |
| 81 | // We should retain the depth, so we need to create a new element since there's a depth boundary |
| 82 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth, Or(current))) |
| 83 | } |
| 84 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth)) |
| 85 | } else { |
| 86 | if parentElement.depth == sgs.depth { |
| 87 | // We're still at the same depth, so we need to check if the parent is an OrGen or not |
| 88 | if orGen, ok := parentElement.LastGenerator().(*OrGen); ok { |
| 89 | // The parent is an OrGen, so we can simply add this as another option |
| 90 | if err := orGen.AddChildren(current); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | } else { |
| 94 | // The parent is not an OrGen, so we need to add to the depth |
| 95 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth, Or(current))) |
| 96 | } |
| 97 | } else { |
| 98 | // We should retain the depth, so we need to create a new element since there's a depth boundary |
| 99 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth, Or(current))) |
| 100 | } |
| 101 | sgs.stack.Push(NewStatementGeneratorStackElement(sgs.depth)) |
| 102 | } |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | // NewScope increases the depth. |
| 107 | func (sgs *StatementGeneratorStack) NewScope() { |
no test coverage detected