forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_reg.go
37 lines (29 loc) · 813 Bytes
/
codec_reg.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
package codec
import (
"fmt"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
)
type Factory func(beat.Info, *common.Config) (Codec, error)
type Config struct {
Namespace common.ConfigNamespace `config:",inline"`
}
var codecs = map[string]Factory{}
func RegisterType(name string, gen Factory) {
if _, exists := codecs[name]; exists {
panic(fmt.Sprintf("output codec '%v' already registered ", name))
}
codecs[name] = gen
}
func CreateEncoder(info beat.Info, cfg Config) (Codec, error) {
// default to json codec
codec := "json"
if name := cfg.Namespace.Name(); name != "" {
codec = name
}
factory := codecs[codec]
if factory == nil {
return nil, fmt.Errorf("'%v' output codec is not available", codec)
}
return factory(info, cfg.Namespace.Config())
}