openmeter / pkg /contextx /attr.go
Leon4gr45's picture
Upload folder using huggingface_hub (part 9)
fea99b3 verified
Raw
History Blame Contribute Delete
597 Bytes
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
}