Skip to content

Commit

Permalink
ctxkey
Browse files Browse the repository at this point in the history
  • Loading branch information
a-menshchikov committed Jul 23, 2024
1 parent 3fe816b commit 9afef6c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
49 changes: 49 additions & 0 deletions ctxkey/ctxkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Package ctxkey is like errors.New() for context keys.
//
// Origin: https://github.com/cstockton/pkg/tree/master/ctxkey
//
// # Invariants
//
// All context keys created with New() will be globally unique during program
// execution for the lifespan of the Key and will not cause additional
// allocations when they are associated with a context value. Uniqueness is
// satisfied by returning pointers to a private key struct, forcing comparison
// operations in context value retrieval to use pointer equality within the
// current Go's specification:
//
// https://golang.org/ref/spec#Comparison_operators
//
// Additional allocations means that you will not allocate for the context key
// as seen with this common idiom:
//
// type key int
// var myKey key = 0
// // 3 allocations, context struct, myKey and someValue
// context.WithValue(ctx, myKey, SomeValue)
//
// Keys issued by New() fit within an interface{} value, i.e.:
//
// var myKey = ctxkey.New(`MyKey`)
// // 2 allocations, context struct and someValue
// context.WithValue(ctx, myKey, SomeValue)
package ctxkey

import (
"fmt"
)

type Key interface {
fmt.Stringer
}

type key struct {
name string
}

func New(name string) Key {
return &key{name}
}

func (k key) String() string {
return `ctxkey "` + k.name + `"`
}
22 changes: 22 additions & 0 deletions ctxkey/ctxkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ctxkey_test

import (
"context"
"testing"

. "github.com/nbgrp/pkg/ctxkey"
"github.com/stretchr/testify/assert"
)

func TestCtxkey(t *testing.T) {
t.Parallel()

key := New("testkey")
val := "testvalue"
ctx := context.WithValue(context.Background(), key, val)

assert.Equal(t, ctx.Value(key), val)
assert.Nil(t, ctx.Value("testkey"))

assert.Equal(t, `ctxkey "testkey"`, key.String())
}
5 changes: 5 additions & 0 deletions ctxkey/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/nbgrp/pkg/ctxkey

go 1.14

require github.com/stretchr/testify v1.9.0
19 changes: 19 additions & 0 deletions ctxkey/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 9afef6c

Please sign in to comment.