• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark-serde / 14021510664

23 Mar 2025 05:52PM CUT coverage: 94.045%. Remained the same
14021510664

push

github

web-flow
Add GZIP to README features (#20)

379 of 403 relevant lines covered (94.04%)

97.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/options.go
1
package arkserde
2

3
import (
4
        "compress/flate"
5
        "reflect"
6

7
        "github.com/mlange-42/ark/ecs"
8
)
9

10
// GZip compression level, re-exported from the flate package
11
const (
12
        BestSpeed          = flate.BestSpeed
13
        BestCompression    = flate.BestCompression
14
        DefaultCompression = flate.DefaultCompression
15
)
16

17
// Opts is a helper to create Option instances.
18
var Opts = Options{}
19

20
// Option is an option. Modifies o.
21
// Create them using [Opts].
22
type Option func(o *serdeOptions)
23

24
// Options is a helper to create Option instances.
25
// Use it via the instance [Opts].
26
type Options struct{}
27

28
// Compress data using gzip.
29
// For serialized data created with this option,
30
// the option must also be used for deserialization.
31
// The optional compression level argument has no effect when deserializing.
32
//
33
// Ideally, save as <file>.json.gz instead of <file>.json.
34
func (o Options) Compress(level ...int) Option {
5✔
35
        l := DefaultCompression
5✔
36
        if len(level) == 1 {
6✔
37
                l = level[0]
1✔
38
        } else if len(level) != 0 {
6✔
39
                panic("maximum one value allowed for compression level")
1✔
40
        }
41

42
        return func(o *serdeOptions) {
8✔
43
                o.compressed = true
4✔
44
                o.compressionLevel = l
4✔
45
        }
4✔
46
}
47

48
// SkipAllResources skips serialization or de-serialization of all resources.
49
func (o Options) SkipAllResources() Option {
3✔
50
        return func(o *serdeOptions) {
6✔
51
                o.skipAllResources = true
3✔
52
        }
3✔
53
}
54

55
// SkipAllComponents skips serialization or de-serialization of all components.
56
func (o Options) SkipAllComponents() Option {
3✔
57
        return func(o *serdeOptions) {
6✔
58
                o.skipAllComponents = true
3✔
59
        }
3✔
60
}
61

62
// SkipEntities skips serialization or de-serialization of all entities and components.
63
func (o Options) SkipEntities() Option {
3✔
64
        return func(o *serdeOptions) {
6✔
65
                o.skipEntities = true
3✔
66
        }
3✔
67
}
68

69
// SkipComponents skips serialization or de-serialization of certain components.
70
//
71
// When deserializing, the skipped components must still be registered.
72
func (o Options) SkipComponents(comps ...ecs.Comp) Option {
3✔
73
        return func(o *serdeOptions) {
6✔
74
                o.skipComponents = make([]reflect.Type, len(comps))
3✔
75
                for i, c := range comps {
6✔
76
                        o.skipComponents[i] = c.Type()
3✔
77
                }
3✔
78
        }
79
}
80

81
// SkipResources skips serialization or de-serialization of certain resources.
82
//
83
// When deserializing, the skipped resources must still be registered.
84
func (o Options) SkipResources(comps ...ecs.Comp) Option {
3✔
85
        return func(o *serdeOptions) {
6✔
86
                o.skipResources = make([]reflect.Type, len(comps))
3✔
87
                for i, c := range comps {
6✔
88
                        o.skipResources[i] = c.Type()
3✔
89
                }
3✔
90
        }
91
}
92

93
type serdeOptions struct {
94
        skipAllResources  bool
95
        skipAllComponents bool
96
        skipEntities      bool
97

98
        compressed       bool
99
        compressionLevel int
100

101
        skipComponents []reflect.Type
102
        skipResources  []reflect.Type
103
}
104

105
func newSerdeOptions(opts ...Option) serdeOptions {
44✔
106
        o := serdeOptions{}
44✔
107
        for _, opt := range opts {
63✔
108
                opt(&o)
19✔
109
        }
19✔
110
        return o
44✔
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc