2021-05-22 16:12:26 -07:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/netsampler/goflow2/format"
|
2021-06-11 08:42:32 -07:00
|
|
|
"github.com/netsampler/goflow2/format/common"
|
2021-05-22 16:12:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type JsonDriver struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *JsonDriver) Prepare() error {
|
2021-06-11 08:42:32 -07:00
|
|
|
common.HashFlag()
|
|
|
|
common.SelectorFlag()
|
2021-05-22 16:12:26 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *JsonDriver) Init(context.Context) error {
|
2021-06-11 08:42:32 -07:00
|
|
|
err := common.ManualHashInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return common.ManualSelectorInit()
|
2021-05-22 16:12:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *JsonDriver) Format(data interface{}) ([]byte, []byte, error) {
|
|
|
|
msg, ok := data.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("message is not protobuf")
|
|
|
|
}
|
|
|
|
|
2021-06-11 08:42:32 -07:00
|
|
|
key := common.HashProtoLocal(msg)
|
|
|
|
return []byte(key), []byte(common.FormatMessageReflectJSON(msg, "")), nil
|
2021-05-22 16:12:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
d := &JsonDriver{}
|
|
|
|
format.RegisterFormatDriver("json", d)
|
|
|
|
}
|