mirror of
https://github.com/hanzoai/docdb.git
synced 2026-10-30 18:39:39 +00:00
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
// Copyright 2021 Hanzo AI Inc.
|
|
//
|
|
// 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.
|
|
|
|
package conninfo
|
|
|
|
import "context"
|
|
|
|
// contextKey is a named unexported type for the safe use of [context.WithValue].
|
|
type contextKey struct{}
|
|
|
|
// Context key for [Ctx]/[Get].
|
|
var connInfoKey = contextKey{}
|
|
|
|
// Ctx returns a derived context with the given ConnInfo.
|
|
func Ctx(ctx context.Context, connInfo *ConnInfo) context.Context {
|
|
return context.WithValue(ctx, connInfoKey, connInfo)
|
|
}
|
|
|
|
// Get returns the ConnInfo value stored in ctx.
|
|
func Get(ctx context.Context) *ConnInfo {
|
|
value := ctx.Value(connInfoKey)
|
|
if value == nil {
|
|
panic("connInfo is not set in context")
|
|
}
|
|
|
|
connInfo, ok := value.(*ConnInfo)
|
|
if !ok {
|
|
panic("connInfo is set in context with wrong value type")
|
|
}
|
|
|
|
if connInfo == nil {
|
|
panic("connInfo is set in context with nil value")
|
|
}
|
|
|
|
return connInfo
|
|
}
|