forked from rainycape/gondola
/
pipe.go
57 lines (50 loc) · 1.65 KB
/
pipe.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
// Package pipe implements pipes which transform data,
// generally for compressing it.
//
// This package includes the pipe "zlib", which compreses
// any value bigger than 100 bytes using the default zlib
// compression level. To tell compressed and uncompressed
// data apart, it prepends a byte to its output (0 for
// uncompressed, 1 for compressed).
package pipe
import (
"fmt"
"gnd.la/util/structs"
)
var (
registry = map[string]*Pipe{}
)
// Pipe represents a codec pipe, which can encode and decode data
// as it's saved to or loaded from the database.
type Pipe struct {
// Encode passes the given data trough the pipe and produces
// a new ouput. len(data) is always > 0.
Encode func(data []byte) ([]byte, error)
// Decode performs the inverse operation of Encode, producing
// the original input to Encode from its output.
// len(data) is always > 0.
Decode func(data []byte) ([]byte, error)
}
// Register registers a new Pipe into the pipe registry.
// If there's already a Pipe with the same name, it will panic.
func Register(name string, p *Pipe) {
if _, ok := registry[name]; ok {
panic(fmt.Errorf("there's already a codec pipe named %q", name))
}
registry[name] = p
}
// Get returns the Pipe with the give name, or nil if there's no such Pipe.
func Get(name string) *Pipe {
return registry[name]
}
// FromTag returns the pipe for a given field tag.
func FromTag(t *structs.Tag) *Pipe {
return registry[t.PipeName()]
}
// RequiredImport returns the import required
// for using the pipe with the given name, or
// the empty string if the pipe is not known.
func RequiredImport(name string) string {
// no pipes require imports for now
return ""
}