File size: 597 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package contextx
import (
"context"
"github.com/peterbourgon/ctxdata/v4"
)
// WithAttr adds a key-value pair to the context.
func WithAttr(ctx context.Context, key string, value any) context.Context {
d := ctxdata.From(ctx)
if d == nil {
ctx, d = ctxdata.New(ctx)
}
_ = d.Set(key, value)
return ctx
}
// ctxdata adds amultiple key-value pairs to the context.
func WithAttrs(ctx context.Context, data map[string]string) context.Context {
d := ctxdata.From(ctx)
if d == nil {
ctx, d = ctxdata.New(ctx)
}
for key, value := range data {
_ = d.Set(key, value)
}
return ctx
}
|