1
0
mirror of https://github.com/netsampler/goflow2.git synced 2024-05-06 15:54:52 +00:00
Louis e52a053c9c formatting improved with selectors and text output (#14)
* Improve format registration
* less interleaving (json does not require importing protobuf)
* generic text renderer
* escape for strings in text/json formatter
2021-06-11 08:42:32 -07:00

42 lines
811 B
Go

package text
import (
"context"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/netsampler/goflow2/format"
"github.com/netsampler/goflow2/format/common"
)
type TextDriver struct {
}
func (d *TextDriver) Prepare() error {
common.HashFlag()
common.SelectorFlag()
return nil
}
func (d *TextDriver) Init(context.Context) error {
err := common.ManualHashInit()
if err != nil {
return err
}
return common.ManualSelectorInit()
}
func (d *TextDriver) Format(data interface{}) ([]byte, []byte, error) {
msg, ok := data.(proto.Message)
if !ok {
return nil, nil, fmt.Errorf("message is not protobuf")
}
key := common.HashProtoLocal(msg)
return []byte(key), []byte(common.FormatMessageReflectText(msg, "")), nil
}
func init() {
d := &TextDriver{}
format.RegisterFormatDriver("text", d)
}