From 1ca2e1420d6455ffdd330dffb768b83c5759e377 Mon Sep 17 00:00:00 2001 From: rejchev Date: Thu, 23 Oct 2025 19:16:11 +0500 Subject: [PATCH 1/4] ref: add `Exist()` method on opts --- opts/opts.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/opts/opts.go b/opts/opts.go index ade0713..82c74c3 100644 --- a/opts/opts.go +++ b/opts/opts.go @@ -8,6 +8,11 @@ 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] } From 4699fdcb78743f4d8665a5da999ac8b728a1dfd6 Mon Sep 17 00:00:00 2001 From: rejchev Date: Thu, 23 Oct 2025 19:16:28 +0500 Subject: [PATCH 2/4] ref: add `Exist()` method on mopts --- mopts/mopts.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mopts/mopts.go b/mopts/mopts.go index 7ca0a7b..1e89795 100644 --- a/mopts/mopts.go +++ b/mopts/mopts.go @@ -8,6 +8,11 @@ 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] } From f59a8373ad808f4f7ffa95131ea9d7c7902eee45 Mon Sep 17 00:00:00 2001 From: rejchev Date: Thu, 25 Dec 2025 17:15:46 +0500 Subject: [PATCH 3/4] 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 -} From 51f9a31e55425a8f942f0b801586cfb82512eed8 Mon Sep 17 00:00:00 2001 From: rejchev Date: Thu, 25 Dec 2025 17:22:41 +0500 Subject: [PATCH 4/4] chore: rename mopts to opts --- mopts.go => opts.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename mopts.go => opts.go (100%) diff --git a/mopts.go b/opts.go similarity index 100% rename from mopts.go rename to opts.go