Skip to content

Commit f9a8dde

Browse files
authored
Merge pull request #1 from Rosemound/v2
feat: v2 impl
2 parents 4699fdc + f59a837 commit f9a8dde

File tree

4 files changed

+68
-94
lines changed

4 files changed

+68
-94
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/rosemound/opts
1+
module github.com/rosemound/opts/v2
22

3-
go 1.24.0
3+
go 1.25.0

mopts.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package opts
2+
3+
type OptionContainer[T comparable] map[T]any
4+
5+
type Option[T comparable] func(o OptionContainer[T]) error
6+
7+
func CreateContainer[T comparable]() OptionContainer[T] {
8+
return OptionContainer[T]{}
9+
}
10+
11+
func CreateContainerWithOptions[T comparable](o []Option[T]) (OptionContainer[T], error) {
12+
c := CreateContainer[T]()
13+
if err := c.ApplyA(o); err != nil {
14+
return nil, err
15+
}
16+
17+
return c, nil
18+
}
19+
20+
func CreateContainerWithOptionsS[T comparable](o []Option[T]) OptionContainer[T] {
21+
c := CreateContainer[T]()
22+
_ = c.ApplySilentA(o)
23+
return c
24+
}
25+
26+
func (c OptionContainer[T]) Set(k T, v any) OptionContainer[T] {
27+
c[k] = v
28+
return c
29+
}
30+
31+
func (c OptionContainer[T]) Exist(k T) bool {
32+
_, ok := c[k]
33+
return ok
34+
}
35+
36+
func (c OptionContainer[T]) Get(k T) any {
37+
return c[k]
38+
}
39+
40+
func (c OptionContainer[T]) Apply(opts ...Option[T]) error {
41+
return c.ApplyA(opts)
42+
}
43+
44+
func (c OptionContainer[T]) ApplySilent(opts ...Option[T]) error {
45+
return c.ApplySilentA(opts)
46+
}
47+
48+
func (c OptionContainer[T]) ApplyA(opts []Option[T]) error {
49+
var err error
50+
51+
for _, opt := range opts {
52+
if err = opt(c); err != nil {
53+
return err
54+
}
55+
}
56+
57+
return err
58+
}
59+
60+
func (c OptionContainer[T]) ApplySilentA(opts []Option[T]) error {
61+
for _, opt := range opts {
62+
opt(c)
63+
}
64+
65+
return nil
66+
}

mopts/mopts.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

opts/opts.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)