Skip to content

Commit 116cbcf

Browse files
authored
feat: Added option library (#58)
1 parent 6099f58 commit 116cbcf

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

option/function.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package option
2+
3+
type Function[T any] func(*T)
4+
5+
func Resolve[T any](opts ...Function[T]) *T {
6+
o := new(T)
7+
for _, opt := range opts {
8+
if opt != nil {
9+
opt(o)
10+
}
11+
}
12+
return o
13+
}

option/function_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package option_test
2+
3+
import (
4+
options "github.com/awslabs/operatorpkg/option"
5+
. "github.com/onsi/ginkgo/v2"
6+
. "github.com/onsi/gomega"
7+
)
8+
9+
type Option struct {
10+
Foo bool
11+
Bar string
12+
Baz int
13+
}
14+
15+
func FooOptions(o *Option) {
16+
o.Foo = true
17+
}
18+
func BarOptions(o *Option) {
19+
o.Bar = "bar"
20+
}
21+
22+
func BazOptions(baz int) options.Function[Option] {
23+
return func(o *Option) {
24+
o.Baz = baz
25+
}
26+
}
27+
28+
var _ = Describe("Function", func() {
29+
It("should resolve options", func() {
30+
Expect(options.Resolve(
31+
FooOptions,
32+
BarOptions,
33+
BazOptions(5),
34+
)).To(Equal(&Option{
35+
Foo: true,
36+
Bar: "bar",
37+
Baz: 5,
38+
}))
39+
40+
Expect(options.Resolve(
41+
FooOptions,
42+
BarOptions,
43+
)).To(Equal(&Option{
44+
Foo: true,
45+
Bar: "bar",
46+
}))
47+
48+
Expect(options.Resolve[Option]()).To(Equal(&Option{}))
49+
})
50+
})

option/suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package option_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/onsi/ginkgo/v2"
7+
"github.com/onsi/gomega"
8+
)
9+
10+
func Test(t *testing.T) {
11+
gomega.RegisterFailHandler(ginkgo.Fail)
12+
ginkgo.RunSpecs(t, "Options")
13+
}

0 commit comments

Comments
 (0)