{"id":18150043,"url":"https://github.com/sysulq/chain-go","last_synced_at":"2025-10-28T05:30:19.041Z","repository":{"id":254539560,"uuid":"842921747","full_name":"sysulq/chain-go","owner":"sysulq","description":"A generic operation chain for executing a series of tasks in a structured and controlled manner.","archived":false,"fork":false,"pushed_at":"2024-12-09T08:39:54.000Z","size":50,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-20T10:45:41.293Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/sysulq/chain-go","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/sysulq.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":"2024-08-15T11:53:28.000Z","updated_at":"2024-12-15T15:30:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"8b9a6225-a3d4-4055-a921-1a3753801330","html_url":"https://github.com/sysulq/chain-go","commit_stats":null,"previous_names":["sysulq/chain-go"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysulq%2Fchain-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysulq%2Fchain-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysulq%2Fchain-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sysulq%2Fchain-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sysulq","download_url":"https://codeload.github.com/sysulq/chain-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238597383,"owners_count":19498397,"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":[],"created_at":"2024-11-02T00:07:16.465Z","updated_at":"2025-10-28T05:30:13.674Z","avatar_url":"https://github.com/sysulq.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chain: A Generic Operation Chain for Go\n\n[![Go](https://github.com/sysulq/chain-go/actions/workflows/go.yml/badge.svg)](https://github.com/sysulq/chain-go/actions/workflows/go.yml)\n[![codecov](https://codecov.io/gh/sysulq/chain-go/graph/badge.svg?token=OYENB0NFMA)](https://codecov.io/gh/sysulq/chain-go)\n\n## Introduction\n\nChain is a powerful and flexible Go package that provides a generic operation chain for executing a series of tasks in a structured and controlled manner. It supports both serial and parallel execution, error handling, interceptor, and context management, making it ideal for complex workflows and data processing pipelines.\n\n## Features\n\n- **Generic Implementation**: Works with any input and output types.\n- **Serial and Parallel Execution**: Supports both sequential and concurrent task execution.\n- **Context Management**: Integrates with Go's context package for cancellation and timeout control.\n- **Error Handling**: Gracefully handles errors and stops execution on first encountered error.\n- **Thread-Safe**: Provides mutex-based synchronization for shared resource access.\n- **Timeout Control**: Allows setting a timeout for the entire chain execution.\n- **Fluent Interface**: Offers a chainable API for easy and readable setup.\n- **Interceptor Support**: Enables custom logic before and after each operation.\n\n## Installation\n\nTo install Chain, use `go get`:\n\n```bash\ngo get github.com/sysulq/chain-go\n```\n\n## Usage\n\nHere's a simple example demonstrating the basic usage of Chain:\n\n```go\npackage chain_test\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log/slog\"\n\t\"time\"\n\n\t\"github.com/sysulq/chain-go\"\n)\n\n// Input represents the input data structure\ntype Input struct {\n\tNumbers []int\n}\n\n// Output represents the output data structure\ntype Output struct {\n\tSum       int\n\tProduct   int\n\tProcessed bool\n}\n\nfunc Example() {\n\tslog.SetLogLoggerLevel(slog.LevelDebug)\n\n\t// Initialize input and output\n\tinput := \u0026Input{Numbers: []int{1, 2, 3, 4, 5}}\n\toutput := \u0026Output{}\n\n\t// Create a new chain\n\tc := chain.New(input, output).\n\t\tWithTimeout(5*time.Second).\n\t\tUse(chain.RecoverInterceptor, chain.LogInterceptor)\n\n\t// Define chain operations\n\tc.Serial(\n\t\tfunc(ctx context.Context, c *chain.State[Input, Output]) error {\n\t\t\tfmt.Println(\"Starting serial operations\")\n\t\t\treturn nil\n\t\t},\n\t\tcalculateSum,\n\t).Parallel(\n\t\tsimulateSlowOperation,\n\t\tcalculateProduct,\n\t).Serial(\n\t\tmarkProcessed,\n\t)\n\n\t// Execute the chain\n\tresult, err := c.Execute()\n\tif err != nil {\n\t\tfmt.Printf(\"Error: %v\\n\", err)\n\t\treturn\n\t}\n\n\tfmt.Printf(\"Sum: %d, Product: %d, Processed: %v\\n\", result.Sum, result.Product, result.Processed)\n\n\t// Output:\n\t// Starting serial operations\n\t// Calculating sum\n\t// Calculating product\n\t// Simulating slow operation\n\t// Marking as processed\n\t// Sum: 15, Product: 120, Processed: true\n}\n\nfunc calculateSum(ctx context.Context, c *chain.State[Input, Output]) error {\n\tfmt.Println(\"Calculating sum\")\n\tsum := 0\n\tfor _, num := range c.Input().Numbers {\n\t\tsum += num\n\t}\n\tc.SetOutput(func(o *Output) {\n\t\to.Sum = sum\n\t})\n\treturn nil\n}\n\nfunc calculateProduct(ctx context.Context, c *chain.State[Input, Output]) error {\n\tfmt.Println(\"Calculating product\")\n\tproduct := 1\n\tfor _, num := range c.Input().Numbers {\n\t\tproduct *= num\n\t}\n\tc.SetOutput(func(o *Output) {\n\t\to.Product = product\n\t})\n\treturn nil\n}\n\nfunc simulateSlowOperation(ctx context.Context, c *chain.State[Input, Output]) error {\n\tselect {\n\tcase \u003c-time.After(100 * time.Millisecond):\n\t\tfmt.Println(\"Simulating slow operation\")\n\t\treturn nil\n\tcase \u003c-ctx.Done():\n\t\treturn ctx.Err()\n\t}\n}\n\nfunc markProcessed(ctx context.Context, c *chain.State[Input, Output]) error {\n\tfmt.Println(\"Marking as processed\")\n\tc.SetOutput(func(o *Output) {\n\t\to.Processed = true\n\t})\n\treturn nil\n}\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysulq%2Fchain-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsysulq%2Fchain-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysulq%2Fchain-go/lists"}