2022-11-21 14:39:53 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Placeholder to use as unique key in context.Context.
|
|
|
|
var bundleKey int
|
|
|
|
|
|
|
|
// Context stores the specified bundle on a new context.
|
|
|
|
// The bundle is available through the `Get()` function.
|
|
|
|
func Context(ctx context.Context, b *Bundle) context.Context {
|
|
|
|
return context.WithValue(ctx, &bundleKey, b)
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:33:30 +00:00
|
|
|
// GetOrNil returns the bundle as configured on the context.
|
|
|
|
// It returns nil if it isn't configured.
|
|
|
|
func GetOrNil(ctx context.Context) *Bundle {
|
|
|
|
bundle, ok := ctx.Value(&bundleKey).(*Bundle)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bundle
|
|
|
|
}
|
|
|
|
|
2022-11-21 14:39:53 +00:00
|
|
|
// Get returns the bundle as configured on the context.
|
|
|
|
// It panics if it isn't configured.
|
|
|
|
func Get(ctx context.Context) *Bundle {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := GetOrNil(ctx)
|
|
|
|
if b == nil {
|
2022-11-21 14:39:53 +00:00
|
|
|
panic("context not configured with bundle")
|
|
|
|
}
|
2023-11-15 14:03:36 +00:00
|
|
|
return b
|
2022-11-21 14:39:53 +00:00
|
|
|
}
|