phos

package module
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

PHOS

Go Report Card

You Can (Not) Redo

PHOS

PHOS is a channel with internal handlers and diversified options.

Install

go get github.com/B1NARY-GR0UP/phos

Quick Start

package main

import (
    "context"
    "fmt"

    "github.com/B1NARY-GR0UP/phos"
)

func hello(_ context.Context, data string) (string, error) {
    return data + "-PHOS", nil
}

func main() {
    ph := phos.New[string]()
    defer ph.Close()
    ph.Append(hello)
    ph.In <- "BINARY"
    res := <-ph.Out
    ph.Delete(ph.Len() - 1)
    fmt.Println(res.Data)
}

Configuration

Option Default Description Example
WithContext context.Background() Set context for PHOS example
WithZero false Set zero value for return when error happened example
WithTimeout 3 * time.Second Set timeout for handlers execution example
WithErrHandleFunc nil Set error handle function for PHOS which will be called when handle error happened example
WithErrTimeoutFunc nil Set error timeout function for PHOS which will be called when timeout error happened example
WithErrDoneFunc nil Set err done function for PHOS which will be called when context done happened example

Blogs

License

PHOS is distributed under the Apache License 2.0. The licenses of third party dependencies of PHOS are explained here.

ECOLOGY

BMS

PHOS is a Subproject of the Basic Middleware Service

Documentation

Index

Constants

View Source
const (
	Name    = "PHOS"
	Version = "v0.3.3"
)

BINARY-PHOS

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrDoneFunc added in v0.1.1

type ErrDoneFunc func(ctx context.Context, data any, err error) any

type ErrHandleFunc

type ErrHandleFunc func(ctx context.Context, data any, err error) any

type ErrTimeoutFunc

type ErrTimeoutFunc func(ctx context.Context, data any) any

type Error

type Error struct {
	Err  error
	Type ErrorType
}

Error for PHOS Error implements the error interface

func (*Error) Error

func (e *Error) Error() string

Error returns the error string

type ErrorType

type ErrorType uint64

ErrorType for PHOS Error

const (
	TimeoutErr ErrorType
	HandlerErr
	CtxErr
)

type Handler

type Handler[T any] func(ctx context.Context, data T) (T, error)

Handler handles the data of PHOS channel

type Option

type Option func(o *Options)

Option for PHOS

func WithContext

func WithContext(ctx context.Context) Option

WithContext will set context for PHOS

func WithErrDoneFunc added in v0.1.1

func WithErrDoneFunc(fn ErrDoneFunc) Option

WithErrDoneFunc will set err done function for PHOS which will be called when ctx done happened Note: You should use it will WithContext, otherwise it will not work

func WithErrHandleFunc

func WithErrHandleFunc(fn ErrHandleFunc) Option

WithErrHandleFunc will set error handle function for PHOS which will be called when handle error happened

func WithErrTimeoutFunc

func WithErrTimeoutFunc(fn ErrTimeoutFunc) Option

WithErrTimeoutFunc will set error timeout function for PHOS which will be called when timeout error happened

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout will set timeout for the handler chain execution (not just for each handler)

func WithZero

func WithZero() Option

WithZero will set zero value for return when error happened

type Options

type Options struct {
	Ctx            context.Context
	Zero           bool
	Timeout        time.Duration
	ErrHandleFunc  ErrHandleFunc
	ErrTimeoutFunc ErrTimeoutFunc
	ErrDoneFunc    ErrDoneFunc
}

Options for PHOS

type Phos

type Phos[T any] struct {
	In  chan<- T
	Out <-chan Result[T]
	// contains filtered or unexported fields
}

Phos short for Phosphophyllite PHOS is a channel with internal handler chain

func New

func New[T any](opts ...Option) *Phos[T]

New PHOS channel

func (*Phos[T]) Append added in v0.2.0

func (ph *Phos[T]) Append(handlers ...Handler[T])

Append add handler for PHOS to execute

func (*Phos[T]) Close added in v0.2.1

func (ph *Phos[T]) Close()

Close PHOS channel Note: You should not close In channel manually before or after calling Close

func (*Phos[T]) Delete added in v0.3.2

func (ph *Phos[T]) Delete(index int)

Delete handler according to the index

func (*Phos[T]) Len added in v0.2.2

func (ph *Phos[T]) Len() int

Len return the number of handlers

func (*Phos[T]) Remove added in v0.2.2

func (ph *Phos[T]) Remove(index int)

Remove handler from PHOS Deprecated: use Delete instead

type Result

type Result[T any] struct {
	Data T
	// Note: You should use the OK of Result rather than the second return value of PHOS Out channel
	OK  bool
	Err *Error
}

Result PHOS output result

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL