Skip to content

Commit

Permalink
feat: Added option library (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellistarn committed Jul 1, 2024
1 parent 6099f58 commit 116cbcf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
13 changes: 13 additions & 0 deletions option/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package option

type Function[T any] func(*T)

func Resolve[T any](opts ...Function[T]) *T {
o := new(T)
for _, opt := range opts {
if opt != nil {
opt(o)
}
}
return o
}
50 changes: 50 additions & 0 deletions option/function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package option_test

import (
options "github.com/awslabs/operatorpkg/option"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

type Option struct {
Foo bool
Bar string
Baz int
}

func FooOptions(o *Option) {
o.Foo = true
}
func BarOptions(o *Option) {
o.Bar = "bar"
}

func BazOptions(baz int) options.Function[Option] {
return func(o *Option) {
o.Baz = baz
}
}

var _ = Describe("Function", func() {
It("should resolve options", func() {
Expect(options.Resolve(
FooOptions,
BarOptions,
BazOptions(5),
)).To(Equal(&Option{
Foo: true,
Bar: "bar",
Baz: 5,
}))

Expect(options.Resolve(
FooOptions,
BarOptions,
)).To(Equal(&Option{
Foo: true,
Bar: "bar",
}))

Expect(options.Resolve[Option]()).To(Equal(&Option{}))
})
})
13 changes: 13 additions & 0 deletions option/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package option_test

import (
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func Test(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Options")
}

0 comments on commit 116cbcf

Please sign in to comment.