Skip to content

mhmtszr/pipeline

Repository files navigation

Pipeline GoDoc Build Status Coverage Status Go Report Card

133256800-8d51f5e5-1cc5-45d2-a195-28e95f1cb92c

Go pipeline solution that can be used in many different combinations for chaining pipeline steps.

Inspired by @bilal-kilic's Kotlin implementation boru.

Usage

Supports 1.18+ Go versions because of Go Generics

go get github.com/mhmtszr/pipeline

Examples

Basic Pipeline

package main

import (
	"fmt"
	"github.com/mhmtszr/pipeline"
)

type Square struct{}
type Add struct{}

func (s Square) Execute(context int, next func(context int)) error {
	context = context * context
	println(fmt.Sprintf("After first chain context: %d", context))
	return next(context)
}

func (a Add) Execute(context int, next func(context int)) {
	context = context + context
	println(fmt.Sprintf("After second chain context: %d", context))
	return next(context)
}

func main() {
	p, _ := pipeline.Builder[int]{}.UsePipelineStep(Square{}).UsePipelineStep(Add{}).Build()
	p.Execute(3)
}
// After first chain context: 9
// After second chain context: 18

Conditional Pipeline

p := pipeline.Builder[*int]{}.
	UseConditionalStepBuilder(
		pipeline.NewConditionalStepBuilder[*int]().
			Condition(func(context *int) bool {
				return *context == 3
			}).
			IfTrue(Square{}).
			IfFalse(Add{}),
	).UsePipelineStep(Add{}).Build()
		
nm := 3
_ = p.Execute(&nm)

// nm 18

nm = 4
_ = p.Execute(&nm)

// nm 16