options

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2026 License: Apache-2.0 Imports: 3 Imported by: 5

README

CI Go Report Card GoDoc

options

import "github.com/go-coldbrew/options"

Index

Constants

SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages reference this to enforce version compatibility.

const SupportPackageIsVersion1 = true

func AddToLogFields

func AddToLogFields(ctx context.Context, key string, value any) context.Context

AddToLogFields adds a key-value pair to the log fields stored in ctx. If ctx is nil, context.Background() is used.

func AddToOptions

func AddToOptions(ctx context.Context, key string, value any) context.Context

AddToOptions adds a key-value pair to the Options stored in ctx. If no Options exists in the context, a new one is created. Empty keys are silently ignored and do not allocate.

Example

package main

import (
	"context"
	"fmt"

	"github.com/go-coldbrew/options"
)

func main() {
	ctx := context.Background()

	// Add request-scoped metadata to context
	ctx = options.AddToOptions(ctx, "tenant", "acme-corp")
	ctx = options.AddToOptions(ctx, "region", "us-west-2")

	// Retrieve values downstream
	opts := options.FromContext(ctx)
	if tenant, ok := opts.Get("tenant"); ok {
		fmt.Println("tenant:", tenant)
	}
	if region, ok := opts.Get("region"); ok {
		fmt.Println("region:", region)
	}
}
Output
tenant: acme-corp
region: us-west-2

type Options

Options are request options passed from ColdBrew to server. Uses RWMutex + map instead of sync.Map since Options is per-request and never shared across goroutines.

type Options struct {
    // contains filtered or unexported fields
}

func FromContext
func FromContext(ctx context.Context) *Options

FromContext fetches options from provided context. If no options are found, it returns nil.

Example

package main

import (
	"context"
	"fmt"

	"github.com/go-coldbrew/options"
)

func main() {
	ctx := context.Background()

	// Without any options set, FromContext returns nil
	opts := options.FromContext(ctx)
	fmt.Println("opts:", opts)
}
Output
opts: <nil>

func LogFieldsFromContext
func LogFieldsFromContext(ctx context.Context) *Options

LogFieldsFromContext retrieves the log fields Options from context. Returns nil if not present.

func (*Options) Add
func (o *Options) Add(key string, value any)

Add adds a key-value pair to Options. Empty keys are silently ignored.

func (*Options) Del
func (o *Options) Del(key string)

Del deletes an option by key.

func (*Options) Delete
func (o *Options) Delete(key any)

Delete is a sync.Map-compatible alias for Del. Only string keys are supported; non-string keys are silently ignored.

func (*Options) Get
func (o *Options) Get(key string) (any, bool)

Get retrieves an option value by key.

func (*Options) Load
func (o *Options) Load(key any) (any, bool)

Load is a sync.Map-compatible alias for Get. Only string keys are supported; non-string keys return (nil, false).

func (*Options) Range
func (o *Options) Range(f func(key, value any) bool)

Range calls f sequentially for each key and value. If f returns false, Range stops the iteration. The callback may safely call Add/Del on the same Options instance.

func (*Options) RangeSlice
func (o *Options) RangeSlice(f func(key, value any) bool)

RangeSlice calls f sequentially for each key and value, using a slice snapshot. This is more efficient than Range for small maps and matches the iteration pattern used by LogFields.

func (*Options) Store
func (o *Options) Store(key, value any)

Store is a sync.Map-compatible alias for Add. Only string keys are supported; non-string keys are silently ignored.

type RequestContext

RequestContext holds both request-scoped options and log-scoped fields in a single context value, reducing context.WithValue allocations from 2 to 1. Both fields are eagerly initialized to avoid data races from concurrent lazy init.

type RequestContext struct {
    // contains filtered or unexported fields
}

func RequestContextFromContext
func RequestContextFromContext(ctx context.Context) *RequestContext

RequestContextFromContext retrieves the RequestContext from ctx. Returns nil if not present.

func (*RequestContext) LogFields
func (rc *RequestContext) LogFields() *Options

LogFields returns the Options used for log fields.

func (*RequestContext) Opts
func (rc *RequestContext) Opts() *Options

Opts returns the Options for request-scoped key-value pairs.

Generated by gomarkdoc

Documentation

Index

