{"id":15035390,"url":"https://github.com/moznion/go-errgen","last_synced_at":"2025-04-10T00:21:26.734Z","repository":{"id":57490934,"uuid":"164061767","full_name":"moznion/go-errgen","owner":"moznion","description":"A golang code generator for errors from the definitions","archived":false,"fork":false,"pushed_at":"2020-01-19T14:01:41.000Z","size":50,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T02:05:13.243Z","etag":null,"topics":["code-generator","error","golang"],"latest_commit_sha":null,"homepage":null,"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/moznion.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}},"created_at":"2019-01-04T05:22:19.000Z","updated_at":"2024-12-02T22:53:15.000Z","dependencies_parsed_at":"2022-08-29T20:31:30.251Z","dependency_job_id":null,"html_url":"https://github.com/moznion/go-errgen","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-errgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-errgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-errgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-errgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moznion","download_url":"https://codeload.github.com/moznion/go-errgen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248132121,"owners_count":21052977,"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":["code-generator","error","golang"],"created_at":"2024-09-24T20:28:31.214Z","updated_at":"2025-04-10T00:21:26.713Z","avatar_url":"https://github.com/moznion.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"errgen\n==\n\n[![Build Status](https://travis-ci.org/moznion/go-errgen.svg?branch=master)](https://travis-ci.org/moznion/go-errgen)\n\n`errgen` is a code generator. This library generates functions that return error according to the definition described in the `struct`.\n\nAnd the function can receive variables as the function parameters. The parameters will be bound into placeholders that are compatible with sprintf.\n\nPlease refer to the [synopsis](#Synopsis) for a concrete example.\n\nInstallation\n--\n\n```shell\n$ go get -u github.com/moznion/go-errgen/cmd/errgen\n```\n\nAnd this library requires `goimports` that is on executable path.\n\nUsage\n--\n\n```\nUsage of errgen:\n  -out-file string\n        [optional] the output destination path of the generated code (default \"\u003csnake_case_struct_name\u003e_errmsg_gen.go\")\n  -prefix string\n        [optional] prefix of error type (default \"ERR-\")\n  -type string\n        [mandatory] struct type name of source of error definition\n  -version\n        show version and revision\n```\n\nSynopsis\n--\n\nDefine errors and configure with `go:generate`:\n\n```go\npackage mypkg\n\n//go:generate errgen -type=myErrors\ntype myErrors struct {\n\tFooErr error `errmsg:\"this is FOO error\"`\n\tBarErr error `errmsg:\"this is BAR error [%d, %s]\" vars:\"hoge int, fuga string\"`\n}\n```\n\nAnd execute go generate:\n\n```shell\n$ go generate ./...\n```\n\nThen it generates `my_errors_errmsg_gen.go`. That has the following contents:\n\n```go\n// This package was auto generated.\n// DO NOT EDIT BY YOUR HAND!\n\npackage mypkg\n\nimport \"errors\"\nimport \"fmt\"\n\nfunc FooErr() error {\n\treturn errors.New(`[ERR-1] this is FOO error`)\n}\n\nfunc BarErr(hoge int, fuga string) error {\n\treturn fmt.Errorf(`[ERR-2] this is BAR error [%d, %s]`, hoge, fuga)\n}\n\nfunc MyErrorsList() []string {\n\treturn []string{\n\t\t`[ERR-1] this is FOO error`,\n\t\t`[ERR-2] this is BAR error [%d, %s]`,\n\t}\n}\n```\n\nCustom Tag Syntax\n--\n\nexample:\n\n```go\ntype myErrors struct {\n\tFooErr error `errmsg:\"this is FOO error [%d, %s]\" vars:\"hoge int, fuga string\"`\n\tBarErr error `errmsg:\"this is BAR error\" obsoleted:\"true\"`\n}\n```\n\n### `errmsg`\n\n- This is a __mandatory__ parameter\n- Generated function returns this value\n- This parameter supports `sprintf` style placeholder\n  - If you use the placeholder, you should use the `vars` parameter together\n\n### `vars`\n\n- This is an optional parameter\n- The generated function uses this value as a function parameter\n  - i.e. this parameter must be the valid syntax of golang's function parameter\n- And variables that are described by this parameter will be filled into `sprintf` style placeholders of `errmsg`\n\n### `obsoleted`\n\n- This is an optional parameter\n- If this parameter is __not empty__, the error message won't be generated as go code\n  - But it increments a serial number of prefix\n  - This parameter is useful for excluding the error message from the target of code generation\n    - See also [Notes](#Notes)\n\nNotes\n--\n\n- errgen will automatically give a prefix to each error message\n  - The prefix contains a serial number\n    - It is useful to determine the error type\n    - So you should not...\n      - Remove an error message definition from the struct\n      - Change order of error messages\n    - =\u003e __You should only append error definitions__\n      - Please consider using `obsoleted` parameter to exclude an error message from the target of code generation\n\nFAQ\n--\n\n### Why don't use map like structure instead of struct?\n\nA map doesn't ensure the order of members. That is a bit inconvenient to generate error messages with a prefix that contains the serial number.\n\n### Why don't use slice like structure instead of struct?\n\nIt's a good point. But I (i.e. moznion) don't want to consider the unbalanced slice members; unbalanced means odd number of members.\n\nLicense\n--\n\n```\nThe MIT License (MIT)\nCopyright © 2019 moznion, http://moznion.net/ \u003cmoznion@gmail.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fgo-errgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoznion%2Fgo-errgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fgo-errgen/lists"}