Files
summercms/towel/context.go
Jakub Zych a654e96d66 feat(01-02): dispatch typed events with request context
- App-owned festival bus with Fire, Collect and UntilHandled
- Recover listener panics with owner plugin IDs; isolate buses per app
- towel context accessors and hello command demo of all three modes
2026-09-16 13:30:46 +02:00

64 lines
1.7 KiB
Go

package towel
import "context"
type actorKey struct{}
type organizationKey struct{}
type collectionKey struct{}
type localeKey struct{}
func withValue(ctx context.Context, key, value any) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, key, value)
}
func stringValue(ctx context.Context, key any) (string, bool) {
if ctx == nil {
return "", false
}
v, ok := ctx.Value(key).(string)
return v, ok
}
// WithActor stores the request actor on ctx.
func WithActor(ctx context.Context, actor string) context.Context {
return withValue(ctx, actorKey{}, actor)
}
// Actor returns the request actor from ctx.
func Actor(ctx context.Context) (string, bool) {
return stringValue(ctx, actorKey{})
}
// WithOrganization stores the request organization on ctx.
func WithOrganization(ctx context.Context, org string) context.Context {
return withValue(ctx, organizationKey{}, org)
}
// Organization returns the request organization from ctx.
func Organization(ctx context.Context) (string, bool) {
return stringValue(ctx, organizationKey{})
}
// WithCollection stores the request collection on ctx.
func WithCollection(ctx context.Context, collection string) context.Context {
return withValue(ctx, collectionKey{}, collection)
}
// Collection returns the request collection from ctx.
func Collection(ctx context.Context) (string, bool) {
return stringValue(ctx, collectionKey{})
}
// WithLocale stores the request locale on ctx.
func WithLocale(ctx context.Context, locale string) context.Context {
return withValue(ctx, localeKey{}, locale)
}
// Locale returns the request locale from ctx.
func Locale(ctx context.Context) (string, bool) {
return stringValue(ctx, localeKey{})
}