-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_struct.go
More file actions
59 lines (46 loc) · 840 Bytes
/
auto_struct.go
File metadata and controls
59 lines (46 loc) · 840 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
56
57
58
59
package autostruct
import "reflect"
const defaultTag = "auto"
type option func(*config)
type config struct {
tag string
cache *cache
deepCopy bool
}
func newConfig(opts ...option) *config {
cfg := &config{
tag: defaultTag,
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}
func WithTag(tag string) option {
return func(c *config) {
c.tag = tag
}
}
func WithCache(cache *cache) option {
return func(c *config) {
c.cache = cache
}
}
func WithDeepCopy() option {
return func(c *config) {
c.deepCopy = true
}
}
func Set(v any, opts ...option) error {
return structFieldsSetter(newConfig(opts...), reflect.ValueOf(v))
}
func MustSet(v any, opts ...option) {
if err := Set(v, opts...); err != nil {
panic(err)
}
}
func New[T any](opts ...option) T {
var v T
MustSet(&v, opts...)
return v
}