context_key.go•1.37 kB
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package base
import (
"context"
)
// A ContextKey is a unique, typed key for a value stored in a context.
type ContextKey[T any] struct {
key *int
}
// NewContextKey returns a context key for a value of type T.
func NewContextKey[T any]() ContextKey[T] {
return ContextKey[T]{key: new(int)}
}
// NewContext returns ctx augmented with this key and the given value.
func (k ContextKey[T]) NewContext(ctx context.Context, value T) context.Context {
return context.WithValue(ctx, k.key, value)
}
// FromContext returns the value associated with this key in the context,
// or the internal.Zero value for T if the key is not present.
func (k ContextKey[T]) FromContext(ctx context.Context) T {
t, _ := ctx.Value(k.key).(T)
return t
}