-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoptions.go
More file actions
55 lines (42 loc) · 1022 Bytes
/
options.go
File metadata and controls
55 lines (42 loc) · 1022 Bytes
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
package gomapper
import (
"github.com/insei/fmap/v3"
)
type withFuncOption[TSource, TDest any] struct {
fn func(TSource, *TDest)
}
type withFieldSkip[TDest any] struct {
field fmap.Field
}
type options struct {
Fns []any
Excluded []fmap.Field
}
type Option interface {
apply(*options)
}
func (a withFuncOption[TSource, TDest]) apply(opts *options) {
opts.Fns = append(opts.Fns, a.fn)
}
func (a withFieldSkip[TDest]) apply(opts *options) {
if opts.Excluded == nil {
opts.Excluded = make([]fmap.Field, 0)
}
opts.Excluded = append(opts.Excluded, a.field)
}
func WithFunc[TSource, TDest any](fn func(TSource, *TDest)) Option {
return &withFuncOption[TSource, TDest]{fn: fn}
}
func WithFieldSkip[TSource any](fn func(*TSource) any) Option {
source := new(TSource)
storage, err := fmap.GetFrom(source)
if err != nil {
panic(err)
}
fieldPtr := fn(source)
field, err := storage.GetFieldByPtr(source, fieldPtr)
if err != nil {
panic(err)
}
return &withFieldSkip[TSource]{field: field}
}