github.com/MontFerret/ferret@v0.18.0/pkg/runtime/collections/tap.go (about)

     1  package collections
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  )
     8  
     9  type TapIterator struct {
    10  	values    Iterator
    11  	predicate core.Expression
    12  }
    13  
    14  func NewTapIterator(values Iterator, predicate core.Expression) (Iterator, error) {
    15  	if values == nil {
    16  		return nil, core.Error(core.ErrMissedArgument, "values")
    17  	}
    18  
    19  	if predicate == nil {
    20  		return nil, core.Error(core.ErrMissedArgument, "predicate")
    21  	}
    22  
    23  	return &TapIterator{values, predicate}, nil
    24  }
    25  
    26  func (iterator *TapIterator) Next(ctx context.Context, scope *core.Scope) (*core.Scope, error) {
    27  	nextScope, err := iterator.values.Next(ctx, scope)
    28  
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	_, err = iterator.predicate.Exec(ctx, nextScope)
    34  
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return nextScope, nil
    40  }