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

mlange-42 / ark-serde / 14020897854

23 Mar 2025 04:33PM CUT coverage: 94.045% (-2.0%) from 96.023%
14020897854

push

github

web-flow
Gzip compression (#18)

42 of 52 new or added lines in 4 files covered. (80.77%)

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 serialization data created with this option,
30
// the option must also be used for de-serialization.
31
// The optional compression level argument has no effect when de-serializing.
32
func (o Options) Compress(level ...int) Option {
5✔
33
        l := DefaultCompression
5✔
34
        if len(level) == 1 {
6✔
35
                l = level[0]
1✔
36
        } else if len(level) != 0 {
6✔
37
                panic("maximum one value allowed for compression level")
1✔
38
        }
39

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

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

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

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

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

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

91
type serdeOptions struct {
92
        skipAllResources  bool
93
        skipAllComponents bool
94
        skipEntities      bool
95

96
        compressed       bool
97
        compressionLevel int
98

99
        skipComponents []reflect.Type
100
        skipResources  []reflect.Type
101
}
102

103
func newSerdeOptions(opts ...Option) serdeOptions {
44✔
104
        o := serdeOptions{}
44✔
105
        for _, opt := range opts {
63✔
106
                opt(&o)
19✔
107
        }
19✔
108
        return o
44✔
109
}
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