chaining

package module
v0.0.0-...-43dcdc5 Latest Latest
Warning

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

Go to latest
Published: May 7, 2018 License: MIT Imports: 0 Imported by: 6

README

go-chaining

Simply functions chaining in Go

Methods

.New(val interface{}, err error) *Chain

create the chaining by called a func that returns a value and an error.

.Next(f func(c *Chain) (interface{}, error)) *Chain

pass chaning to next func, will ignore funcs if there is any error occured on the upstream

.NextWithFail(f func(c *Chain) (interface{}, error)) *Chain

pass chaning to next func no matter whether there got an error

.Fail(f func(err error)) *Chain

if any error has occured in the upstream, all the downstrem will be ignored until thers is a Fail handle the error.

Example

package main

import (
	"errors"
	"fmt"

	"github.com/Laisky/go-chaining"
)

func rootChainFunc() (int, error) {
	return 0, nil
}

func plus1(c *chaining.Chain) (interface{}, error) {
	return c.GetInt(), nil
}

func throwError(c *chaining.Chain) (interface{}, error) {
	return nil, errors.New("some error happened")
}

func handleError(err error) {
	fmt.Printf("deal with error: %v\n", err)
}

func main() {
	// r := chaining.New(0, nil).  // create chaining manually
	r := chaining.New(rootChainFunc()).  // create chaining by func
		Next(plus1). // +1
		Next(plus1). // +1
		Next(throwError). // will interupt chain
		Next(plus1).
		Next(plus1).
		Fail(handleError). // will recover chain
		Next(plus1). // +1
		Next(throwError).  // interupt again
		Next(plus1).
		NextWithFail(plus1).  // +1, deal error by yourself
		Next(throwError)  // throw error

	r.GetInt() // got 4
	r.GetError()  // got Error("some error happened")
}

Or you can use Flow:

// Flow chaining your funcs.
// but you should deal with your error by yourself in funcs
Flow(fs ...func(*Chain) (interface{}, error)) func(interface{}, error) (c *Chain)
c := chaining.Flow(
	plus1, // +1
	throwError,  // error will pass to the downstream funcs
	plus1, // +1, and vanish the error (you should return error manually in the func)
	plus1, // +1
)(0, nil)

c.GetInt()  // got 4
c.GetError()  // got nil

API References

chaining
type Chain struct

New(val interface{}, err error) *Chain
Flow(fs ...func(*Chain) (interface{}, error)) func(interface{}, error) (c *Chain)
chaining.Chain

There are many convenient methods to get the value from chaining:


.Next(f func(c *Chain) (interface{}, error)) *Chain
.NextWithFail(f func(c *Chain) (interface{}, error)) *Chain
.Fail(f func(err error)) *Chain

.GetError() error
.GetVal() interface{}
.GetString() string
.GetInt() int
.GetInt32() int32
.GetInt64() int64
.GetFloat32() float32
.GetFloat64() float64
.GetBool() bool
.GetSliceString() []string
.GetSliceInterface() []interface{}
.GetMapStringString() map[string]string
.GetMapStringInterface() map[string]interface{}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flow

func Flow(fs ...func(*Chain) (interface{}, error)) func(interface{}, error) (c *Chain)

Flow chaining funcs

Types

type Chain

type Chain struct {
	// contains filtered or unexported fields
}

func New

func New(val interface{}, err error) *Chain

New is the root of the channing New(func(args...))

func (*Chain) Fail

func (c *Chain) Fail(f func(err error)) *Chain

Fail deal with the first error that occured at the upstream of the chain

func (*Chain) GetBool

func (c *Chain) GetBool() bool

GetBool get the value in bool

func (*Chain) GetError

func (c *Chain) GetError() error

GetError get the error

func (*Chain) GetFloat32

func (c *Chain) GetFloat32() float32

GetFloat32 get the value in float32

func (*Chain) GetFloat64

func (c *Chain) GetFloat64() float64

GetFloat64 get the value in float64

func (*Chain) GetInt

func (c *Chain) GetInt() int

GetInt get the value in int

func (*Chain) GetInt32

func (c *Chain) GetInt32() int32

GetInt32 get the value in int32

func (*Chain) GetInt64

func (c *Chain) GetInt64() int64

GetInt64 get the value in int64

func (*Chain) GetMapStringInterface

func (c *Chain) GetMapStringInterface() map[string]interface{}

GetMapStringInterface get the value in map[string]interface{}

func (*Chain) GetMapStringString

func (c *Chain) GetMapStringString() map[string]string

GetMapStringString get the value in map[string]string

func (*Chain) GetSliceInterface

func (c *Chain) GetSliceInterface() []interface{}

GetSliceInterface get the value in []interface{}

func (*Chain) GetSliceString

func (c *Chain) GetSliceString() []string

GetSliceString get the value in []string

func (*Chain) GetString

func (c *Chain) GetString() string

GetString get the value in string

func (*Chain) GetVal

func (c *Chain) GetVal() interface{}

GetVal get the value in interface{}

func (*Chain) Next

func (c *Chain) Next(f func(c *Chain) (interface{}, error)) *Chain

Next pass the result of the upstream to the next func if any error occured at any point of the upstream, no downstream will be involved, until there is a `Fail` deal with the error

func (*Chain) NextWithFail

func (c *Chain) NextWithFail(f func(c *Chain) (interface{}, error)) *Chain

NextWithFail is Similiar to Next, the different is NextWithFail will ignore the error that happended in the upstream. You should deal with the fail by yourself

Jump to

Keyboard shortcuts

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