{"id":20695973,"url":"https://github.com/weiwenchen2022/typed","last_synced_at":"2026-04-24T09:36:58.835Z","repository":{"id":192215915,"uuid":"686296215","full_name":"weiwenchen2022/typed","owner":"weiwenchen2022","description":"A wrapper around ma[string]any and []any for strong type retrieve","archived":false,"fork":false,"pushed_at":"2023-09-02T15:58:30.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-11T02:46:21.014Z","etag":null,"topics":["generic","go","golang","json","library"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/weiwenchen2022/typed","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weiwenchen2022.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}},"created_at":"2023-09-02T10:20:34.000Z","updated_at":"2023-09-02T10:30:21.000Z","dependencies_parsed_at":"2023-09-03T12:51:06.993Z","dependency_job_id":null,"html_url":"https://github.com/weiwenchen2022/typed","commit_stats":null,"previous_names":["weiwenchen2022/typed"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/weiwenchen2022/typed","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weiwenchen2022%2Ftyped","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weiwenchen2022%2Ftyped/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weiwenchen2022%2Ftyped/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weiwenchen2022%2Ftyped/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weiwenchen2022","download_url":"https://codeload.github.com/weiwenchen2022/typed/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weiwenchen2022%2Ftyped/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32217067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-24T08:24:32.376Z","status":"ssl_error","status_checked_at":"2026-04-24T08:24:26.731Z","response_time":64,"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":["generic","go","golang","json","library"],"created_at":"2024-11-17T00:12:07.989Z","updated_at":"2026-04-24T09:36:53.825Z","avatar_url":"https://github.com/weiwenchen2022.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Typed\n\n*Making `map[string]any and []any` slightly less painful*\n\nIt isn't always desirable or even possible to decode JSON into a structure.\nThat, unfortunately, leaves us with writing ugly code around `map[string]any` and `[]any`.\n\nThis library hopes to make that slightly less painful.\n\n## Install\n\n```sh\ngo get github.com/weiwenchen2022/typed\n```\n\n## Note\n\nAlthough the library *is* tailored to be used with `encoding/json`, it also can be used with any `map[string]any` and `[]any`.\n\nSpecifically, it wrapped `map[string]any` and `[]any` to `M` and `A` recurse down. If the given key is multiple keys conjunction with dot \".\" character, corresponding methods also recurse down.\n\n## Usage:\n\nA typed wrapper around a `map[string]any` and `[]any` can be created in one of two ways:\n\n```go\n// Wrapped from a map[string]any or []any\nm := typed.Wrap(originalM).(M)\n\na := typed.Wrap(originalA).(A)\n\n// Decoded from a json data\nvar m typed.M\nerr := json.Unmarshal(data, \u0026m)\n\nvar a typed.A\nerr := json.Unmarshal(data, \u0026a)\n```\n\nOnce we have a wrapper `M`, we can use various methods to navigate the structure:\n\n- `(M) Bool(key string) bool`\n- `(M) BoolOK(key string) (bool, bool)`\n\n- `(M) AsInt(key string) int`\n- `(M) AsIntOK(key string) (int, bool)`\n\n- `(M) AsInt64(key string) int64`\n- `(M) AsInt64OK(key string) (int64, bool)`\n\n- `(M) Float(key string) float64`\n- `(M) FloatOK(key string) (float64, bool)`\n\n- `(M) StringValue(key string) string`\n- `(M) StringValueOK(key string) (string, bool)`\n\n- `(M) Any(key string) any`\n- `(M) AnyOK(key string) (any, bool)`\n\nWe can also extract arrays via wrapper `A`:\n\n- `(M) Array(key string) A`\n\n`A` has various methods to convert to concrete array:\n\n- `(A) Bools(key string) []bool`\n- `(A) BoolsOK(key string) ([]bool, bool)`\n\n- `(A) AsInts(key string) []int`\n- `(A) AsIntsOK(key string) ([]int, bool)`\n\n- `(A) AsInt64s(key string) []int64`\n- `(A) AsInt64sOK(key string) ([]int64, bool)`\n\n- `(A) Floats(key string) []float64`\n- `(A) FloatsOK(key string) ([]float64, bool)`\n\n- `(A) Strings(key string) []string`\n- `(A) StringsOK(key string) ([]string, bool)`\n\nWe can extract nested document, as a nested wrapper, or as a original `map[string]any`:\n\n- `(M) Document(key string) M`\n- `(M) DocumentOK(key string) (M, bool)`\n\n- `(M) Map(key string) map[string]any`\n- `(M) MapOK(key string) (map[string]any, bool)`\n\n- `(A) Documents(key string) []M`\n- `(A) DocumentsOK(key string) ([]M, bool)`\n\n- `(A) Maps(key string) []map[string]any`\n- `(A) MapsOK(key string) ([]map[string]any, bool)`\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/weiwenchen2022/typed\"\n)\n\nfunc main() {\n\tvar j = []byte(`{\n\t\t\"Name\": \"Wednesday\",\n\t\t\"Age\": 6,\n\t\t\"Parents\": [\"Gomez\",\"Morticia\"]\n\t}`)\n\n\tvar m typed.M\n\terr := json.Unmarshal(j, \u0026m)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(m.StringValue(\"Name\"))\n\tfmt.Println(m.AsInt(\"Age\"))\n\tfmt.Println(m.Array(\"Parents\").Strings())\n}\n```\n\n# Misc\n\n## AsTime and AsDuration\n`(M) AsTime(key string) time.Time` can be used to get the string value, as a time.Time.\n\nAlternatively, `(M) AsDuration(key string) time.Duration` can be used to get the string value, as a time.Duration.\n\n## Root Array\n\nJSON array at root is supported. Use the `A` to decoded, or wrapped from []any.\n\n```go\nvar js = []byte(`[6, {\"Name\": \"Wednesday\"}, \"Gomez\"]`)\nvar a typed.A\nerr := json.Unmarshal(js, \u0026a)\nif err != nil {\n\tpanic(err)\n}\n\nfmt.Println(a[0].(float64))\nfmt.Println(a[1].(typed.M).StringValue(\"Name\"))\nfmt.Println(a[2].(string))\n```\n\n## Doc\nGoDoc: [https://godoc.org/github.com/weiwenchen2022/typed](https://godoc.org/github.com/weiwenchen2022/typed)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweiwenchen2022%2Ftyped","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweiwenchen2022%2Ftyped","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweiwenchen2022%2Ftyped/lists"}