{"id":16304831,"url":"https://github.com/moznion/gowrtr","last_synced_at":"2025-03-16T14:30:32.096Z","repository":{"id":34011922,"uuid":"164806669","full_name":"moznion/gowrtr","owner":"moznion","description":"gowrtr is a library that supports golang code generation","archived":false,"fork":false,"pushed_at":"2022-08-22T08:04:15.000Z","size":320,"stargazers_count":114,"open_issues_count":2,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-27T09:51:08.085Z","etag":null,"topics":["code-generation","go","golang"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/moznion/gowrtr/generator","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-09T06:56:45.000Z","updated_at":"2025-01-27T13:15:56.000Z","dependencies_parsed_at":"2022-08-07T23:31:22.172Z","dependency_job_id":null,"html_url":"https://github.com/moznion/gowrtr","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/moznion%2Fgowrtr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgowrtr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgowrtr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgowrtr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moznion","download_url":"https://codeload.github.com/moznion/gowrtr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243818171,"owners_count":20352629,"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-generation","go","golang"],"created_at":"2024-10-10T21:04:56.303Z","updated_at":"2025-03-16T14:30:31.788Z","avatar_url":"https://github.com/moznion.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"gowrtr [![CircleCI](https://circleci.com/gh/moznion/gowrtr.svg?style=svg)](https://circleci.com/gh/moznion/gowrtr) [![codecov](https://codecov.io/gh/moznion/gowrtr/branch/master/graph/badge.svg)](https://codecov.io/gh/moznion/gowrtr) [![GoDoc](https://godoc.org/github.com/moznion/gowrtr/generator?status.svg)](https://godoc.org/github.com/moznion/gowrtr/generator) [![Go Report Card](https://goreportcard.com/badge/github.com/moznion/gowrtr)](https://goreportcard.com/report/github.com/moznion/gowrtr)\n==\n\ngowrtr (pronunciation:`go writer`) is a library that supports golang code generation. This library is [go generics](https://go.dev/doc/tutorial/generics) ready!\n\nThis library is inspired by [square/javapoet](https://github.com/square/javapoet).\n\nSynopsis\n--\n\nHere is a simple example:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/moznion/gowrtr/generator\"\n)\n\nfunc main() {\n\tgenerator := generator.NewRoot(\n\t\tgenerator.NewComment(\" THIS CODE WAS AUTO GENERATED\"),\n\t\tgenerator.NewPackage(\"main\"),\n\t\tgenerator.NewNewline(),\n\t).AddStatements(\n\t\tgenerator.NewFunc(\n\t\t\tnil,\n\t\t\tgenerator.NewFuncSignature(\"main\"),\n\t\t).AddStatements(\n\t\t\tgenerator.NewRawStatement(`fmt.Println(\"hello, world!\")`),\n\t\t),\n\t).\n\t\tGofmt(\"-s\").\n\t\tGoimports()\n\n\tgenerated, err := generator.Generate(0)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(generated)\n}\n```\n\nthen it generates the golang code like so:\n\n```go\n// THIS CODE WAS AUTO GENERATED\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n        fmt.Println(\"hello, world!\")\n}\n```\n\nGenerics example is here:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/moznion/gowrtr/generator\"\n)\n\nfunc main() {\n\tgenerated, err := generator.NewRoot(\n\t\tgenerator.NewComment(\" THIS CODE WAS AUTO GENERATED\"),\n\t\tgenerator.NewPackage(\"main\"),\n\t\tgenerator.NewNewline(),\n\t\tgenerator.NewStruct(\"MyStruct\").\n\t\t\tTypeParameters(TypeParameters{\n\t\t\t\tgenerator.NewTypeParameter(\"T\", \"any\"),\n\t\t\t}).AddField(\"anything\", \"T\"),\n\t\tgenerator.NewNewline(),\n\t\tgenerator.NewFunc(\n\t\t\tnil,\n\t\t\tgenerator.NewFuncSignature(\"foo\").\n\t\t\t\tTypeParameters(TypeParameters{\n\t\t\t\t\tgenerator.NewTypeParameter(\"U\", \"any\"),\n\t\t\t\t}).\n\t\t\t\tReturnTypeStatements(generator.NewFuncReturnTypeWithGenerics(\"MyStruct\", generator.TypeParameterNames{\"U\"})),\n\t\t).AddStatements(generator.NewComment(\"do something\")),\n\t\tgenerator.NewNewline(),\n\t\tgenerator.NewFunc(\n\t\t\tgenerator.NewFuncReceiverWithGenerics(\"s\", \"MyStruct\", generator.TypeParameterNames{\"T\"}),\n\t\t\tgenerator.NewFuncSignature(\"bar\").\n\t\t\t\tReturnTypeStatements(generator.NewFuncReturnTypeWithGenerics(\"MyStruct\", generator.TypeParameterNames{\"T\"})),\n\t\t).AddStatements(generator.NewComment(\"do something\")),\n\t).\n\t\tGofmt(\"-s\").\n\t\tGoimports().\n\t\tGenerate(0)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(generated)\n}\n```\n\nthen it generates the following golang code:\n\n```go\n// THIS CODE WAS AUTO GENERATED\npackage main\n\ntype MyStruct[T any] struct {\n\tanything T\n}\n\nfunc foo[U any]() MyStruct[U] {\n\t//do something\n}\n\nfunc (s MyStruct[T]) bar() MyStruct[T] {\n\t//do something\n}\n```\n\nAnd [GoDoc](https://godoc.org/github.com/moznion/gowrtr/generator) shows you a greater number of examples.\n\nDescription\n--\n\nPlease refer to the godoc: [![GoDoc](https://godoc.org/github.com/moznion/gowrtr/generator?status.svg)](https://godoc.org/github.com/moznion/gowrtr/generator)\n\n### Root\n\n- `Root` is an entry point to generate the go code.\n- `Root` supports following code formatting on code generating phase. It applies such formatters to generated code.\n  - `gofmt`: with `Gofmt(gofmtOptions ...string)`\n  - `goimports`: with `Goimports()`\n\n### Immutability\n\nMethods of this library act as immutable. It means it doesn't change any internal state implicitly, so you can take a snapshot of the code generator. That is useful to reuse and derive the code generator instance.\n\n### Debug friendly\n\nThis library shows \"where is a cause of the error\" when code generator raises an error. This means each error message contains a pointer for the error source (i.e. file name and the line number).  This should be helpful for debugging.\n\nError messages example:\n\n```\n[GOWRTR-14] condition of case must not be empty, but it gets empty (caused at /tmp/main.go:22)\n```\n\n### Supported syntax\n\n- [x] `package`\n- [x] `import`\n- [x] `struct`\n  - [x] generics type parameters\n- [x] `interface`\n  - [x] generics type parameters\n- [x] [composite literal](https://golang.org/doc/effective_go.html#composite_literals)\n- [x] `if`\n  - [x] `else if`\n  - [x] `else`\n- [x] `switch`\n  - [x] `case`\n  - [x] `default`\n- [x] `for`\n- [x] code block\n- [x] `func`\n  - [x] generics type parameters on the signature\n  - [x] generics type names on the receiver\n  - [x] generics type names on the return types\n  - [x] generics types on the invocation\n- [x] anonymous func\n  - [x] immediately invoking\n- one line statement\n  - [x] raw\n  - [x] newline\n  - [x] `return`\n  - [x] `comment`\n- [x] union types in the generics type parameters\n\nFor developers of this library\n--\n\n### Setup development environment\n\n```\n$ make bootstrap\n```\n\n### How to define and generate error messages\n\nPlease edit `internal/errmsg/errmsg.go` and execute `make errgen`.\n\nSee also: [moznion/go-errgen](https://github.com/moznion/go-errgen)\n\nBlog posts\n--\n\n- English: [gowrtr - a library that supports golang code generation](https://moznion.hatenablog.jp/entry/2019/01/15/094236)\n- Japanese: [gowrtr - goコード生成支援ライブラリ](https://moznion.hatenadiary.com/entry/2019/01/14/111719)\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%2Fgowrtr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoznion%2Fgowrtr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fgowrtr/lists"}