gofuncy/context.go
Kevin Franklin Kim 94afcde6ab
refactor: use zap
2025-05-13 08:32:38 +02:00

52 lines
1.2 KiB
Go

package gofuncy
import (
"context"
)
type contextKey string
const (
NoNameRoutine string = "noname"
contextKeyRoutine contextKey = "routine"
contextKeyParentRoutine contextKey = "parentRoutine"
contextKeySender contextKey = "sender"
)
func RootContext(ctx context.Context) context.Context {
return injectRoutineIntoContext(ctx, "root")
}
func injectRoutineIntoContext(ctx context.Context, name string) context.Context {
return context.WithValue(ctx, contextKeyRoutine, name)
}
func RoutineFromContext(ctx context.Context) string {
if value, ok := ctx.Value(contextKeyRoutine).(string); ok {
return value
}
return NoNameRoutine
}
func injectSenderIntoContext(ctx context.Context, name string) context.Context {
return context.WithValue(ctx, contextKeySender, name)
}
func SenderFromContext(ctx context.Context) string {
if value, ok := ctx.Value(contextKeySender).(string); ok {
return value
}
return ""
}
func injectParentRoutineIntoContext(ctx context.Context, name string) context.Context {
return context.WithValue(ctx, contextKeyParentRoutine, name)
}
func ParentRoutineFromContext(ctx context.Context) string {
if value, ok := ctx.Value(contextKeyParentRoutine).(string); ok {
return value
}
return ""
}