{"id":13713922,"url":"https://github.com/mauricioklein/go-chainable","last_synced_at":"2025-05-07T00:34:08.916Z","repository":{"id":95322989,"uuid":"123349867","full_name":"mauricioklein/go-chainable","owner":"mauricioklein","description":"Chain function calls in Golang, with support to argument's feedback and error-handling","archived":false,"fork":false,"pushed_at":"2019-10-25T15:16:01.000Z","size":31,"stargazers_count":23,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-03T23:29:07.226Z","etag":null,"topics":["chainable","chainable-methods","go","golang","pipe"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mauricioklein.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-28T22:27:22.000Z","updated_at":"2024-01-05T02:16:58.000Z","dependencies_parsed_at":"2023-04-11T10:31:12.422Z","dependency_job_id":null,"html_url":"https://github.com/mauricioklein/go-chainable","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mauricioklein%2Fgo-chainable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mauricioklein%2Fgo-chainable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mauricioklein%2Fgo-chainable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mauricioklein%2Fgo-chainable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mauricioklein","download_url":"https://codeload.github.com/mauricioklein/go-chainable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224551100,"owners_count":17330070,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chainable","chainable-methods","go","golang","pipe"],"created_at":"2024-08-02T23:01:47.716Z","updated_at":"2024-11-14T01:30:15.846Z","avatar_url":"https://github.com/mauricioklein.png","language":"Go","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"# Go-chainable\n\n[![Build Status](https://travis-ci.org/mauricioklein/go-chainable.svg?branch=master)](https://travis-ci.org/mauricioklein/go-chainable)\n![](https://img.shields.io/github/release/mauricioklein/go-chainable.svg)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e00a9b448408857da9b6/maintainability)](https://codeclimate.com/github/mauricioklein/go-chainable/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/e00a9b448408857da9b6/test_coverage)](https://codeclimate.com/github/mauricioklein/go-chainable/test_coverage)\n[![GoDoc](https://godoc.org/github.com/mauricioklein/go-chainable?status.svg)](https://godoc.org/github.com/mauricioklein/go-chainable)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nProvides an easy and convenient way of chaining function calls in Golang, with support to argument's feedback and error-handling\n\n## Requirements\n\n- Go 1.9 or later\n\n## Installation\n\n```go\ngo get github.com/mauricioklein/go-chainable\n```\n\n## Basics\n\nIt's common to find Go programs similar to the one below:\n\n```go\nx, err := sum2(2)\nif err != nil {\n    return 0, err\n}\n\ny, err = mul2(x)\nif err != nil {\n    return 0, err\n}\n\nz, err = div2(y)\nif err != nil {\n    return 0, err\n}\n\nreturn z, nil\n```\n\nError handling can make the codebase messy. At the same time, chaining function calls can obfuscate\nerrors and cascading calls can increase the cognitive complexity.\n\n`Elixir`, `F#` and other languages solve this problem with the support to pipes, but Golang hasn't such\nfeature.\n\nChainable provides a clearer way to chain function calls, using the output of the previous function\nas the input of the next one. \n\nThe example above could be re-written using Chainable as follow:\n\n```go\nimport (chainable \"github.com/mauricioklein/go-chainable\")\n\nres, err := chainable.New().\n    From(2).\n    Chain(\n        sum2,\n        mul2,\n        div2,\n    ).\n    Unwrap()\n\n// \"Unwrap\" returns the result as a slice of Argument, matching the return values\n// of the last function. So, we just need to cast them to the correct type\nz := res[0].(int)\n```\n\nAnother advantage is that Chainable automatically handle errors in a chain.\n\nThus, if one of the methods returns an error as the last argument, the chain is broken\nand the error is returned by the \"Unwrap\" method:\n\n```go\nraiseError := func ()      (int, error) { return 0, errors.New(\"generic error\") }\nplus2      := func (x int)              { return x + 2 }\n\nchainable.New().\n    Chain(raiseError). // breakes the chain\n    Chain(plus2).      // never called\n    Unwrap()           // returns nil, \"a generic error\"\n```\n\nIf automatic error handling isn't desired (i.e. the error should be chained along with the other arguments),\nthe method \"ChainDummy\" should be used instead of \"Chain\"\n(pay attention that the next function in the chain\nmust be able to receive the error generated by the previous one):\n\n```go\nraiseError    := func ()               (int, error) { return 2, errors.New(\"generic error\") }\nplus2AndError := func (x int, e error) (int)        { return x + 2 }\n\nchainable.New().\n    ChainDummy(raiseError).\n    Chain(plus2AndError).\n    Unwrap() // returns []Argument{4}, nil\n```\n\n\"Chain\" and \"DummyChain\" methods are variadics, and can be used in conjunction:\n\n```go\nchainable.New().\n    Chain(f1).       // error handling enabled for f1\n    ChainDummy(f2).  // error handling disabled for f2\n    Chain(f3, f4).   // error handling enabled for f3 and f4\n    Unwrap()\n```\n\nFinally, to reset a chain and make it ready to be reused, just call the method \"Reset\":\n\n```go\nchain := chainable.New()\n\n// ... use of chain\n\nchain.Reset()\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmauricioklein%2Fgo-chainable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmauricioklein%2Fgo-chainable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmauricioklein%2Fgo-chainable/lists"}