{"id":30116411,"url":"https://github.com/authzed/ctxkey","last_synced_at":"2026-03-05T16:09:40.789Z","repository":{"id":274502777,"uuid":"921909533","full_name":"authzed/ctxkey","owner":"authzed","description":"a zero-dependency library for generically typed accessors for go's context package.","archived":false,"fork":false,"pushed_at":"2025-02-26T15:55:16.000Z","size":52,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-22T01:56:34.620Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/authzed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE-OF-CONDUCT.md","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":"2025-01-24T21:11:32.000Z","updated_at":"2025-03-30T11:59:19.000Z","dependencies_parsed_at":"2025-01-27T18:47:10.212Z","dependency_job_id":null,"html_url":"https://github.com/authzed/ctxkey","commit_stats":null,"previous_names":["authzed/ctxkey"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/authzed/ctxkey","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authzed%2Fctxkey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authzed%2Fctxkey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authzed%2Fctxkey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authzed%2Fctxkey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/authzed","download_url":"https://codeload.github.com/authzed/ctxkey/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authzed%2Fctxkey/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269705685,"owners_count":24462173,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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"],"created_at":"2025-08-10T09:37:15.821Z","updated_at":"2026-03-05T16:09:40.698Z","avatar_url":"https://github.com/authzed.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `ctxkey` - Typed Context Keys for Go\n\n`ctxkey` is a zero-dependency library adding generically typed accessors for `context.Context`.\n\n## Installation\n\nAdd `ctxkey` to your project with:\n\n```bash\ngo get github.com/authzed/ctxkey\n```\n\n## Example Usage\n\nSee [examples](./examples) for full, runnable examples.\n\n### Basic Usage\n\nDefine a unique key for a context value, without type conversion:\n\n```go\nctxMyKey := ctxkey.New[string]()\n\nctx := ctxMyKey.Set(context.Background(), \"value\")\n\nfmt.Println(ctxMyKey.Value(ctx)) // \"value\", true \n```\n\nCheck if a key has a value, or panic if not found:\n\n```go\nctxMyKey := ctxkey.New[string]()\nctx := ctxMyKey.Set(context.Background(), \"value\")\n\nvalue, ok := ctxMyKey.Value(ctx)\nif !ok {\n    panic(\"value not found\")\n}\n\nvalue = ctxMyKey.MustValue(ctx) // \"value\"\n```\n\nProvide a default value if a key is not found:\n\n```go\nctxMyKey := ctxkey.NewWithDefault(\"defaultValue\")\n\nctx := context.Background()\nvalue := ctxMyKey.Value(ctx) // \"defaultValue\"\n\nctx = ctxMyKey.Set(ctx, \"newValue\")\nvalue = ctxMyKey.MustNonEmptyValue(ctx) // \"newValue\"\n```\n\nProvide a box for a key to store a value, useful in cases where it is not convenient to return a context:\n\n```go\nvar ctxMyKey = ctxkey.NewBoxedWithDefault[string](nil)\n\nfunc inner(ctx context.Context) {   \n    ctxMyKey.Set(ctx, \"value\") \n    // note that the context is not returned\n}\n\nfunc outer() {\n    ctx := context.Background()\n    ctxMyKey.SetBox(ctx)\n\t\n    inner(ctx)\n\n    value := ctxMyKey.MustValue(ctx) // \"value\"\n}\n```\n\n### Composition\n\nThe `With` helpers can be used to compose multiple keys into a single context.\n\n```go\nctxMyKey := ctxkey.New[string]()\nctxMyKey2 := ctxkey.New[int]()\nctxMyKey3 := ctxkey.New[string]()\nctxMyKey4 := ctxkey.NewWithDefault(\"default\")\n\nfirstValueSet := ctxMyKey.WithValue(\"first\")\ntwoAndThreeSet := ctxkey.With(\n    ctxMyKey2.WithValue(42),\n    ctxMyKey3.WithValue(\"third\"),\n)\n\nctx := ctxkey.With(\n    firstValueSet,\n    twoAndThreeSet,\n)(context.Background())\n\nvalue, ok := ctxMyKey.Value(ctx) // \"first\", true\nvalue, ok = ctxMyKey2.Value(ctx) // 42, true\nvalue, ok = ctxMyKey3.Value(ctx) // \"third\", true\nvalue, ok = ctxMyKey4.Value(ctx) // \"default\", true\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fauthzed%2Fctxkey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fauthzed%2Fctxkey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fauthzed%2Fctxkey/lists"}