{"id":35178293,"url":"https://github.com/toniphan21/go-mapper-gen","last_synced_at":"2026-01-13T21:09:32.145Z","repository":{"id":330893006,"uuid":"1112543600","full_name":"toniphan21/go-mapper-gen","owner":"toniphan21","description":"A type-safe code generator for Go that automates mappings between different struct types.","archived":false,"fork":false,"pushed_at":"2026-01-11T15:03:42.000Z","size":591,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-11T18:04:09.699Z","etag":null,"topics":["code-generator","compiled-time","generator","go","golang","mapper","mapping","pkl","structs"],"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/toniphan21.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-08T19:15:26.000Z","updated_at":"2026-01-11T15:03:46.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/toniphan21/go-mapper-gen","commit_stats":null,"previous_names":["toniphan21/go-mapper-gen"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/toniphan21/go-mapper-gen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniphan21%2Fgo-mapper-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniphan21%2Fgo-mapper-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniphan21%2Fgo-mapper-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniphan21%2Fgo-mapper-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toniphan21","download_url":"https://codeload.github.com/toniphan21/go-mapper-gen/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniphan21%2Fgo-mapper-gen/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28400916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","compiled-time","generator","go","golang","mapper","mapping","pkl","structs"],"created_at":"2025-12-28T23:44:04.733Z","updated_at":"2026-01-13T21:09:32.140Z","avatar_url":"https://github.com/toniphan21.png","language":"Go","funding_links":["https://buymeacoffee.com/toniphan21"],"categories":[],"sub_categories":[],"readme":"## go-mapper-gen\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/toniphan21/go-bf)](https://goreportcard.com/report/github.com/toniphan21/go-bf)\n\n--- \n\n**go-mapper-gen** is a type-safe code generator for Go that automates mappings between different struct types such\nas domain entities ↔︎ database models or API ↔︎ internal representations.\n\nIt uses [pkl](https://pkl-lang.org) as a modern, schema-driven configuration language, providing strong validation,\nclear semantics, and IDE auto-completion when defining mappings.\n\n### Highlights\n\n- 🔒 Type-safe code generation with compile-time guarantees.\n- 🧘 Flexible output: functions or interface/implementation pairs.\n- 💪 Strongly typed [pkl](https://pkl-lang.org) configuration schema with IDE support.\n- 🧰 Built-in support for common field types, extensible via custom functions.\n- 🛠️ Can be used as a standalone CLI or embedded as a Go library.\n\n---\n\n### Quickstart\n\nFirstly, let set up a project which uses `sqlc` and `pgtype`\n\n```go.mod\nmodule github.com/toniphan21/go-mapper-gen/basic\n\ngo 1.25\n\nrequire github.com/jackc/pgx/v5 v5.7.6\n```\n\nthe `go.sum` file is\n\n```go.sum\ngithub.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=\ngithub.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=\n```\n\nGiven that you have an entity located in your `domain` package:\n\n```go\n// file: domain/entity.go\n//go:generate go run github.com/toniphan21/go-mapper-gen/cmd/generator\n\npackage domain\n\ntype User struct {\n    ID        string\n    FirstName string\n    LastName  string\n    Email     string\n    Password  *string\n}\n```\n\nand another struct perhaps generated from your database using tool such as [sqlc](https://sqlc.dev) located in `db`\n\n```go\n// file: db/models.go\n\npackage db\n\nimport \"github.com/jackc/pgx/v5/pgtype\"\n\ntype User struct {\n    ID        string\n    Email     string\n    FirstName string\n    LastName  string\n    Password  pgtype.Text\n}\n```\n\n---\n\n#### Default mode: types (interface + implementation)\n\nWith this minimal configuration, `go-mapper-gen` generates an unexported interface and implementation with methods to\nconvert both ways.\n\n```pkl\n// file: mapper.pkl\namends \"https://github.com/toniphan21/go-mapper-gen/releases/download/current/Config.pkl\"\n\nlocal function package(path: String) = \"github.com/toniphan21/go-mapper-gen/basic/\" + path\n\npackages {\n\t[package(\"db\")] {\n\t\tsource_pkg = package(\"domain\")\n\n\t\tstructs {\n\t\t\t[\"User\"] {}\n\t\t}\n\t}\n}\n```\n\nTo generate code:\n\n- Run `go generate ./...`, or\n- Invoke directly: `go run github.com/toniphan21/go-mapper-gen/cmd/generator` (from the module root)\n\nThis produces mapping code (by default in the target package) and keeps names unexported to avoid polluting your API surface.\n\n```go\n// golden-file: db/gen_mapper.go\n// Code generated by github.com/toniphan21/go-mapper-gen - test, DO NOT EDIT.\n\npackage db\n\nimport (\n\tpgtype \"github.com/jackc/pgx/v5/pgtype\"\n\tdomain \"github.com/toniphan21/go-mapper-gen/basic/domain\"\n)\n\ntype iMapper interface {\n\t// ToUser converts a domain.User value into a User value.\n\tToUser(in domain.User) User\n\n\t// FromUser converts a User value into a domain.User value.\n\tFromUser(in User) domain.User\n}\n\nfunc new_iMapper() iMapper {\n\treturn \u0026iMapperImpl{}\n}\n\ntype iMapperImpl struct{}\n\nfunc (m *iMapperImpl) ToUser(in domain.User) User {\n\tvar out User\n\n\tout.ID = in.ID\n\tout.Email = in.Email\n\tout.FirstName = in.FirstName\n\tout.LastName = in.LastName\n\tif in.Password != nil {\n\t\tout.Password = pgtype.Text{\n\t\t\tString: *in.Password,\n\t\t\tValid:  true,\n\t\t}\n\t}\n\n\treturn out\n}\n\nfunc (m *iMapperImpl) FromUser(in User) domain.User {\n\tvar out domain.User\n\n\tout.ID = in.ID\n\tout.FirstName = in.FirstName\n\tout.LastName = in.LastName\n\tout.Email = in.Email\n\tif in.Password.Valid {\n\t\tout.Password = \u0026in.Password.String\n\t}\n\n\treturn out\n}\n\nvar _ iMapper = (*iMapperImpl)(nil)\n```\n\nUse via composition to keep your API clean:\n\n```go\n// file: db/mapper.go\nvar mapper = new_iMapper()\n\nfunc demo() {\n    _ = mapper.ToUser(/* ... */)\n}\n\n// Or expose your own interface that embeds the generated one\ntype Mapper interface { iMapper }\n```\n\nYou can customize names (interface, implementation, constructor) in the Pkl config.\nSee [Config.pkl](https://github.com/toniphan21/go-mapper-gen/blob/main/pkl/Config.pkl), \n[mapper.pkl](https://github.com/toniphan21/go-mapper-gen/blob/main/pkl/mapper.pkl) for all options.\n\n---\n\n#### Functional mode: package-level functions\n\nSwitch to functions mode to emit only functions:\n\n```pkl\n// file: mapper.pkl\namends \"https://github.com/toniphan21/go-mapper-gen/releases/download/current/Config.pkl\"\n\nlocal function package(path: String) = \"github.com/toniphan21/go-mapper-gen/basic/\" + path\n\npackages {\n\t[package(\"db\")] {\n\t\tmode = \"functions\"\n\t\tsource_pkg = package(\"domain\")\n\n\t\tstructs {\n\t\t\t[\"User\"] {}\n\t\t}\n\t}\n}\n```\n\nThis yields functions like `ToUser` and `FromUser` in the target package:\n\n```go\n// golden-file: db/gen_mapper.go\n// Code generated by github.com/toniphan21/go-mapper-gen - test, DO NOT EDIT.\n\npackage db\n\nimport (\n\tpgtype \"github.com/jackc/pgx/v5/pgtype\"\n\tdomain \"github.com/toniphan21/go-mapper-gen/basic/domain\"\n)\n\n// ToUser converts a domain.User value into a User value.\nfunc ToUser(in domain.User) User {\n\tvar out User\n\n\tout.ID = in.ID\n\tout.Email = in.Email\n\tout.FirstName = in.FirstName\n\tout.LastName = in.LastName\n\tif in.Password != nil {\n\t\tout.Password = pgtype.Text{\n\t\t\tString: *in.Password,\n\t\t\tValid:  true,\n\t\t}\n\t}\n\n\treturn out\n}\n\n// FromUser converts a User value into a domain.User value.\nfunc FromUser(in User) domain.User {\n\tvar out domain.User\n\n\tout.ID = in.ID\n\tout.FirstName = in.FirstName\n\tout.LastName = in.LastName\n\tout.Email = in.Email\n\tif in.Password.Valid {\n\t\tout.Password = \u0026in.Password.String\n\t}\n\n\treturn out\n}\n```\n\n---\n\n### Next Steps\n\nTake a look at examples of how to:\n\n- Config \n  [multiple structs](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/config/02-multiple-structs), \n  [change functions' name](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/config/03-change-functions-name),\n  [multiple mappers in a package](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/config/01-multiple-mappers).\n- Convert custom type: \n  [with package level functions](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/functions-converter/01-use-package-level-functions),\n  [with variable methods](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/functions-converter/02-use-variable-methods).\n- [Manual mapping fields](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/field-mapping/02-manual-mapping-fields),\n  [use function/method to convert individual field](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/field-mapping/03-use-function-on-individual-field).\n- [Use go-mapper-gen as a library.](https://github.com/toniphan21/go-mapper-gen/tree/main/examples/use-as-library)\n\n---\n\n### Contributing \u0026 Licence\n\nPRs are welcome! See the [CONTRIBUTING](https://github.com/toniphan21/go-mapper-gen/blob/main/CONTRIBUTING.md).\nDistributed under the MIT License.\n\n❤️ Like the project? [Buy me a coffee](https://buymeacoffee.com/toniphan21) ☕. Thank you!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoniphan21%2Fgo-mapper-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoniphan21%2Fgo-mapper-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoniphan21%2Fgo-mapper-gen/lists"}