Skip to content

Commit

Permalink
add Yeti as a pipe function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Kossovich committed Oct 15, 2023
1 parent 73083cd commit 3fe5550
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
22 changes: 22 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"errors"
"fmt"

"github.com/koss-null/funcfrog/pkg/pipe"
)

func main() {
p := pipe.Slice([]int{1, 2, 3, 4, 5, 6, 7, 8, 9})
y := pipe.NewYeti()
res := p.Yeti(y).Map(func(i int) int {
if i < 0 {
y.Yeet(errors.New("omg the value is negative"))
}
return 2 * i
}).Snag(func(err error) {
fmt.Println("Snagging an error: " + err.Error())
}).Do()
fmt.Println(res)
}
7 changes: 0 additions & 7 deletions internal/internalpipe/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internalpipe

import (
"math"
"unsafe"

"golang.org/x/exp/constraints"
)
Expand Down Expand Up @@ -62,12 +61,6 @@ func (p Pipe[T]) Count() int {
return cnt
}

// Sang ads error handler to a current Pipe step.
func (p Pipe[T]) Snag(h ErrHandler) Pipe[T] {
p.y.SnagPipe(unsafe.Pointer(p.prevP), h)
return p
}

// limit returns the upper border limit as the pipe evaluation limit.
func (p *Pipe[T]) limit() int {
switch {
Expand Down
25 changes: 25 additions & 0 deletions internal/internalpipe/snag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package internalpipe

import "unsafe"

// Sang ads error handler to a current Pipe step.
func (p Pipe[T]) Snag(h ErrHandler) Pipe[T] {
// todo: think about NPE here
p.y.SnagPipe(unsafe.Pointer(p.prevP), h)
return p
}

type YeetSnag interface {
// yeet an error
Yeet(err error)
// snag and handle the error
Snag(ErrHandler)
}

// Yeti adds Yeti error handler to the pipe.
// If some other handlers were set before, they are handled by the Snag
func (p Pipe[T]) Yeti(y YeetSnag) Pipe[T] {
// todo: save previous y
p.y = y.(yeti)
return p
}
2 changes: 1 addition & 1 deletion internal/internalpipe/yeet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"unsafe"
)

type ErrHandler func(err error)
type ErrHandler func(error)

type Yeti struct {
Errs []error
Expand Down
8 changes: 8 additions & 0 deletions pkg/pipe/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pipe

import "github.com/koss-null/funcfrog/internal/internalpipe"

// Piper interface contains all methods of a pipe with determened length.
type Piper[T any] interface {
doer[T]
Expand All @@ -19,6 +21,7 @@ type Piper[T any] interface {
promicer[T]
eraser[Piper[any]]
snagger[Piper[T]]
yeti[Piper[T]]
}

// PiperNoLen represents methods available to a Pipe type with no length determened.
Expand All @@ -36,6 +39,7 @@ type PiperNoLen[T any] interface {

eraser[PiperNoLen[any]]
snagger[PiperNoLen[T]]
yeti[PiperNoLen[T]]
}

type paralleller[T, PiperT any] interface {
Expand Down Expand Up @@ -97,3 +101,7 @@ type promicer[T any] interface {
type snagger[PiperT any] interface {
Snag(func(error)) PiperT
}

type yeti[PiperT any] interface {
Yeti(y internalpipe.YeetSnag) PiperT
}
19 changes: 3 additions & 16 deletions pkg/pipe/yeet.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pipe

import (
"sync"

"github.com/koss-null/funcfrog/internal/internalpipe"
)

Expand All @@ -11,18 +9,7 @@ const (
initHandlersAmount = 5
)

type Yeti interface {
// yeet an error
Yeet(err error)
// snag and handle the error
Snag(handler func(err error))
}

// FIXME
func Yeet() Yeti {
return &internalpipe.Yeti{
Errs: make(map[*any][]error, initErrsAmount),
Handlers: make(map[*any][]internalpipe.ErrHandler, initHandlersAmount),
Mx: &sync.Mutex{},
}
// NewYeti creates a brand new Yeti - an object for error handling.
func NewYeti() internalpipe.YeetSnag {
return internalpipe.NewYeti()
}

0 comments on commit 3fe5550

Please sign in to comment.