{"id":13414144,"url":"https://github.com/Eun/go-convert","last_synced_at":"2025-03-14T20:31:15.308Z","repository":{"id":38319591,"uuid":"190779574","full_name":"Eun/go-convert","owner":"Eun","description":"Convert a value into another type","archived":false,"fork":false,"pushed_at":"2024-05-08T04:22:29.000Z","size":508,"stargazers_count":23,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-31T20:53:34.668Z","etag":null,"topics":["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/Eun.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":"2019-06-07T16:56:38.000Z","updated_at":"2024-05-17T14:14:05.000Z","dependencies_parsed_at":"2024-06-20T15:45:23.974Z","dependency_job_id":null,"html_url":"https://github.com/Eun/go-convert","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/Eun%2Fgo-convert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eun%2Fgo-convert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eun%2Fgo-convert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eun%2Fgo-convert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eun","download_url":"https://codeload.github.com/Eun/go-convert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243642116,"owners_count":20323959,"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":["go","golang"],"created_at":"2024-07-30T20:01:59.056Z","updated_at":"2025-03-14T20:31:14.952Z","avatar_url":"https://github.com/Eun.png","language":"Go","readme":"# go-convert [![Actions Status](https://github.com/Eun/go-convert/workflows/CI/badge.svg)](https://github.com/Eun/go-convert/actions) [![Coverage Status](https://coveralls.io/repos/github/Eun/go-convert/badge.svg?branch=master)](https://coveralls.io/github/Eun/go-convert?branch=master) [![PkgGoDev](https://img.shields.io/badge/pkg.go.dev-reference-blue)](https://pkg.go.dev/github.com/Eun/go-convert) [![GoDoc](https://godoc.org/github.com/Eun/go-convert?status.svg)](https://godoc.org/github.com/Eun/go-convert) [![go-report](https://goreportcard.com/badge/github.com/Eun/go-convert)](https://goreportcard.com/report/github.com/Eun/go-convert)\nConvert a value into another type.\n\n```bash\ngo get -u github.com/Eun/go-convert\n```\n## Usage\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/Eun/go-convert\"\n)\n\nfunc main() {\n\t// convert a int to a string\n\tvar s string\n\tconvert.MustConvert(1, \u0026s)\n\tfmt.Printf(\"%s\\n\", s)\n\n\t// convert a map into a struct\n\ttype User struct {\n\t\tID   int\n\t\tName string\n\t}\n\tvar u User\n\tconvert.MustConvert(map[string]string{\n\t\t\"Name\": \"Joe\",\n\t\t\"ID\":   \"10\",\n\t}, \u0026u)\n\tfmt.Printf(\"%#v\\n\", u)\n\n\t// convert Id to int and Groups to []int and keep the rest\n\tm := map[string]interface{}{\n\t\t\"Id\":     0,\n\t\t\"Groups\": []int{},\n\t}\n\t// convert a map into well defined map\n\tconvert.MustConvert(\n\t\tmap[string]interface{}{\n\t\t\t\"Id\":      \"1\",\n\t\t\t\"Name\":    \"Joe\",\n\t\t\t\"Groups\":  []string{\"3\", \"6\"},\n\t\t\t\"Country\": \"US\",\n\t\t},\n\t\t\u0026m,\n\t)\n\tfmt.Printf(\"%v\\n\", m)\n\n\t// convert a interface slice into well defined interface slice\n\t// making the first one an integer, the second a string and the third an float\n\tsl := []interface{}{0, \"\", 0.0}\n\tconvert.MustConvert([]string{\"1\", \"2\", \"3\"}, \u0026sl)\n\tfmt.Printf(\"%v\\n\", sl)\n}\n\n```\n\n## Recipe system\n_go-convert_ uses a recipe system that defines how and which types should be converted in which type.\nA lot of recipes are already builtin (see [recipes.go](recipes.go)), however you can add your own or overwrite the builtin ones.\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/Eun/go-convert\"\n)\n\ntype Roles struct {\n\tIsAdmin     bool\n\tIsDeveloper bool\n}\n\ntype User struct {\n\tID    int\n\tName  string\n\tRoles Roles\n}\n\nfunc main() {\n\t// this is the data we want to convert\n\tdata := map[string]string{\n\t\t\"id\":    \"10\",\n\t\t\"Name\":  \"Joe\",\n\t\t\"roles\": \"AD\", // this user is Admin (A) and Developer (D)\n\t}\n\n\t// create a converter\n\tconv := convert.New(convert.Options{\n\t\tRecipes: convert.MustMakeRecipes(\n\t\t\t// convert string into Roles\n\t\t\tfunc(_ convert.Converter, in string, out *Roles) error {\n\t\t\t\t(*out).IsAdmin = false\n\t\t\t\t(*out).IsDeveloper = false\n\t\t\t\tif strings.Contains(in, \"A\") {\n\t\t\t\t\t(*out).IsAdmin = true\n\t\t\t\t}\n\t\t\t\tif strings.Contains(in, \"D\") {\n\t\t\t\t\t(*out).IsDeveloper = true\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t},\n\t\t),\n\t})\n\n\tvar user User\n\tconv.MustConvert(data, \u0026user)\n\t// user is now an instance of User\n\tfmt.Printf(\"%#v\\n\", user)\n}\n\n```\n\n## Adding inline recipes\nYou can also add recipes inline by implementing a `ConvertRecipes() []Recipe` function.  \nExample:\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/Eun/go-convert\"\n)\n\ntype Roles struct {\n\tIsAdmin     bool\n\tIsDeveloper bool\n}\n\ntype User struct {\n\tID    int\n\tName  string\n\tRoles Roles\n}\n\nfunc (user *User) ConvertRecipes() []convert.Recipe {\n\treturn convert.MustMakeRecipes(\n\t\t// convert string into Roles\n\t\tfunc(_ convert.Converter, in string, out *Roles) error {\n\t\t\tout.IsAdmin = false\n\t\t\tout.IsDeveloper = false\n\t\t\tif strings.Contains(in, \"A\") {\n\t\t\t\tout.IsAdmin = true\n\t\t\t}\n\t\t\tif strings.Contains(in, \"D\") {\n\t\t\t\tout.IsDeveloper = true\n\t\t\t}\n\t\t\treturn nil\n\t\t},\n\t)\n}\n\nfunc main() {\n\t// this is the data we want to convert\n\tdata := []map[string]string{\n\t\t{\n\t\t\t\"id\":    \"10\",\n\t\t\t\"Name\":  \"Joe\",\n\t\t\t\"roles\": \"AD\", // this user is Admin (A) and Developer (D)\n\t\t},\n\t\t{\n\t\t\t\"id\":    \"21\",\n\t\t\t\"Name\":  \"Alice\",\n\t\t\t\"roles\": \"D\", // this user is Developer (D)\n\t\t},\n\t}\n\n\tvar users []User\n\tconvert.MustConvert(data, \u0026users)\n\t// users is now an instance of []User\n\tfmt.Printf(\"%#v\\n\", users)\n}\n\n```\n\n\n### Notice\nThis library is using reflection so be aware it might be slow in your usecase.  \n","funding_links":[],"categories":["Utilities","工具库","公用事业公司","工具库`可以提升效率的通用代码库和工具`","Utility"],"sub_categories":["Utility/Miscellaneous","查询语","实用程序/Miscellaneous","Fail injection","HTTP Clients"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEun%2Fgo-convert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEun%2Fgo-convert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEun%2Fgo-convert/lists"}