diff --git a/.github/workflows/build-test.yaml b/.github/workflows/build-test.yaml index 1e0bc8e..c207c59 100644 --- a/.github/workflows/build-test.yaml +++ b/.github/workflows/build-test.yaml @@ -8,7 +8,7 @@ on: # yamllint disable-line rule:truthy branches: - "*" env: - GO_VERSION: "~1.23.4" + GO_VERSION: "~1.24.13" jobs: unit: name: "Unit" diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 9dc9c4b..40d46ea 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -8,7 +8,7 @@ on: # yamllint disable-line rule:truthy pull_request: branches: ["*"] env: - GO_VERSION: "~1.23.4" + GO_VERSION: "~1.24.13" jobs: lint: name: "Lint" diff --git a/context.go b/context.go index 0a4d743..94d23f4 100644 --- a/context.go +++ b/context.go @@ -21,7 +21,9 @@ type ContextSet[V any] interface { // Key is a type that is used as a key in a context.Context for a // specific type of value V. -type Key[V any] struct{} +type Key[V any] struct { + _ int // ensures each instance has a unique address +} // New creates a new Key func New[V any]() *Key[V] { diff --git a/context_test.go b/context_test.go index 8e84b7e..a4cbc1a 100644 --- a/context_test.go +++ b/context_test.go @@ -233,3 +233,30 @@ func expectPanic(t testing.TB, f func()) { }() wg.Wait() } + +func TestKeyUniqueness(t *testing.T) { + // Create two separate keys with the same type + key1 := New[string]() + key2 := New[string]() + + // These should be different keys + if key1 == key2 { + t.Fatal("two separate keys have the same memory address!") + } + + // They should store independent values + ctx := context.Background() + ctx = key1.Set(ctx, "value1") + ctx = key2.Set(ctx, "value2") + + // Each key should retrieve its own value + val1, ok1 := key1.Value(ctx) + if !ok1 || val1 != "value1" { + t.Fatalf("key1 should return 'value1', got %q (ok=%v)", val1, ok1) + } + + val2, ok2 := key2.Value(ctx) + if !ok2 || val2 != "value2" { + t.Fatalf("key2 should return 'value2', got %q (ok=%v)", val2, ok2) + } +} diff --git a/go.mod b/go.mod index 0b5c3e3..d7213fb 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/authzed/ctxkey -go 1.23.4 +go 1.24 diff --git a/magefiles/go.work b/magefiles/go.work index 195cc6d..28d14fb 100644 --- a/magefiles/go.work +++ b/magefiles/go.work @@ -1,4 +1,4 @@ -go 1.23.4 +go 1.24 use ( .