{"id":15693495,"url":"https://github.com/tdakkota/astbuilders","last_synced_at":"2025-05-08T03:58:07.528Z","repository":{"id":37029758,"uuid":"286154386","full_name":"tdakkota/astbuilders","owner":"tdakkota","description":"Go AST utility package","archived":false,"fork":false,"pushed_at":"2023-10-06T19:16:37.000Z","size":112,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-08T03:58:02.217Z","etag":null,"topics":["ast","builders","codegen","codegeneration","go","golang"],"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/tdakkota.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":"2020-08-09T02:40:24.000Z","updated_at":"2024-04-11T20:17:49.000Z","dependencies_parsed_at":"2024-06-21T15:19:51.699Z","dependency_job_id":"5ec09a77-584a-46b2-864a-bfa0cb3d5a7b","html_url":"https://github.com/tdakkota/astbuilders","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdakkota%2Fastbuilders","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdakkota%2Fastbuilders/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdakkota%2Fastbuilders/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdakkota%2Fastbuilders/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tdakkota","download_url":"https://codeload.github.com/tdakkota/astbuilders/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252996334,"owners_count":21837621,"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":["ast","builders","codegen","codegeneration","go","golang"],"created_at":"2024-10-03T18:44:26.730Z","updated_at":"2025-05-08T03:58:07.507Z","avatar_url":"https://github.com/tdakkota.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# astbuilders\n\n[![Go](https://github.com/tdakkota/astbuilders/workflows/Go/badge.svg)](https://github.com/tdakkota/astbuilders/actions)\n[![Documentation](https://godoc.org/github.com/tdakkota/astbuilders?status.svg)](https://pkg.go.dev/github.com/tdakkota/astbuilders)\n[![codecov](https://codecov.io/gh/tdakkota/astbuilders/branch/master/graph/badge.svg)](https://codecov.io/gh/tdakkota/astbuilders)\n[![license](https://img.shields.io/github/license/tdakkota/astbuilders.svg)](https://github.com/tdakkota/astbuilders/blob/master/LICENSE)\n\nGo AST utility package \n\n## Install\n```\ngo get github.com/tdakkota/astbuilders\n```\n\n## Examples\n\n### Creating a function\n```go\npackage main\n\nimport (\n\t\"go/printer\"\n\t\"go/token\"\n\t\"os\"\n\n\t\"github.com/tdakkota/astbuilders\"\n)\n\nfunc main() {\n\tnode := builders.NewFunctionBuilder(\"main\").\n\t\tBody(func(s builders.StatementBuilder) builders.StatementBuilder {\n\t\t\treturn s.Expr(builders.CallPackage(\"fmt\", \"Println\", builders.StringLit(\"Hello, world!\")))\n\t\t}).\n\t\tCompleteAsDecl()\n\n\tprinter.Fprint(os.Stdout, token.NewFileSet(), node)\n}\n```\nprints\n```go\nfunc main() {\n    fmt.Println(\"Hello, world!\")\n}\n```\n\n### `if err != nil`\n```go\npackage main\n\nimport (\n\t\"go/ast\"\n\t\"go/printer\"\n\t\"go/token\"\n\t\"os\"\n\t\n\t\"github.com/tdakkota/astbuilders\"\n)\n\nfunc main() {\n\terrIdent := ast.NewIdent(\"err\")\n\tnilIdent := ast.NewIdent(\"nil\")\n\tcond := builders.NotEq(errIdent, nilIdent)\n\n\ts := builders.NewStatementBuilder()\n\ts = s.If(nil, cond, func(body builders.StatementBuilder) builders.StatementBuilder {\n\t\treturn body.Return(errIdent)\n\t})\n\n\tstmts := s.Complete()\n\tnode := stmts[0]\n\tprinter.Fprint(os.Stdout, token.NewFileSet(), node)\n}\n```\nprints\n```go\nif err != nil {\n\treturn err\n}\n```\n\n### Reverse from SliceTricks\n```go\npackage main\n\nimport (\n\t\"go/ast\"\n\t\"go/printer\"\n\t\"go/token\"\n\t\"os\"\n\t\n\t\"github.com/tdakkota/astbuilders\"\n)\n\nfunc main() {\n\ts := builders.NewStatementBuilder()\n\n\ta := ast.NewIdent(\"a\")\n\tleft, right := ast.NewIdent(\"left\"), ast.NewIdent(\"right\")\n\tone := builders.IntegerLit(1)\n\n\t// left, right := 0, len(a)-1\n\tinit := builders.Define(left, right)(builders.IntegerLit(0), builders.Sub(builders.Len(a), one))\n\t// left \u003c right\n\tcond := builders.Less(left, right)\n\t// left, right = left+1, right-1\n\tpost := builders.Assign(left, right)(token.ASSIGN)(builders.Add(left, one), builders.Sub(right, one))\n\n\t// a[left]\n\tindexLeft := builders.Index(a, left)\n\t// a[right]\n\tindexRight := builders.Index(a, right)\n\n\t// for $init; $cond; $post {\n\t// for left, right := 0, len(a)-1; left \u003c right; left, right = left+1, right-1 {\n\ts = s.For(init, cond, post, func(loop builders.StatementBuilder) builders.StatementBuilder {\n\t\t// a[left], a[right] = a[right], a[left]\n\t\tloop = loop.AddStmts(builders.Swap(indexLeft, indexRight))\n\t\treturn loop\n\t})\n\n\tstmts := s.Complete()\n\tnode := stmts[0]\n\tprinter.Fprint(os.Stdout, token.NewFileSet(), node)\n}\n```\n\nprints\n\n```go\nfor left, right := 0, len(a)-1; left \u003c right; left, right = left+1, right-1 {\n    a[left], a[right] = a[right], a[left]\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdakkota%2Fastbuilders","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftdakkota%2Fastbuilders","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdakkota%2Fastbuilders/lists"}