{"id":23388855,"url":"https://github.com/pricelessrabbit/flatr","last_synced_at":"2026-02-08T03:33:02.840Z","repository":{"id":130012450,"uuid":"610878023","full_name":"pricelessrabbit/flatr","owner":"pricelessrabbit","description":"Golang util to easily flatten a nested map into a 1-level deep one.","archived":false,"fork":false,"pushed_at":"2025-01-26T22:34:40.000Z","size":44,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-12T14:44:59.731Z","etag":null,"topics":["flat","go","golang","map"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pricelessrabbit.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":"2023-03-07T16:59:06.000Z","updated_at":"2025-01-26T22:34:43.000Z","dependencies_parsed_at":"2025-02-14T09:53:06.731Z","dependency_job_id":"bcbd907e-eaef-4ce0-b520-8677345c196a","html_url":"https://github.com/pricelessrabbit/flatr","commit_stats":null,"previous_names":["pricelessrabbit/flat"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/pricelessrabbit/flatr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pricelessrabbit%2Fflatr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pricelessrabbit%2Fflatr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pricelessrabbit%2Fflatr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pricelessrabbit%2Fflatr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pricelessrabbit","download_url":"https://codeload.github.com/pricelessrabbit/flatr/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pricelessrabbit%2Fflatr/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29219273,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T03:18:47.732Z","status":"ssl_error","status_checked_at":"2026-02-08T03:15:31.985Z","response_time":57,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["flat","go","golang","map"],"created_at":"2024-12-22T02:35:02.140Z","updated_at":"2026-02-08T03:33:02.821Z","avatar_url":"https://github.com/pricelessrabbit.png","language":"Go","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/22039194/224507070-5e534128-a350-421a-8cb8-3bef2ecce729.png\" /\u003e\u003cbr\u003e\n  \u003ci\u003eThe Flat Rabbit,  Barour Oskarsson\u003c/i\u003e\n\u003c/p\u003e\n\n# FLATR\n\nGolang util to easily flat a nested map into a 1-level deep one.\n\n## Features\n\n- Prefix selection\n- Separator selection\n- Transformers to preprocess each node for advanced use cases\n\n## Get it\n\n`go get github.com/pricelessrabbit/flatr`\n\n## Use it\n\n### Basic usage\n\n```go\ntoFlat := map[string]any{\n\t\t\"foo\": \"bar\",\n\t\t\"nest\": map[string]any{\n\t\t\t\"bar\": \"baz\",\n\t\t},\n\t\t\"list\": []any{\n\t\t\tmap[string]any{\n\t\t\t\t\"id\":   1,\n\t\t\t\t\"data\": \"data1\",\n\t\t\t},\n\t\t\tmap[string]any{\n\t\t\t\t\"id\":   2,\n\t\t\t\t\"data\": \"data2\",\n\t\t\t},\n\t\t},\n\t}\nf := flatr.New()\nflatten, err := f.Flat(toFlat)\n\n\n/* result\nmap[\n  foo: bar \n  list.0.data: data1 \n  list.0.id: 1 \n  list.1.data: data2 \n  list.1.id: 2 \n  nest.bar: baz\n]\n*/\n```\n\n### Options\n\nAdd options to the constructor to use them: `flatr.New(Option1(),Option2()...)`\n\n#### Prefix option\n\nAdds a prefix to all the flatted keys\n\n```go\nf := New(flatr.WithPrefix(\"namespace\"))\nflatten, _ := f.Flat(toFlat)\n\n\n/* result\nmap[\n  namespace.foo: bar \n  namespace.list.0.data: data1 \n  namespace.list.0.id: 1 \n  namespace.list.1.data: data2 \n  namespace.list.1.id: 2 \n  namespace.nest.bar: baz\n]\n*/\n```\n\n\n#### Separator option\n\nChoose path separator (default `.`)\n\n\n```go\nf := New(flatr.WithSeparator(\"_\"))\nflatten, _ := f.Flat(toFlat)\n\n/* result\nmap[\n  foo: bar\n  list_0_data: data1\n  list_0_id: 1\n  list_1_data: data2\n  list_1_id: 2\n  nest_bar: baz\n]\n*/\n```\n\n### Transformers\n\nTransformers:\n- Have access to the entry related to node being flatted\n- Can read and update node key, value, and set a stop flag  to block children processing\n\n#### Default transformers\n\n##### `flatr.Transformers.MaxDeep` \ndefine a max deep for the flattening process.\nafter reaching the max deep, the children of the current will be left untouched.\n\n```go\n toFlat := map[string]any{\n    \"foo\": []any{\n        map[string]any{\"id\": \"a\", \"data\": 10},\n        map[string]any{\"id\": \"b\", \"data\": 20},\n    },\n    \"bar\": []any{\n        map[string]any{\"id\": 1, \"data\": 30},\n        map[string]any{\"id\": 2, \"data\": 40},\n    },\n}\nf := New(\n    AddTransformer(MaxDeep(2)),\n)\nflatted, _ := f.Flat(toFlat)\n/*\nmap[\n\tfoo.0: {\"id\": \"a\", \"data\": 10}\n\tfoo.1: {\"id\": 1, \"data\": 30}\n\tbar.0: {\"id\": 1, \"data\": 30}\n    bar.1: {\"id\": 2, \"data\": 40}\n]\n*/\n\t\n```\n\n##### `flatr.Transformers.UseFieldAsIndex` \nif the current node is a slice, use the value of the given field (eg `id`) as index instead\nof the array index.\n\n```go\ntoFlat := map[string]any{\n    \"foo\": []any{\n    map[string]any{\"id\": \"a\", \"data\": 10},\n    map[string]any{\"id\": \"b\", \"data\": 20},\n    },\n    \"bar\": []any{\n    map[string]any{\"id\": 1, \"data\": 30},\n    map[string]any{\"id\": 2, \"data\": 40},\n    },\n}\n\nf := New(\nAddScopedTransformer(\"foo\", UseFieldAsIndex(\"id\")),\nAddScopedTransformer(\"bar\", UseFieldAsIndex(\"id\")),\n)\n\nflatten, _ := f.Flat(toFlat)\n\n/*\nmap[\nfoo.a.id: a\nfoo.a.data: 10\nfoo.b.id: b \nfoo.b.data: 20\n\nbar.1.id: 1\nbar.1: data: 30\nbar.2.id: 2\nbar.2.data: 40\n]\n*/\n\n```\n\n#### Custom transfomers \nIt is possible to implement custom transformers. A trasformer is valid if: \n- takes an Entry\n- returns the transformed Entry and an optional error\n\nThe entry field that can be transformed are:\n- K    the key of the node\n- V    the value related to the key \n- H    deep of the node\n- Stop stops processing of the children of the current node\n\n##### Example\nthe transformer in the example adds the suffix \"_transformed\" to all the string values\nthat are found in the structure. other value types are left untouched.\n\n```go\ncustomTransformer := func (e Entry) (Entry, error) {\n    //check if the current entry value is a string\n    // and in that case add the suffix \"transformed\"\n    if s, ok := e.V.(string); ok {\n    e.V = s + \"_transformed\"\n    }\n    return e, nil\n}\n\nf := New(AddTransformer(customTransformer))\nflatten, _ := f.Flat(toFlat)\n\n/*\nmap[\n\tfoo: bar_transformed\n\tlist_0_data: data1_transformed\n\tlist_0_id: 1\n\tlist_1_data: data2_transformed\n\tlist_1_id: 2\n\tnest_bar: baz_transformed\n]\n*/\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpricelessrabbit%2Fflatr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpricelessrabbit%2Fflatr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpricelessrabbit%2Fflatr/lists"}