isle/go/daemon/jsonrpc2/meta.go

31 lines
757 B
Go
Raw Normal View History

package jsonrpc2
import (
"context"
"maps"
)
type ctxKeyMeta int
// WithMeta returns a Context where the given key will be set to the given value
// in the Meta field of all JSONRPC2 requests made using Clients from this
// package.
func WithMeta(ctx context.Context, key string, value any) context.Context {
m, _ := ctx.Value(ctxKeyMeta(0)).(map[string]any)
if m == nil {
m = map[string]any{}
} else {
m = maps.Clone(m)
}
m[key] = value
return context.WithValue(ctx, ctxKeyMeta(0), m)
}
// GetMeta returns all key/values which have been set on the Context using
// WithMeta. This may return nil if WithMeta was never called.
func GetMeta(ctx context.Context) map[string]any {
m, _ := ctx.Value(ctxKeyMeta(0)).(map[string]any)
return m
}