-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
93 lines (83 loc) · 2.23 KB
/
config.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
81
82
83
84
85
86
87
88
89
90
91
92
93
package parsley
type bufferType int
const (
none bufferType = iota
fixed
relative
)
// Filter specifies a filter structure with a field name (key) and a nested
// filter list used for nested objects.
type Filter struct {
Field string
Filter []Filter
}
// Config describes optional settings that alter the behavior of the encoder/decoder.
type Config struct {
btype bufferType
bsize int
filter []Filter
}
// UseFilter creates a config from a filter list. Only fields that appear in the
// filter list will be processed by the encoder/decoder If the filter is nil,
// all elements get processed.
//
// Example:
// var filter = []parsley.Filter{
// {"name": []parsley.Filter{
// {"firstName": nil},
// {"lastName": nil},
// }},
// {"address": []parsley.Filter{
// {"country": nil},
// {"city": nil},
// }},
// {"age": nil},
// }
func UseFilter(filter []Filter) Config {
return Config{
filter: filter,
}
}
// UseFixedBuffer creates a config to add extra buffer space for the encoder.
// The function adds a fixed number of bytes space.
//
// A larger buffer can speed up encoding by reducing required allocations.
// Extra buffer space is only recommended if the object contains strings with
// characters that should be escaped.
func UseFixedBuffer(bytes int) Config {
return Config{
btype: fixed,
bsize: bytes,
}
}
// UseRelativeBuffer creates a config to add extra buffer space for the encoder.
// The function calculates the length of all string fields and uses a percentage
// of that as extra buffer space.
//
// A larger buffer can speed up encoding by reducing required allocations.
// Extra buffer space is only recommended if the object contains strings with
// characters that should be escaped.
func UseRelativeBuffer(percentage int) Config {
return Config{
btype: relative,
bsize: percentage,
}
}
// MergeConfigs takes multiple config options and merges them into a single object.
func MergeConfigs(cfgs ...Config) Config {
cfg := Config{
btype: fixed,
bsize: 0,
filter: nil,
}
for _, e := range cfgs {
if e.btype != none {
cfg.btype = e.btype
cfg.bsize = e.bsize
}
if e.filter != nil {
cfg.filter = e.filter
}
}
return cfg
}