From f59a8373ad808f4f7ffa95131ea9d7c7902eee45 Mon Sep 17 00:00:00 2001 From: rejchev Date: Thu, 25 Dec 2025 17:15:46 +0500 Subject: [PATCH] feat: v2 impl --- go.mod | 4 +-- mopts.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ mopts/mopts.go | 46 ----------------------------------- opts/opts.go | 46 ----------------------------------- 4 files changed, 68 insertions(+), 94 deletions(-) create mode 100644 mopts.go delete mode 100644 mopts/mopts.go delete mode 100644 opts/opts.go diff --git a/go.mod b/go.mod index 9f99dbd..6ecf5ef 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/rosemound/opts +module github.com/rosemound/opts/v2 -go 1.24.0 +go 1.25.0 diff --git a/mopts.go b/mopts.go new file mode 100644 index 0000000..494426f --- /dev/null +++ b/mopts.go @@ -0,0 +1,66 @@ +package opts + +type OptionContainer[T comparable] map[T]any + +type Option[T comparable] func(o OptionContainer[T]) error + +func CreateContainer[T comparable]() OptionContainer[T] { + return OptionContainer[T]{} +} + +func CreateContainerWithOptions[T comparable](o []Option[T]) (OptionContainer[T], error) { + c := CreateContainer[T]() + if err := c.ApplyA(o); err != nil { + return nil, err + } + + return c, nil +} + +func CreateContainerWithOptionsS[T comparable](o []Option[T]) OptionContainer[T] { + c := CreateContainer[T]() + _ = c.ApplySilentA(o) + return c +} + +func (c OptionContainer[T]) Set(k T, v any) OptionContainer[T] { + c[k] = v + return c +} + +func (c OptionContainer[T]) Exist(k T) bool { + _, ok := c[k] + return ok +} + +func (c OptionContainer[T]) Get(k T) any { + return c[k] +} + +func (c OptionContainer[T]) Apply(opts ...Option[T]) error { + return c.ApplyA(opts) +} + +func (c OptionContainer[T]) ApplySilent(opts ...Option[T]) error { + return c.ApplySilentA(opts) +} + +func (c OptionContainer[T]) ApplyA(opts []Option[T]) error { + var err error + + for _, opt := range opts { + if err = opt(c); err != nil { + return err + } + } + + return err +} + +func (c OptionContainer[T]) ApplySilentA(opts []Option[T]) error { + for _, opt := range opts { + opt(c) + } + + return nil +} diff --git a/mopts/mopts.go b/mopts/mopts.go deleted file mode 100644 index 1e89795..0000000 --- a/mopts/mopts.go +++ /dev/null @@ -1,46 +0,0 @@ -package mopts - -type MoptionContainer[T comparable] map[T]any - -type Moption[T comparable] func(o MoptionContainer[T]) error - -func (c MoptionContainer[T]) Set(k T, v any) { - c[k] = v -} - -func (c MoptionContainer[T]) Exist(k T) bool { - _, ok := c[k] - return ok -} - -func (c MoptionContainer[T]) Get(k T) any { - return c[k] -} - -func (c MoptionContainer[T]) Apply(opts ...Moption[T]) error { - return c.ApplyA(opts) -} - -func (c MoptionContainer[T]) ApplySilent(opts ...Moption[T]) error { - return c.ApplySilentA(opts) -} - -func (c MoptionContainer[T]) ApplyA(opts []Moption[T]) error { - var err error - - for _, opt := range opts { - if err = opt(c); err != nil { - return err - } - } - - return err -} - -func (c MoptionContainer[T]) ApplySilentA(opts []Moption[T]) error { - for _, opt := range opts { - opt(c) - } - - return nil -} diff --git a/opts/opts.go b/opts/opts.go deleted file mode 100644 index 82c74c3..0000000 --- a/opts/opts.go +++ /dev/null @@ -1,46 +0,0 @@ -package opts - -type OptionContainer map[any]any - -type Option func(c OptionContainer) error - -func (c OptionContainer) Set(k any, v any) { - c[k] = v -} - -func (c OptionContainer) Exist(k any) bool { - _, ok := c[k] - return ok -} - -func (c OptionContainer) Get(k any) any { - return c[k] -} - -func (c OptionContainer) Apply(opts ...Option) error { - return c.ApplyA(opts) -} - -func (c OptionContainer) ApplySilent(opts ...Option) error { - return c.ApplySilentA(opts) -} - -func (c OptionContainer) ApplyA(opts []Option) error { - var err error - - for _, opt := range opts { - if err = opt(c); err != nil { - return err - } - } - - return err -} - -func (c OptionContainer) ApplySilentA(opts []Option) error { - for _, opt := range opts { - opt(c) - } - - return nil -}