{"id":37224463,"url":"https://github.com/soulka/golymorph","last_synced_at":"2026-01-15T01:41:47.611Z","repository":{"id":209702678,"uuid":"723321816","full_name":"SoulKa/golymorph","owner":"SoulKa","description":"The golymorph module enables resolving polymorphic typing at runtime. It's usually used during JSON parsing.","archived":false,"fork":false,"pushed_at":"2024-01-06T21:08:30.000Z","size":60,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-01-06T22:20:13.917Z","etag":null,"topics":["go","golang","json","mapping","polymorphism","struct"],"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/SoulKa.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}},"created_at":"2023-11-25T09:48:40.000Z","updated_at":"2024-01-06T22:20:21.421Z","dependencies_parsed_at":"2023-11-28T19:25:27.034Z","dependency_job_id":"ee956bb3-cdb0-4c0b-b478-4333ebe20a87","html_url":"https://github.com/SoulKa/golymorph","commit_stats":null,"previous_names":["soulka/golymorph"],"tags_count":2,"template":null,"template_full_name":null,"purl":"pkg:github/SoulKa/golymorph","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoulKa%2Fgolymorph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoulKa%2Fgolymorph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoulKa%2Fgolymorph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoulKa%2Fgolymorph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoulKa","download_url":"https://codeload.github.com/SoulKa/golymorph/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoulKa%2Fgolymorph/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28441031,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:55:22.719Z","status":"ssl_error","status_checked_at":"2026-01-15T00:55:20.945Z","response_time":107,"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":["go","golang","json","mapping","polymorphism","struct"],"created_at":"2026-01-15T01:41:46.966Z","updated_at":"2026-01-15T01:41:47.599Z","avatar_url":"https://github.com/SoulKa.png","language":"Go","readme":"# golymorph [![Pipeline Tests Status](https://github.com/SoulKa/golymorph/actions/workflows/go-test.yaml/badge.svg)](https://github.com/SoulKa/golymorph/actions/workflows/go-test.yaml) [![Godoc](https://godoc.org/github.com/SoulKa/golymorph?status.svg)](https://godoc.org/github.com/SoulKa/golymorph)\n\nThe golymorph module enables resolving polymorphic typing at runtime. It's usually used in\nconjunction with JSON parsing and the `mapstructure` module. In fact, this module takes the use case\nof `mapstructure` a step further by allowing the user to define a custom type resolver.\n\n## Installation\n\nStandard `go get`:\n\n```shell\ngo get github.com/SoulKa/golymorph\n```\n\n## Docs\n\nThe docs are hosted on [Godoc](http://godoc.org/github.com/SoulKa/golymorph).\n\n## Use Case\n\nUse this module to resolve polymorphic types at runtime. An example would be a struct that contains\na payload field which can be of different struct types not known at compile time:\n\n```go\n// the parent type that contains the polymorphic payload\ntype Event struct {\n\tTimestamp string\n\tPayload   any // \u003c-- AlertPayload or PingPayload?\n}\n\n// the polymorphic child types\ntype AlertPayload struct {\n\tType    string\n\tMessage string\n}\ntype PingPayload struct {\n\tType string\n\tIp   string\n}\n```\n\nIf, for example, you have a JSON that you decode into the `Event` struct, it is cumbersome to parse the JSON into a map, look into the `type` field of the `payload` and after that select and parse the map into the correct type at the `payload` field of the `event` struct.\ngolymorph does exactly this:\n\n1. Look at the value of a defined field anywhere in the given `map` or JSON\n2. Find the correct type using the given pairs of `value ==\u003e reflect.Type`\n3. Assign the correct type at the given position anywhere in the \"parent\" struct\n4. Fully decode the given JSON or `map`, now with a concrete struct type as `payload`\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/SoulKa/golymorph\"\n\t\"reflect\"\n)\n\nfunc main() {\n\t// get a JSON that contains a payload with a type field that determines the type of the payload\n\talertEventJson := `{ \"timestamp\": \"2023-11-27T22:14:09+00:00\", \"payload\": { \"type\": \"alert\", \"message\": \"something is broken!\" } }`\n\n\t// the parent type that contains the polymorphic payload\n\ttype Event struct {\n\t\tTimestamp string\n\t\tPayload   any\n\t}\n\n\t// the polymorphic child types\n\ttype AlertPayload struct {\n\t\tType    string\n\t\tMessage string\n\t}\n\ttype PingPayload struct {\n\t\tType string\n\t\tIp   string\n\t}\n\n\t// define a mapping from the type value to the type of the payload\n\ttypeMap := golymorph.TypeMap{\n\t\t\"alert\": reflect.TypeOf(AlertPayload{}),\n\t\t\"ping\":  reflect.TypeOf(PingPayload{}),\n\t}\n\n\t// create a TypeResolver that assigns the type of the payload based on the type field\n\terr, resolver := golymorph.NewPolymorphismBuilder().\n\t\tDefineTypeAt(\"payload\").\n\t\tUsingTypeMap(typeMap).\n\t\tWithDiscriminatorAt(\"type\").\n\t\tBuild()\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"error building polymorpher: %s\", err))\n\t}\n\n\t// create a new event\n\tvar event Event\n\tif err := golymorph.UnmarshalJSON(resolver, []byte(alertEventJson), \u0026event); err != nil {\n\t\tpanic(fmt.Sprintf(\"error unmarshalling event: %s\", err))\n\t}\n\n\t// continue to work with the event\n\tfmt.Printf(\"event: %+v\\n\", event)\n\tfmt.Printf(\"event payload: %T %+v\\n\", event.Payload, event.Payload.(AlertPayload))\n}\n```\n\n## Contributing\n\nI am very happy for contributions or feature suggestions. As long as this module is not stable released (version 1.0.0) I am also open for refactorings.\n\n**How to:**\n - Fork it\n - Create your feature branch (git checkout -b my-new-feature)\n - Commit your changes (git commit -am 'Added some feature')\n - Push to the branch (git push origin my-new-feature)\n - Create new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoulka%2Fgolymorph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoulka%2Fgolymorph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoulka%2Fgolymorph/lists"}