opts

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 0 Imported by: 0

README

OPTS

Too simple, zero-dependencies library to flexible passing dynamic options

Go Reference Go Report Card

About

Too simple, zero-dependencies library to flexible passing of dynamic params into variadic functions.

Installation

go get github.com/rosemound/opts/v2

Usage mapped opts

import (
	"context"
	"errors"

	"github.com/rosemound/opts/v2"
)

// Mapped option key type
type OptionKey string

// Owner key
const OwnerKey OptionKey = "owner"

// Company key (like ctx keys)
const CompanyKey OptionKey = "company"

// simple service
type service struct {
	owner   string
	company any
}

// main
func main() {

	// service instantination with dynamic params
	s, err := NewService(context.Background(), WithCompany("test"), WithOwner("test"))

	// err handling
	if err != nil {
		panic(err)
	}

	// do staff
}

func NewService(ctx context.Context, o ...opts.Option[OptionKey]) (*service, error){

	// init option container with err handling
	c, err := opts.CreateContainerWithOptions(o)

	if err != nil {
		return nil, err
	}

	// or silently
	c := opts.CreateContainerWithOptionsS(o)

	s := service{}

	// extract values from container with res handling
	if !opts.OptionValue(c, OwnerKey, &s.owner) {
		return nil, errors.New("some err")
	}

	// or ignore 
	_ = opts.OptionValue(c, CompanyKey, &s.company)

	// ret your instance
	return &s, nil
}

// With company
func WithCompany(company any) opts.Option[OptionKey] {
	return func(o opts.OptionContainer[OptionKey]) error {
		o.Set(CompanyKey, company)
		return nil
	}
}

// With owner & err
func WithOwner(val string) opts.Option[OptionKey] {
	var err error

	if val == "" {
		err = errors.New("owner must be present")
	}

	return func(o opts.OptionContainer[OptionKey]) error {
		if err == nil {
			o.Set(OwnerKey, val)
		}

		return err
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptionValue added in v2.1.0

func OptionValue[K comparable, V any](optc OptionContainer[K], k K, buff *V) bool

Types

type Option

type Option[T comparable] func(o OptionContainer[T]) error

type OptionContainer

type OptionContainer[T comparable] map[T]any

func CreateContainer

func CreateContainer[T comparable]() OptionContainer[T]

func CreateContainerWithOptions

func CreateContainerWithOptions[T comparable](o []Option[T]) (OptionContainer[T], error)

func CreateContainerWithOptionsS

func CreateContainerWithOptionsS[T comparable](o []Option[T]) OptionContainer[T]

func (OptionContainer[T]) Apply

func (c OptionContainer[T]) Apply(opts ...Option[T]) error

func (OptionContainer[T]) ApplyA

func (c OptionContainer[T]) ApplyA(opts []Option[T]) error

func (OptionContainer[T]) ApplySilent

func (c OptionContainer[T]) ApplySilent(opts ...Option[T]) error

func (OptionContainer[T]) ApplySilentA

func (c OptionContainer[T]) ApplySilentA(opts []Option[T]) error

func (OptionContainer[T]) Exist

func (c OptionContainer[T]) Exist(k T) bool

func (OptionContainer[T]) Get

func (c OptionContainer[T]) Get(k T) any

func (OptionContainer[T]) Set

func (c OptionContainer[T]) Set(k T, v any) OptionContainer[T]

Jump to

Keyboard shortcuts

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