-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
80 lines (67 loc) · 1.95 KB
/
options.go
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
70
71
72
73
74
75
76
77
78
79
80
package archeserde
import (
"reflect"
"github.com/mlange-42/arche/generic"
)
// Opts is a helper to create Option instances.
var Opts = Options{}
// Option is an option. Modifies o.
// Create them using [Opts].
type Option func(o *serdeOptions)
// Options is a helper to create Option instances.
// Use it via the instance [Opts].
type Options struct{}
// SkipAllResources skips serialization or de-serialization of all resources.
func (o Options) SkipAllResources() Option {
return func(o *serdeOptions) {
o.skipAllResources = true
}
}
// SkipAllComponents skips serialization or de-serialization of all components.
func (o Options) SkipAllComponents() Option {
return func(o *serdeOptions) {
o.skipAllComponents = true
}
}
// SkipEntities skips serialization or de-serialization of all entities and components.
func (o Options) SkipEntities() Option {
return func(o *serdeOptions) {
o.skipEntities = true
}
}
// SkipAllResources skips serialization or de-serialization of certain components.
//
// When deserializing, the skipped components must still be registered.
func (o Options) SkipComponents(comps ...generic.Comp) Option {
return func(o *serdeOptions) {
o.skipComponents = make([]reflect.Type, len(comps))
for i, c := range comps {
o.skipComponents[i] = reflect.Type(c)
}
}
}
// SkipAllResources skips serialization or de-serialization of certain resources.
//
// When deserializing, the skipped resources must still be registered.
func (o Options) SkipResources(comps ...generic.Comp) Option {
return func(o *serdeOptions) {
o.skipResources = make([]reflect.Type, len(comps))
for i, c := range comps {
o.skipResources[i] = reflect.Type(c)
}
}
}
type serdeOptions struct {
skipAllResources bool
skipAllComponents bool
skipEntities bool
skipComponents []reflect.Type
skipResources []reflect.Type
}
func newSerdeOptions(opts ...Option) serdeOptions {
o := serdeOptions{}
for _, opt := range opts {
opt(&o)
}
return o
}