-
Notifications
You must be signed in to change notification settings - Fork 47
/
compression.go
56 lines (43 loc) · 989 Bytes
/
compression.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
package config
import (
"fmt"
"github.com/BurntSushi/toml"
)
type CompAlgo int
const (
CompAlgoNone CompAlgo = iota
CompAlgoLZ4
)
var compressionMap = map[string]CompAlgo{
"none": CompAlgoNone,
"lz4": CompAlgoLZ4,
}
var compressionMapReversed = map[CompAlgo]string{}
func init() {
for k, v := range compressionMap {
compressionMapReversed[v] = k
}
}
// Compression wrapper for TOML
type Compression struct {
CompAlgo
}
var _ toml.TextMarshaler = &Compression{}
// UnmarshalText from TOML
func (c *Compression) UnmarshalText(text []byte) (err error) {
t := string(text)
algo, ok := compressionMap[t]
if !ok {
return fmt.Errorf("Compression algorithm '%s' not supported", t)
}
c.CompAlgo = algo
return
}
// MarshalText encode text with TOML format
func (c *Compression) MarshalText() ([]byte, error) {
return []byte(compressionMapReversed[c.CompAlgo]), nil
}
// Value return time.Duration value
func (c *Compression) Value() CompAlgo {
return c.CompAlgo
}