pipeline API

pipeline

package

API reference for the pipeline package.

T
type

Middleware

Middleware wraps a pipeline handler with cross-cutting behavior.

core/pipeline/pipeline.go:8-8
type Middleware func(ctx context.Context, input T, next func(context.Context, T) (U, error)) (U, error)
S
struct

Pipeline

Pipeline chains Middleware functions around a final handler.

core/pipeline/pipeline.go:11-14
type Pipeline struct

Fields

Name Type Description
middlewares []Middleware[T, U]
handler func(context.Context, T) (U, error)
F
function

New

New creates an empty Pipeline.

Returns

*Pipeline[T,
U]
core/pipeline/pipeline.go:17-19
func New[T, U any]() *Pipeline[T, U]

{
	return &Pipeline[T, U]{}
}
F
function

TestPipelineProcessOrder

Parameters

core/pipeline/pipeline_test.go:9-43
func TestPipelineProcessOrder(t *testing.T)

{
	var calls []string
	p := New[string, string]().
		Use(func(ctx context.Context, input string, next func(context.Context, string) (string, error)) (string, error) {
			calls = append(calls, "before-a")
			out, err := next(ctx, input+"a")
			calls = append(calls, "after-a")
			return out + "A", err
		}).
		Use(func(ctx context.Context, input string, next func(context.Context, string) (string, error)) (string, error) {
			calls = append(calls, "before-b")
			out, err := next(ctx, input+"b")
			calls = append(calls, "after-b")
			return out + "B", err
		}).
		Then(func(ctx context.Context, input string) (string, error) {
			calls = append(calls, "handler")
			return input + "h", nil
		})

	got, err := p.Process(context.Background(), "")
	if err != nil {
		t.Fatalf("Process() error = %v", err)
	}
	if got != "abhBA" {
		t.Fatalf("Process() = %q, want abhBA", got)
	}

	want := []string{"before-a", "before-b", "handler", "after-b", "after-a"}
	for i := range want {
		if calls[i] != want[i] {
			t.Fatalf("calls[%d] = %q, want %q", i, calls[i], want[i])
		}
	}
}
F
function

TestPipelineProcessWithoutHandler

Parameters

core/pipeline/pipeline_test.go:45-53
func TestPipelineProcessWithoutHandler(t *testing.T)

{
	got, err := New[string, int]().Process(context.Background(), "input")
	if err != nil {
		t.Fatalf("Process() error = %v", err)
	}
	if got != 0 {
		t.Fatalf("Process() = %d, want zero", got)
	}
}
F
function

TestPipelineReturnsError

Parameters

core/pipeline/pipeline_test.go:55-66
func TestPipelineReturnsError(t *testing.T)

{
	want := errors.New("stop")
	p := New[string, string]().
		Then(func(ctx context.Context, input string) (string, error) {
			return "", want
		})

	_, err := p.Process(context.Background(), "")
	if !errors.Is(err, want) {
		t.Fatalf("Process() error = %v, want %v", err, want)
	}
}