github.com/blend/go-sdk@v1.20220411.3/collections/batch_iterator.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  // BatchIterator is an iterator for interface{}
    11  type BatchIterator struct {
    12  	Items     []interface{}
    13  	BatchSize int
    14  	Cursor    int
    15  }
    16  
    17  // HasNext returns if we should process another batch.
    18  func (bi *BatchIterator) HasNext() bool {
    19  	return bi.Cursor < (len(bi.Items) - 1)
    20  }
    21  
    22  // Next yields the next batch.
    23  func (bi *BatchIterator) Next() []interface{} {
    24  	if bi.BatchSize == 0 {
    25  		return nil
    26  	}
    27  	if bi.Cursor >= len(bi.Items) {
    28  		return nil
    29  	}
    30  
    31  	if (bi.Cursor + bi.BatchSize) < len(bi.Items) {
    32  		output := bi.Items[bi.Cursor : bi.Cursor+bi.BatchSize]
    33  		bi.Cursor += len(output)
    34  		return output
    35  	}
    36  
    37  	output := bi.Items[bi.Cursor:]
    38  	bi.Cursor += len(output)
    39  	return output
    40  }