Examples

Constants

View Source
const SupportPackageIsVersion1 = true

SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages reference this to enforce version compatibility.

Variables

This section is empty.

Functions

func AddToLogFields added in v0.3.0

func AddToLogFields(ctx context.Context, key string, value any) context.Context

AddToLogFields adds a key-value pair to the log fields stored in ctx. If ctx is nil, context.Background() is used.

func AddToOptions

func AddToOptions(ctx context.Context, key string, value any) context.Context

AddToOptions adds a key-value pair to the Options stored in ctx. If no Options exists in the context, a new one is created. Empty keys are silently ignored and do not allocate.

Example
package main

import (
	"context"
	"fmt"

	"github.com/go-coldbrew/options"
)

func main() {
	ctx := context.Background()

	// Add request-scoped metadata to context
	ctx = options.AddToOptions(ctx, "tenant", "acme-corp")
	ctx = options.AddToOptions(ctx, "region", "us-west-2")

	// Retrieve values downstream
	opts := options.FromContext(ctx)
	if tenant, ok := opts.Get("tenant"); ok {
		fmt.Println("tenant:", tenant)
	}
	if region, ok := opts.Get("region"); ok {
		fmt.Println("region:", region)
	}
}
Output:
tenant: acme-corp
region: us-west-2

Types

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options are request options passed from ColdBrew to server. Uses RWMutex + map instead of sync.Map since Options is per-request and never shared across goroutines.

func FromContext

func FromContext(ctx context.Context) *Options

FromContext fetches options from provided context. If no options are found, it returns nil.

Example
package main

import (
	"context"
	"fmt"

	"github.com/go-coldbrew/options"
)

func main() {
	ctx := context.Background()

	// Without any options set, FromContext returns nil
	opts := options.FromContext(ctx)
	fmt.Println("opts:", opts)
}
Output:
opts: <nil>

func LogFieldsFromContext added in v0.3.0

func LogFieldsFromContext(ctx context.Context) *Options

LogFieldsFromContext retrieves the log fields Options from context. Returns nil if not present.

func (*Options) Add

func (o *Options) Add(key string, value any)

Add adds a key-value pair to Options. Empty keys are silently ignored.

func (*Options) Del

func (o *Options) Del(key string)

Del deletes an option by key.

func (*Options) Delete added in v0.2.7

func (o *Options) Delete(key any)

Delete is a sync.Map-compatible alias for Del. Only string keys are supported; non-string keys are silently ignored.

func (*Options) Get

func (o *Options) Get(key string) (any, bool)

Get retrieves an option value by key.

func (*Options) Load added in v0.2.7

func (o *Options) Load(key any) (any, bool)

Load is a sync.Map-compatible alias for Get. Only string keys are supported; non-string keys return (nil, false).

func (*Options) Range added in v0.2.7

func (o *Options) Range(f func(key, value any) bool)

Range calls f sequentially for each key and value. If f returns false, Range stops the iteration. The callback may safely call Add/Del on the same Options instance.

func (*Options) RangeSlice added in v0.3.0

func (o *Options) RangeSlice(f func(key, value any) bool)

RangeSlice calls f sequentially for each key and value, using a slice snapshot. This is more efficient than Range for small maps and matches the iteration pattern used by LogFields.

func (*Options) Store added in v0.2.7

func (o *Options) Store(key, value any)

Store is a sync.Map-compatible alias for Add. Only string keys are supported; non-string keys are silently ignored.

type RequestContext added in v0.3.0

type RequestContext struct {
	// contains filtered or unexported fields
}

RequestContext holds both request-scoped options and log-scoped fields in a single context value, reducing context.WithValue allocations from 2 to 1. Both fields are eagerly initialized to avoid data races from concurrent lazy init.

func RequestContextFromContext added in v0.3.0

func RequestContextFromContext(ctx context.Context) *RequestContext

RequestContextFromContext retrieves the RequestContext from ctx. Returns nil if not present.

func (*RequestContext) LogFields added in v0.3.0

func (rc *RequestContext) LogFields() *Options

LogFields returns the Options used for log fields.

func (*RequestContext) Opts added in v0.3.0

func (rc *RequestContext) Opts() *Options

Opts returns the Options for request-scoped key-value pairs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL