member_grouping_options.gno
1.17 Kb ยท 36 lines
1package commondao
2
3// MemberGroupingOption configures member groupings.
4type MemberGroupingOption func(MemberGrouping)
5
6// UseStorageFactory assigns a custom member storage creation function to the grouping.
7// Creation function is called each time a member group is added, with the name of the
8// group as the only argument, to create a storage where the new group stores its members.
9func UseStorageFactory(fn func(group string) MemberStorage) MemberGroupingOption {
10 if fn == nil {
11 panic("storage factory function must not be nil")
12 }
13
14 return func(g MemberGrouping) {
15 grouping, ok := g.(*memberGrouping)
16 if !ok {
17 panic("storage factory not supported by member grouping")
18 }
19
20 grouping.createStorage = fn
21 }
22}
23
24// WithGroups creates multiple members groups.
25// To use a custom member storage factory to create the groups make sure that this option
26// comes after the `UseStorageFactory()` option, otherwise groups are created using the
27// default factory which is `commondao.NewMemberStorage()`.
28func WithGroups(names ...string) MemberGroupingOption {
29 return func(g MemberGrouping) {
30 for _, name := range names {
31 if _, err := g.Add(name); err != nil {
32 panic(err)
33 }
34 }
35 }
36}