Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
27 changes: 27 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/authzed/ctxkey

go 1.23.4
go 1.24
2 changes: 1 addition & 1 deletion magefiles/go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.23.4
go 1.24

use (
.
Expand Down