/
slice.go
53 lines (43 loc) · 1.11 KB
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-FileCopyrightText: 2021 The Luigi Authors
//
// SPDX-License-Identifier: MIT
package luigi
import (
"context"
"errors"
)
// SliceSink binds Source methods to an interface array.
type SliceSource []interface{}
// Next implements the Source interface.
func (src *SliceSource) Next(context.Context) (v interface{}, err error) {
if len(*src) == 0 {
return nil, EOS{}
}
v, *src = (*src)[0], (*src)[1:]
return v, nil
}
// SliceSink binds Sink methods to an interface array.
type SliceSink struct {
slice *[]interface{}
closed bool
}
// NewSliceSink returns a new SliceSink bound to the given interface array.
func NewSliceSink(arg *[]interface{}) *SliceSink {
return &SliceSink{
slice: arg,
closed: false,
}
}
// Pour implements the Sink interface. It writes value to a destination Sink.
func (sink *SliceSink) Pour(ctx context.Context, v interface{}) error {
if sink.closed {
return errors.New("pour to closed sink")
}
*sink.slice = append(*sink.slice, v)
return nil
}
// Close is a dummy method to implement the Sink interface.
func (sink *SliceSink) Close() error {
sink.closed = true
return nil
}