Home

opt @main - refs - log -
-
https://git.jolheiser.com/opt.git
Functional options
opt / opt_test.go
- raw -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package opt_test

import (
	"errors"
	"strconv"
	"testing"

	"go.jolheiser.com/opt"

	"github.com/matryer/is"
)

func TestOptions(t *testing.T) {
	assert := is.New(t)

	f := NewFoo(WithBar("bar"))
	assert.Equal(f.Bar, "bar") // Option should set bar

	opt.Apply(f, WithBaz(100))
	assert.Equal(f.Baz, 100) // Apply should set Baz

	_, err := NewErrorFoo(WithBarErr("bar"))
	assert.True(err != nil)          // ErrorOption should return error
	assert.Equal(err.Error(), "bar") // ErrorOption error should be bar

	err = opt.ApplyError(f, WithBazErr(100))
	assert.True(err != nil)          // ApplyError should return error
	assert.Equal(err.Error(), "100") // ApplyError error should be 100
}

type Foo struct {
	Bar string
	Baz int
}

func WithBar(b string) opt.Func[Foo] {
	return func(f *Foo) {
		f.Bar = b
	}
}

func WithBaz(b int) opt.Func[Foo] {
	return func(f *Foo) {
		f.Baz = b
	}
}

func NewFoo(opts ...opt.Func[Foo]) *Foo {
	f := &Foo{ /** some defaults **/ }
	opt.Apply(f, opts...)
	return f
}

func WithBarErr(b string) opt.ErrorFunc[Foo] {
	return func(f *Foo) error {
		return errors.New(b)
	}
}

func WithBazErr(b int) opt.ErrorFunc[Foo] {
	return func(f *Foo) error {
		return errors.New(strconv.Itoa(b))
	}
}

func NewErrorFoo(opts ...opt.ErrorFunc[Foo]) (*Foo, error) {
	f := &Foo{ /** some defaults **/ }
	return f, opt.ApplyError(f, opts...)
}