{"id":24919761,"url":"https://github.com/steinfletcher/gonum","last_synced_at":"2025-04-09T18:08:45.914Z","repository":{"id":57500422,"uuid":"180130849","full_name":"steinfletcher/gonum","owner":"steinfletcher","description":"An enum generator for Go","archived":false,"fork":false,"pushed_at":"2020-12-03T09:48:11.000Z","size":44,"stargazers_count":28,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-23T20:04:47.467Z","etag":null,"topics":["enum","enumeration","generator","go","golang","java"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steinfletcher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-08T11:03:07.000Z","updated_at":"2023-04-22T14:05:26.000Z","dependencies_parsed_at":"2022-08-30T20:50:50.222Z","dependency_job_id":null,"html_url":"https://github.com/steinfletcher/gonum","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgonum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgonum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgonum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgonum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steinfletcher","download_url":"https://codeload.github.com/steinfletcher/gonum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248084387,"owners_count":21045125,"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":["enum","enumeration","generator","go","golang","java"],"created_at":"2025-02-02T10:37:26.401Z","updated_at":"2025-04-09T18:08:45.893Z","avatar_url":"https://github.com/steinfletcher.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gonum\n\n**I don't recommend using this in production, but rather as an example of how one could go about using AST to generate code. I am not planning to maintain this or add any new features.**\n\n`gonum` is an enum generator for Go. It is inspired by the powerful enum types found in Java. `gonum` has the following capabilities\n\n* Reference and compare enums using values\n* Provide a display value for the enumerated fields\n* Generate an enum instance from a string factory method\n* Generate a slice of display values\n* JSON support\n* Enum instances can be passed as errors since they implement `Error() string`\n\n## Install\n\nFrom a github release\n\n```bash\ncurl https://raw.githubusercontent.com/steinfletcher/gonum/master/download.sh | sh\nmv bin/gonum /usr/local/bin\n```\n\nOR\n\n```bash\ngo get -u github.com/steinfletcher/gonum\n```\n\n## Example\n\nTo define an enum, create a `struct` with the suffix `Enum`. You can define a display value in the `struct` tag. Adding a hyphen will assign the field name to the display value.\n\nYou can then generate the enum as follows.\n\n```go\n//go:generate gonum -types=ColorEnum,StatusEnum,SushiEnum\n\n// generate an enum with display values. The display values are used for JSON serialization/deserialization\ntype ColorEnum struct {\n\tRed       string `enum:\"RED\"`\n\tLightBlue string `enum:\"LIGHT_BLUE\"`\n}\n\n// generate an enum with default display values. The display values are set to the field names, e.g. `On` and `Off`\ntype StatusEnum struct {\n\tOn  string `enum:\"-\"`\n\tOff string `enum:\"-\"`\n}\n\n// generate an enum with display values and descriptions.\ntype SushiEnum struct {\n\tMaki    string `enum:\"MAKI,Rice and filling wrapped in seaweed\"`\n\tTemaki  string `enum:\"TEMAKI,Hand rolled into a cone shape\"`\n\tSashimi string `enum:\"SASHIMI,Fish or shellfish served alone without rice\"`\n}\n```\n\nWhen a description is defined the json is serialized as follows (not yet implemented)\n\n```json\n{\n  \"sushi\": {\n    \"name\": \"SASHIMI\",\n    \"description\": \"Fish or shellfish served alone without rice\"\n  }\n}\n```\n\n## Consumer api\n\nThe generated code would yield the following consumer api\n\n### Create an enum value\n\n```go\na := Red // OR var a Color = Red\n```\n\n### Create an enum from a factory method\n\n```go\nvar name Color = NewColor(\"RED\")\n```\n\n### Get the display value\n\n```go\nvar name string = a.Name() // \"RED\"\n```\n\n### Get all display values\n\n```go\nvar names []string = ColorNames() // []string{\"RED\", \"BLUE\"}\n```\n\n### Get all values\n\n```go\nvar values []Color = ColorValues() // []string{Red, Blue}\n```\n\n### Pass as an error\n\nEnums implement `Error() string` which means they can be passed as errors.\n\n```go\nvar a error = Red\n```\n\n## Developing\n\n```bash\ngo build gonum.go\ngo generate\ngo test .\n```\n\nOR\n\n```bash\nmake test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteinfletcher%2Fgonum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteinfletcher%2Fgonum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteinfletcher%2Fgonum/lists"}