options

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: Apache-2.0 Imports: 2 Imported by: 5

README

CI Go Report Card GoDoc

options

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

Index

func AddToOptions

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

AddToOptions adds options to context if no options found, create a new one and adds the provided options to it and returns the new context

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

type Options struct {
    sync.Map
}

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 (*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 an options can be used to delete options from context

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

Get an options can be used to get options from context

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 AddToOptions

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

AddToOptions adds options to context if no options found, create a new one and adds the provided options to it and returns the new context

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 {
	sync.Map
}

Options are request options passed from ColdBrew to server

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 (*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 an options can be used to delete options from context

func (*Options) Get

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

Get an options can be used to get options from context

Jump to

Keyboard shortcuts

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