Giter Club home page Giter Club logo

orrer's Introduction

Orrer

To prefer being terse over scattered error handling in Go. Used preferably in more than three or four consequent error checking.

Api

Types

Fn func() (interface{}, error)

The building block for using the whole library. This is a wrapper function to generalize handling result or error.

FnArg func(interface{}) (interface{}, error)

Same as Fn but accepts an input as argument.

Functions

GetAny(errors ...error) error

Returns an error if either of errors passed to it is not nil. Otherwise the return value would be nil.

Use case:

There are function calls that either return error or update a pointer passed to them the usual Go way to deal with them is:

if err := fooFunc(&fooStruct); err != nil {
    return err
}

if err := barFunc(&barStruct); err != nil {
    return err
}

if err := bazFun(&someOtherStructAgain); err != nil {
    return err
}

return nil

We can summarize them as:

return GetAny(fooFunc(&foorStruct), barFunc(&barStruct), bazFun(&someOtherStructAgain))
GetValsOrError(fns ...Fn) ([]interface{}, error)

Runs a series of functions; in case any of functions return an error, it will be returned, otherwise results is returned in an array.

Use case:

There are function calls that either return error and nil for a value or return a value with a nil error:

if v1, err := fooFunc(fooVal); err != nil {
    return err
}

if v2, err := barFunc(barVal); err != nil {
    return err
}

if v3, err := bazFun(bazVal); err != nil {
    return err
}

return nil

We can summarize (really?) them as:

vals, err: = GetValsOrError(
    func () (interface{}, error) {
        return sayHi("John")
    },
    func () (interface{}, error) {
        return sayBy("John")
    },
    func () (interface{}, error) {
        return sayError("John")
    }
)
println(err) // The error returned by sayError is printed

vals, err: = GetValsOrError(
    func () (interface{}, error) {
        return sayHi("John")
    },
    func () (interface{}, error) {
        return sayBy("John")
    }
)
println(err) // nil
println(vals[0].(string)) // Hi John
GoGetValsOrError(fns ...Fn) ([]interface{}, error)

Same as GetValsOrError, just runs the functions in go routines and waits till either all results are ready in the results array, or an error is returned from any functions.

GetSeriesOrError(kickOff interface{}, fns ...FnArg) (interface{}, error)

Runs a series of functions passing the returning value of first one to the second and so on

Use case:

When you want to run a series of functions passing the first output as second input and so on. If any errors occurs then that error is returned instead.

// We have these functions for demonstration------------
func AddOne(inp int) (int, error) {
	return inp + 1, nil
}

func AddTwo(inp int) (int, error) {
	return inp + 2, nil
}

func MulBy3(inp int) (int, error) {
	return inp * 3, nil
}
//------------------------------------------------------

v1, err := AddOne(1)
if err != nil {
	return nil
}
v2, err := MulBy3(v1)
if err != nil {
    return nil
}
v3, err := AddTwo(v2)
if err != nil {
    return nil
}

println(v3) // 8

We can summarize them as:

if v, err := GetSeriesOrError(
    1,
    func(i interface{}) (interface{}, error) {
        return AddOne(i.(int))
    },
    func(i interface{}) (interface{}, error) {
        return MulBy3(i.(int))
    },
    func(i interface{}) (interface{}, error) {
        return AddTwo(i.(int))
    },
); err != nil {
	println(v) // 8
}

The usefulness of package is more apparent when dealing with lots of errors to check.

For getting more familiar with the usage, take a look at the tests.

orrer's People

Contributors

jamiri avatar

Stargazers

 avatar Javad avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.