{"id":19368490,"url":"https://github.com/anchore/go-struct-converter","last_synced_at":"2025-12-24T21:36:29.064Z","repository":{"id":64299514,"uuid":"566886769","full_name":"anchore/go-struct-converter","owner":"anchore","description":"Go library that provides a set of conversion utilities to help migrate between different versioned Go structs.","archived":false,"fork":false,"pushed_at":"2025-03-12T06:51:59.000Z","size":40,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-05-09T17:06:40.470Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anchore.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}},"created_at":"2022-11-16T16:15:00.000Z","updated_at":"2025-02-11T21:32:30.000Z","dependencies_parsed_at":"2024-06-19T00:26:14.621Z","dependency_job_id":null,"html_url":"https://github.com/anchore/go-struct-converter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchore%2Fgo-struct-converter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchore%2Fgo-struct-converter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchore%2Fgo-struct-converter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anchore%2Fgo-struct-converter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anchore","download_url":"https://codeload.github.com/anchore/go-struct-converter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253292125,"owners_count":21885037,"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":[],"created_at":"2024-11-10T08:06:42.468Z","updated_at":"2025-12-24T21:36:29.057Z","avatar_url":"https://github.com/anchore.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go `struct` Converter\n\nA library for converting between Go structs.\n\n```go\nchain := converter.NewFuncChain(V1toV2, V2toV3)\n\nchain.Convert(myV1struct, \u0026myV3struct)\n```\n\n## Details\n\nAt its core, this library provides a `Convert` function, which automatically\nhandles converting fields with the same name, and \"convertable\"\ntypes. Some examples are:\n* `string` -\u003e `string`\n* `string` -\u003e `*string`\n* `int` -\u003e `string`\n* `string` -\u003e `[]string`\n\nThe automatic conversions are implemented when there is an obvious way\nto convert between the types. A lot more automatic conversions happen\n-- see [the converter tests](converter_test.go) for a more comprehensive\nlist of what is currently supported.\n\nNot everything can be handled automatically, however, so there is also\na way to specify custom conversions. By implementing a function that\nadheres to some basic forms. Any custom conversions found during object graph\ntraversal will attempt automatic  conversion, and pass the resulting structs\nto the custom conversion function.\n\nAdditionally, and maybe most importantly, there is a `converter.FuncChain` available,\nwhich orchestrates conversions across _multiple versions_ of structs. This could\nbe thought of similar to database migrations: given a starting struct and a target\nstruct, the `FuncChain.Convert` function iterates through every intermediary migration\nin order to arrive at the target struct.\n\n## Basic Usage\n\nTo illustrate usage we'll start with a few basic structs:\n\n```go\ntype V1 struct {\n  Name     string\n  OldField string\n}\n\ntype V2 struct {\n  Name     string\n  NewField string // this was a renamed field\n}\n\ntype V3 struct {\n  Name       []string\n  FinalField []string // this field was renamed and the type was changed\n}\n```\n\nGiven these type definitions, we can easily set up a conversion chain\nlike this:\n\n```go\nfunc V1toV2(from V1, to *V2) error { // forward migration\n    to.NewField = from.OldField\n    return nil\n}\n\nfunc V2toV3(from V2, to *V3) error { // forward migration\n    to.FinalField = []string{from.NewField}\n    return nil\n}\n\n...\n\nchain := converter.NewFuncChain(V1toV2, V2toV3)\n```\n\nThis chain can then be used to convert from an _older version_ to a _newer \nversion_. This is because we only supplied _forward_ migrations.\n\nThis chain can be used to convert from a `V1` struct to a `V3` struct easily,\nlike this:\n\n```go\nv1 := // somehow get a populated v1 struct\nv3 := V3{}\nchain.Convert(v1, \u0026v3)\n```\n\nSince we've defined our chain as `V1` \u0026rarr; `V2` \u0026rarr; `V3`, the chain will execute\nconversions to all intermediary structs (`V2`, in this case) and ultimately end\nwhen we've populated the `v3` instance.\n\nNote we haven't needed to define any conversions on the `Name` field of any structs\nsince this one is convertible between structs: `string` \u0026rarr; `string` \u0026rarr; `[]string`.\n\n## Backwards Migrations\n\nIf we wanted to _also_ provide backwards migrations, we could also easily add functions for\nthis.\n\n```go\nfunc V2toV1(from V2, to *V1) error { // backward migration\n    to.OldField = from.NewField\n    return nil\n}\n\nfunc V3toV2(from V3, to *V2) error { // backward migration\n    to.NewField = from.FinalField[0]\n    return nil\n}\n\nchain := converter.NewFuncChain(V1toV2, V2toV1, V2toV3, V3toV2)\n```\n\nAt this point we could convert in either direction, for example a \n`V3` struct could convert to a `V1` struct, with the caveat that there\nmay be data loss, due to whatever changes were made to the data shapes.\n\n## Contributing\n\nIf you would like to contribute to this repository, please see the\n[CONTRIBUTING.md](CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanchore%2Fgo-struct-converter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanchore%2Fgo-struct-converter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanchore%2Fgo-struct-converter/lists"}