-
Notifications
You must be signed in to change notification settings - Fork 132
/
list.go
54 lines (45 loc) · 1.05 KB
/
list.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
package compress
import (
"bytes"
"encoding/gob"
)
func CompressStringArray(input []string, compressor Compressor) ([]byte, error) {
if len(input) <= 0 {
return []byte{}, nil
}
buf := &bytes.Buffer{}
err := gob.NewEncoder(buf).Encode(input)
if err != nil {
return nil, err
}
bs := buf.Bytes()
return compressor.Compress(bs)
}
func MustCompressStringArray(input []string, compressor Compressor) []byte {
b, err := CompressStringArray(input, compressor)
if err != nil {
panic(err)
}
return b
}
func DecompressStringArray(input []byte, decompressor Decompressor) ([]string, error) {
if len(input) <= 0 {
return []string{}, nil
}
decompressedValue, err := decompressor.Decompress(input)
if err != nil {
return nil, err
}
var data []string
buf := bytes.NewBuffer(decompressedValue)
dec := gob.NewDecoder(buf)
err = dec.Decode(&data)
return data, err
}
func MustDecompressStringArray(input []byte, compressor Decompressor) []string {
data, err := DecompressStringArray(input, compressor)
if err != nil {
panic(err)
}
return data
}