forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
40 lines (31 loc) · 868 Bytes
/
codec.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
package outputs
import (
"fmt"
"github.com/elastic/beats/libbeat/common"
)
type Codec interface {
Encode(Event common.MapStr) ([]byte, error)
}
type CodecConfig struct {
Namespace common.ConfigNamespace `config:",inline"`
}
type CodecFactory func(*common.Config) (Codec, error)
var outputCodecs = map[string]CodecFactory{}
func RegisterOutputCodec(name string, gen CodecFactory) {
if _, exists := outputCodecs[name]; exists {
panic(fmt.Sprintf("output codec '%v' already registered ", name))
}
outputCodecs[name] = gen
}
func CreateEncoder(cfg CodecConfig) (Codec, error) {
// default to json codec
codec := "json"
if name := cfg.Namespace.Name(); name != "" {
codec = name
}
factory := outputCodecs[codec]
if factory == nil {
return nil, fmt.Errorf("'%v' output codec is not available", codec)
}
return factory(cfg.Namespace.Config())
}