File tree Expand file tree Collapse file tree 4 files changed +68
-94
lines changed
Expand file tree Collapse file tree 4 files changed +68
-94
lines changed Original file line number Diff line number Diff line change 1- module github.com/rosemound/opts
1+ module github.com/rosemound/opts/v2
22
3- go 1.24 .0
3+ go 1.25 .0
Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments