Rename ctx keys

This commit is contained in:
Daniel 2024-11-10 10:28:04 -05:00
parent e97c9ced0e
commit cecbeb78fe
3 changed files with 10 additions and 10 deletions

View file

@ -56,13 +56,13 @@ func NewClient(ctx context.Context, conf config.DB) (*DB, error) {
return db, nil return db, nil
} }
type DBCtxKey string type dBCtxKey string
const DBCtxKeyValue DBCtxKey = "dbctx" const DBCtxKey dBCtxKey = "dbctx"
// FromCtx returns the database handle from the provided Context. // FromCtx returns the database handle from the provided Context.
func FromCtx(ctx context.Context) *DB { func FromCtx(ctx context.Context) *DB {
c, ok := ctx.Value(DBCtxKeyValue).(*DB) c, ok := ctx.Value(DBCtxKey).(*DB)
if !ok { if !ok {
panic("no DB in context") panic("no DB in context")
} }
@ -72,7 +72,7 @@ func FromCtx(ctx context.Context) *DB {
// CtxWithDB returns a Context with the provided database handle. // CtxWithDB returns a Context with the provided database handle.
func CtxWithDB(ctx context.Context, conn *DB) context.Context { func CtxWithDB(ctx context.Context, conn *DB) context.Context {
return context.WithValue(ctx, DBCtxKeyValue, conn) return context.WithValue(ctx, DBCtxKey, conn)
} }
// IsNoRows is a convenience function that returns whether a returned error is a database // IsNoRows is a convenience function that returns whether a returned error is a database

View file

@ -27,8 +27,8 @@ func (s *Server) setupRoutes() {
} }
r := s.r r := s.r
r.Use(middleware.WithValue(database.DBCtxKeyValue, s.db)) r.Use(middleware.WithValue(database.DBCtxKey, s.db))
r.Use(middleware.WithValue(talkgroups.StoreCtxKeyValue, s.tgs)) r.Use(middleware.WithValue(talkgroups.StoreCtxKey, s.tgs))
s.installPprof() s.installPprof()

View file

@ -57,16 +57,16 @@ type Store interface {
HUP(*config.Config) HUP(*config.Config)
} }
type CtxStoreKey string type storeCtxKey string
const StoreCtxKeyValue CtxStoreKey = "store" const StoreCtxKey storeCtxKey = "store"
func CtxWithStore(ctx context.Context, s Store) context.Context { func CtxWithStore(ctx context.Context, s Store) context.Context {
return context.WithValue(ctx, StoreCtxKeyValue, s) return context.WithValue(ctx, StoreCtxKey, s)
} }
func StoreFrom(ctx context.Context) Store { func StoreFrom(ctx context.Context) Store {
s, ok := ctx.Value(StoreCtxKeyValue).(Store) s, ok := ctx.Value(StoreCtxKey).(Store)
if !ok { if !ok {
return NewCache() return NewCache()
} }